Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(954)

Unified Diff: sky/specs/events.md

Issue 923163003: Specs: change how .where() works so that it won't leak once its own listeners are removed (e.g. by … (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/specs/events.md
diff --git a/sky/specs/events.md b/sky/specs/events.md
index 18cbb33097a86795207d7e2796b72be917124112..56416611497bb7add80b1705ec19b000518f94a6 100644
--- a/sky/specs/events.md
+++ b/sky/specs/events.md
@@ -75,14 +75,7 @@ class Dispatcher<T> {
throw exceptions;
}
- Dispatcher<T> where(Filter<T> filter) {
- var subdispatcher = new Dispatcher<T>();
- listen((T data) {
- if (filter(data))
- subdispatcher._add(data);
- });
- return subdispatcher;
- }
+ Dispatcher<T> where(Filter<T> filter) => new WhereDispatcher<T>(this, filter);
Dispatcher<T> until(Filter<T> filter) {
var subdispatcher = new Dispatcher<T>();
@@ -111,6 +104,28 @@ class Dispatcher<T> {
}
}
+class WhereDispatcher<T> extends Dispatcher {
+ WhereDispatcher(this.parent, this.filter) : super();
+ Dispatcher parent;
+ Filter filter;
+
+ void listen(Handler<T> handler) {
+ if (_listeners == null || _listeners.length == 0)
+ parent.listen(_handler);
+ super.listen(handler);
+ }
+ bool unlisten(Handler<T> handler) {
+ var result = super.unlisten(handler);
+ if (result && _listeners.length == 0)
+ parent.unlisten(_handler);
+ return result;
+ }
+ void _handler(T data) {
+ if (filter(data))
+ _add(data);
+ }
+}
+
abstract class Event<ReturnType> {
Event() { init(); }
void init() { }
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698