arena.dart 4.76 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
class _GestureArenaState {
  final List<GestureArenaMember> members = new List<GestureArenaMember>();
  bool isOpen = true;
48
  bool isHeld = false;
49
  bool hasPendingSweep = false;
50 51 52 53 54 55 56

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

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

61 62
  static final GestureArena instance = new GestureArena();

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

69 70 71 72 73 74 75 76
  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);
  }

77 78 79 80 81 82
  /// Force resolution on this arena, giving the win to the first member
  void sweep(Object key) {
    _GestureArenaState state = _arenas[key];
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    assert(!state.isOpen);
83 84
    if (state.isHeld) {
      state.hasPendingSweep = true;
85
      return;  // This arena is being held for a long-lived member
86 87
    }
    _arenas.remove(key);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (!state.members.isEmpty) {
      // First member wins
      state.members.first.acceptGesture(key);
      // Give all the other members the bad news
      for (int i = 1; i < state.members.length; i++)
        state.members[i].rejectGesture(key);
    }
  }

  /// Prevent the arena from being swept
  void hold(Object key) {
    _GestureArenaState state = _arenas[key];
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    state.isHeld = true;
  }

105 106 107 108 109 110 111 112 113 114 115 116
  /// Release a hold, allowing the arena to be swept
  /// If a sweep was attempted on a held arena, the sweep will be done
  /// on release
  void release(Object key) {
    _GestureArenaState state = _arenas[key];
    if (state == null)
      return;  // This arena either never existed or has been resolved.
    state.isHeld = false;
    if (state.hasPendingSweep)
      sweep(key);
  }

117 118 119 120 121 122 123 124 125 126 127
  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
128
  void _resolve(Object key, GestureArenaMember member, GestureDisposition disposition) {
129 130
    _GestureArenaState state = _arenas[key];
    if (state == null)
131
      return;  // This arena has already resolved.
132 133
    assert(!state.isOpen);
    assert(state.members.contains(member));
Adam Barth's avatar
Adam Barth committed
134
    if (disposition == GestureDisposition.rejected) {
135
      state.members.remove(member);
136
      member.rejectGesture(key);
137
      _tryToResolveArena(key, state);
Adam Barth's avatar
Adam Barth committed
138 139 140
    } else {
      assert(disposition == GestureDisposition.accepted);
      _arenas.remove(key);
141
      for (GestureArenaMember rejectedMember in state.members) {
Adam Barth's avatar
Adam Barth committed
142 143 144 145 146 147 148
        if (rejectedMember != member)
          rejectedMember.rejectGesture(key);
      }
      member.acceptGesture(key);
    }
  }
}