arena.dart 3.42 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5 6 7 8 9
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

enum GestureDisposition {
  accepted,
  rejected
}

10 11 12 13 14 15 16 17
/// Represents an object participating in an arena.
///
/// Receives callbacks from the GestureArena to notify the object when it wins
/// or loses a gesture negotiation. Exactly one of [acceptGesture] or
/// [rejectGesture] will be called for each arena key this member was added to,
/// regardless of what caused the arena to be resolved. For example, if a
/// member resolves the arena itself, that member still receives an
/// [acceptGesture] callback.
Adam Barth's avatar
Adam Barth committed
18
abstract class GestureArenaMember {
19
  /// Called when this member wins the arena for the given key.
Adam Barth's avatar
Adam Barth committed
20 21
  void acceptGesture(Object key);

22
  /// Called when this member loses the arena for the given key.
Adam Barth's avatar
Adam Barth committed
23 24 25
  void rejectGesture(Object key);
}

26 27 28 29
/// An interface to information to an arena
///
/// A given [GestureArenaMember] can have multiple entries in multiple arenas
/// with different keys.
Adam Barth's avatar
Adam Barth committed
30 31 32 33 34 35 36 37
class GestureArenaEntry {
  GestureArenaEntry._(this._arena, this._key, this._member);

  final GestureArena _arena;
  final Object _key;
  final GestureArenaMember _member;

  /// Call this member to claim victory (with accepted) or admit defeat (with rejected).
38 39
  ///
  /// It's fine to attempt to resolve an arena that is already resolved.
Adam Barth's avatar
Adam Barth committed
40 41 42 43 44
  void resolve(GestureDisposition disposition) {
    _arena._resolve(_key, _member, disposition);
  }
}

45 46 47 48 49 50 51 52 53 54
class _GestureArenaState {
  final List<GestureArenaMember> members = new List<GestureArenaMember>();
  bool isOpen = true;

  void add(GestureArenaMember member) {
    assert(isOpen);
    members.add(member);
  }
}

Adam Barth's avatar
Adam Barth committed
55 56
/// The first member to accept or the last member to not to reject wins.
class GestureArena {
57
  final Map<Object, _GestureArenaState> _arenas = new Map<Object, _GestureArenaState>();
Adam Barth's avatar
Adam Barth committed
58

59 60
  static final GestureArena instance = new GestureArena();

Adam Barth's avatar
Adam Barth committed
61
  GestureArenaEntry add(Object key, GestureArenaMember member) {
62 63
    _GestureArenaState state = _arenas.putIfAbsent(key, () => new _GestureArenaState());
    state.add(member);
Adam Barth's avatar
Adam Barth committed
64 65 66
    return new GestureArenaEntry._(this, key, member);
  }

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  void close(Object key) {
    _GestureArenaState state = _arenas[key];
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    state.isOpen = false;
    _tryToResolveArena(key, state);
  }

  void _tryToResolveArena(Object key, _GestureArenaState state) {
    assert(_arenas[key] == state);
    assert(!state.isOpen);
    if (state.members.length == 1) {
      _arenas.remove(key);
      state.members.first.acceptGesture(key);
    } else if (state.members.isEmpty) {
      _arenas.remove(key);
    }
  }

Adam Barth's avatar
Adam Barth committed
86
  void _resolve(Object key, GestureArenaMember member, GestureDisposition disposition) {
87 88
    _GestureArenaState state = _arenas[key];
    if (state == null)
89
      return;  // This arena has already resolved.
90 91
    assert(!state.isOpen);
    assert(state.members.contains(member));
Adam Barth's avatar
Adam Barth committed
92
    if (disposition == GestureDisposition.rejected) {
93
      state.members.remove(member);
94
      member.rejectGesture(key);
95
      _tryToResolveArena(key, state);
Adam Barth's avatar
Adam Barth committed
96 97 98
    } else {
      assert(disposition == GestureDisposition.accepted);
      _arenas.remove(key);
99
      for (GestureArenaMember rejectedMember in state.members) {
Adam Barth's avatar
Adam Barth committed
100 101 102 103 104 105 106
        if (rejectedMember != member)
          rejectedMember.rejectGesture(key);
      }
      member.acceptGesture(key);
    }
  }
}