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

Side by Side Diff: sky/specs/events.md

Issue 919693007: Specs: Implement the Dispatcher classes. (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 | sky/specs/script.md » ('j') | 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 sky:core --> 6 <!-- part of sky:core -->
7 7
8 <script> 8 <script>
9 import 'dart:collection';
10 import 'dart:async';
11
12 class ExceptionAndStackTrace<T> {
13 const ExceptionAndStackTrace(this.exception, this.stackTrace);
14 final T exception;
15 final StackTrace stackTrace;
16 }
17
18 class ExceptionListException<T> extends Object with IterableMixin<ExceptionAndSt ackTrace<T>> implements Exception {
abarth-chromium 2015/02/12 00:28:13 extends Object seems a bit silly
Hixie 2015/02/12 21:31:43 Fixed. Turns out you just need extends IterableMix
19 List<ExceptionAndStackTrace<T>> _exceptions;
20 void add(T exception, [StackTrace stackTrace = null]) {
21 if (_exceptions == null)
22 _exceptions = new List<ExceptionAndStackTrace<T>>();
23 _exceptions.add(new ExceptionAndStackTrace<T>(exception, stackTrace));
24 }
25 int get length => _exceptions == null ? 0 : _exceptions.length;
26 Iterator<ExceptionAndStackTrace<T>> get iterator => _exceptions.iterator;
27 }
28
29 typedef bool Filter<T>(T t);
30 typedef void Handler<T>(T t);
31
32 class DispatcherController<T> {
33 Dispatcher<T> _dispatcher;
abarth-chromium 2015/02/12 00:28:13 final
Hixie 2015/02/12 21:31:43 Done.
34 Dispatcher<T> get dispatcher => _dispatcher;
35 DispatcherController() {
36 _dispatcher = new Dispatcher<T>._internal();
37 }
38 void add(T data) => _dispatcher._add(data);
39 }
40
41 class Dispatcher<T> {
42 Dispatcher.silent() { } // returns a dispatcher that never gets an event
43 Dispatcher._internal() { }
abarth-chromium 2015/02/12 00:28:13 Remove?
Hixie 2015/02/12 21:31:43 Done.
44
45 List<Handler> _listeners;
46 void listen(Handler<T> handler) {
47 // you should not throw out of this handler
48 if (_listeners == null)
49 _listeners = new List<Handler>();
50 _listeners.add(handler);
51 }
52 bool unlisten(Handler<T> handler) {
53 if (_listeners == null)
54 return false;
55 return _listeners.remove(handler);
56 }
57 void _add(T data) {
58 if (_listeners == null)
59 return;
60 ExceptionListException exceptions = new ExceptionListException();
61 // we make a copy of the list here so that the listeners can
62 // mutate our list without worry
63 _listeners.toList().forEach((Handler handler) {
64 try {
65 handler(data);
abarth-chromium 2015/02/12 00:28:13 Zone?
Hixie 2015/02/12 21:31:43 Done.
66 } catch (exception, stackTrace) {
67 exceptions.add(exception, stackTrace);
68 }
69 });
70 if (exceptions.length > 0)
71 throw exceptions;
72 }
73
74 Dispatcher<T> where(Filter<T> filter) {
75 var subdispatcher = new Dispatcher<T>._internal();
76 listen((T data) {
77 if (filter(data))
78 subdispatcher._add(data);
79 });
80 return subdispatcher;
81 }
82
83 Dispatcher<T> until(Filter<T> filter) {
84 var subdispatcher = new Dispatcher<T>._internal();
85 Handler handler;
86 handler = (T data) {
87 if (filter(data))
88 unlisten(handler);
89 else
90 subdispatcher._add(data);
91 };
92 listen(handler);
93 return subdispatcher;
94 }
95
96 Future<T> firstWhere(Filter<T> filter) {
97 Completer completer = new Completer();
98 Handler handler;
99 handler = (T data) {
100 if (filter(data)) {
101 completer.complete(data);
102 unlisten(handler);
103 }
104 };
105 listen(handler);
106 return completer.future;
107 }
108 }
109
9 abstract class Event<ReturnType> { 110 abstract class Event<ReturnType> {
10 Event() { init(); } 111 Event() { init(); }
11 void init() { } 112 void init() { }
12 113
13 bool get bubbles; 114 bool get bubbles;
14 115
15 EventTarget _target; 116 EventTarget _target;
16 EventTarget get target => _target; 117 EventTarget get target => _target;
17 118
18 EventTarget _currentTarget; 119 EventTarget _currentTarget;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 return event.result; 172 return event.result;
72 } 173 }
73 174
74 void _dispatchEventLocally(@nonnull Event event) { 175 void _dispatchEventLocally(@nonnull Event event) {
75 event._currentTarget = this; 176 event._currentTarget = this;
76 _eventsController.add(event); 177 _eventsController.add(event);
77 } 178 }
78 } 179 }
79 </script> 180 </script>
80 ``` 181 ```
OLDNEW
« no previous file with comments | « no previous file | sky/specs/script.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698