Skip to content

Instantly share code, notes, and snippets.

@abarth
Created April 27, 2016 19:20
Show Gist options
  • Save abarth/6e4d7f5745a639ab9473e1cdc2aa8a05 to your computer and use it in GitHub Desktop.
Save abarth/6e4d7f5745a639ab9473e1cdc2aa8a05 to your computer and use it in GitHub Desktop.
class Listenable {
final Set<VoidCallback> _listeners = new Set<VoidCallback>();
void addListener(VoidCallback listener) {
assert(!_listeners.contains(listener));
_listeners.add(listener);
}
void removeListener(VoidCallback listener) {
assert(_listeners.contains(listener));
_listeners.remove(listener);
}
void notifyListeners() {
if (_notificationScheduled)
return;
_notificationScheduled = true;
scheduleMicrotask(_dispatchNotifications);
}
bool _notificationScheduled = false;
void _dispatchNotifications() {
_notificationScheduled = false;
List<VoidCallback> localListeners = new List<VoidCallback>.from(_listeners);
for (VoidCallback listener in localListeners)
listener();
}
}
class ChartData extends Listenable {
String get foo => _foo;
String _foo;
void set foo(String value) {
if (foo == value)
return;
_foo = foo;
notifyListeners();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment