Created
April 27, 2016 19:20
-
-
Save abarth/6e4d7f5745a639ab9473e1cdc2aa8a05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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