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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 Sky Event Model 1 Sky Event Model
2 =============== 2 ===============
3 3
4 ```dart 4 ```dart
5 SKY MODULE 5 SKY MODULE
6 <!-- part of dart:sky --> 6 <!-- part of dart:sky -->
7 7
8 <script> 8 <script>
9 import 'dart:collection'; 9 import 'dart:collection';
10 import 'dart:async'; 10 import 'dart:async';
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 try { 68 try {
69 item.b(data); 69 item.b(data);
70 } catch (exception, stackTrace) { 70 } catch (exception, stackTrace) {
71 exceptions.add(exception, stackTrace); 71 exceptions.add(exception, stackTrace);
72 } 72 }
73 }); 73 });
74 if (exceptions.length > 0) 74 if (exceptions.length > 0)
75 throw exceptions; 75 throw exceptions;
76 } 76 }
77 77
78 Dispatcher<T> where(Filter<T> filter) { 78 Dispatcher<T> where(Filter<T> filter) => new WhereDispatcher<T>(this, filter);
79 var subdispatcher = new Dispatcher<T>();
80 listen((T data) {
81 if (filter(data))
82 subdispatcher._add(data);
83 });
84 return subdispatcher;
85 }
86 79
87 Dispatcher<T> until(Filter<T> filter) { 80 Dispatcher<T> until(Filter<T> filter) {
88 var subdispatcher = new Dispatcher<T>(); 81 var subdispatcher = new Dispatcher<T>();
89 Handler handler; 82 Handler handler;
90 handler = (T data) { 83 handler = (T data) {
91 if (filter(data)) 84 if (filter(data))
92 unlisten(handler); 85 unlisten(handler);
93 else 86 else
94 subdispatcher._add(data); 87 subdispatcher._add(data);
95 }; 88 };
96 listen(handler); 89 listen(handler);
97 return subdispatcher; 90 return subdispatcher;
98 } 91 }
99 92
100 Future<T> firstWhere(Filter<T> filter) { 93 Future<T> firstWhere(Filter<T> filter) {
101 Completer completer = new Completer(); 94 Completer completer = new Completer();
102 Handler handler; 95 Handler handler;
103 handler = (T data) { 96 handler = (T data) {
104 if (filter(data)) { 97 if (filter(data)) {
105 completer.complete(data); 98 completer.complete(data);
106 unlisten(handler); 99 unlisten(handler);
107 } 100 }
108 }; 101 };
109 listen(handler); 102 listen(handler);
110 return completer.future; 103 return completer.future;
111 } 104 }
112 } 105 }
113 106
107 class WhereDispatcher<T> extends Dispatcher {
108 WhereDispatcher(this.parent, this.filter) : super();
109 Dispatcher parent;
110 Filter filter;
111
112 void listen(Handler<T> handler) {
113 if (_listeners == null || _listeners.length == 0)
114 parent.listen(_handler);
115 super.listen(handler);
116 }
117 bool unlisten(Handler<T> handler) {
118 var result = super.unlisten(handler);
119 if (result && _listeners.length == 0)
120 parent.unlisten(_handler);
121 return result;
122 }
123 void _handler(T data) {
124 if (filter(data))
125 _add(data);
126 }
127 }
128
114 abstract class Event<ReturnType> { 129 abstract class Event<ReturnType> {
115 Event() { init(); } 130 Event() { init(); }
116 void init() { } 131 void init() { }
117 132
118 bool get bubbles; 133 bool get bubbles;
119 134
120 EventTarget _target; 135 EventTarget _target;
121 EventTarget get target => _target; 136 EventTarget get target => _target;
122 137
123 EventTarget _currentTarget; 138 EventTarget _currentTarget;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 return event.result; 194 return event.result;
180 } 195 }
181 196
182 void _dispatchEventLocally(Event event) { 197 void _dispatchEventLocally(Event event) {
183 event._currentTarget = this; 198 event._currentTarget = this;
184 _eventsController.add(event); 199 _eventsController.add(event);
185 } 200 }
186 } 201 }
187 </script> 202 </script>
188 ``` 203 ```
OLDNEW
« 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