OLD | NEW |
| (Empty) |
1 part of angular.core; | |
2 | |
3 NOT_IMPLEMENTED() { | |
4 throw new StateError('Not Implemented'); | |
5 } | |
6 | |
7 typedef EvalFunction0(); | |
8 typedef EvalFunction1(context); | |
9 | |
10 /** | |
11 * Injected into the listener function within [Scope.on] to provide | |
12 * event-specific details to the scope listener. | |
13 */ | |
14 class ScopeEvent { | |
15 static final String DESTROY = 'ng-destroy'; | |
16 | |
17 /** | |
18 * Data attached to the event. This would be the optional parameter | |
19 * from [Scope.emit] and [Scope.broadcast]. | |
20 */ | |
21 final data; | |
22 | |
23 /** | |
24 * The name of the intercepted scope event. | |
25 */ | |
26 final String name; | |
27 | |
28 /** | |
29 * The origin scope that triggered the event (via broadcast or emit). | |
30 */ | |
31 final Scope targetScope; | |
32 | |
33 /** | |
34 * The destination scope that intercepted the event. As | |
35 * the event traverses the scope hierarchy the the event instance | |
36 * stays the same, but the [currentScope] reflects the scope | |
37 * of the current listener which is firing. | |
38 */ | |
39 Scope get currentScope => _currentScope; | |
40 Scope _currentScope; | |
41 | |
42 /** | |
43 * true or false depending on if [stopPropagation] was executed. | |
44 */ | |
45 bool get propagationStopped => _propagationStopped; | |
46 bool _propagationStopped = false; | |
47 | |
48 /** | |
49 * true or false depending on if [preventDefault] was executed. | |
50 */ | |
51 bool get defaultPrevented => _defaultPrevented; | |
52 bool _defaultPrevented = false; | |
53 | |
54 /** | |
55 * [name] - The name of the scope event. | |
56 * [targetScope] - The destination scope that is listening on the event. | |
57 */ | |
58 ScopeEvent(this.name, this.targetScope, this.data); | |
59 | |
60 /** | |
61 * Prevents the intercepted event from propagating further to successive | |
62 * scopes. | |
63 */ | |
64 void stopPropagation () { | |
65 _propagationStopped = true; | |
66 } | |
67 | |
68 /** | |
69 * Sets the defaultPrevented flag to true. | |
70 */ | |
71 void preventDefault() { | |
72 _defaultPrevented = true; | |
73 } | |
74 } | |
75 | |
76 /** | |
77 * Allows the configuration of [Scope.digest] iteration maximum time-to-live | |
78 * value. Digest keeps checking the state of the watcher getters until it | |
79 * can execute one full iteration with no watchers triggering. TTL is used | |
80 * to prevent an infinite loop where watch A triggers watch B which in turn | |
81 * triggers watch A. If the system does not stabilize in TTL iterations then | |
82 * the digest is stopped and an exception is thrown. | |
83 */ | |
84 @NgInjectableService() | |
85 class ScopeDigestTTL { | |
86 final int ttl; | |
87 ScopeDigestTTL(): ttl = 5; | |
88 ScopeDigestTTL.value(this.ttl); | |
89 } | |
90 | |
91 //TODO(misko): I don't think this should be in scope. | |
92 class ScopeLocals implements Map { | |
93 static wrapper(scope, Map<String, Object> locals) => | |
94 new ScopeLocals(scope, locals); | |
95 | |
96 Map _scope; | |
97 Map<String, Object> _locals; | |
98 | |
99 ScopeLocals(this._scope, this._locals); | |
100 | |
101 void operator []=(String name, value) { | |
102 _scope[name] = value; | |
103 } | |
104 dynamic operator [](String name) => | |
105 (_locals.containsKey(name) ? _locals : _scope)[name]; | |
106 | |
107 bool get isEmpty => _scope.isEmpty && _locals.isEmpty; | |
108 bool get isNotEmpty => _scope.isNotEmpty || _locals.isNotEmpty; | |
109 List<String> get keys => _scope.keys; | |
110 List get values => _scope.values; | |
111 int get length => _scope.length; | |
112 | |
113 void forEach(fn) { | |
114 _scope.forEach(fn); | |
115 } | |
116 dynamic remove(key) => _scope.remove(key); | |
117 void clear() { | |
118 _scope.clear; | |
119 } | |
120 bool containsKey(key) => _scope.containsKey(key); | |
121 bool containsValue(key) => _scope.containsValue(key); | |
122 void addAll(map) { | |
123 _scope.addAll(map); | |
124 } | |
125 dynamic putIfAbsent(key, fn) => _scope.putIfAbsent(key, fn); | |
126 } | |
127 | |
128 /** | |
129 * [Scope] is represents a collection of [watch]es [observe]ers, and [context] | |
130 * for the watchers, observers and [eval]uations. Scopes structure loosely | |
131 * mimics the DOM structure. Scopes and [Block]s are bound to each other. | |
132 * As scopes are created and destroyed by [BlockFactory] they are responsible | |
133 * for change detection, change processing and memory management. | |
134 */ | |
135 class Scope { | |
136 | |
137 /** | |
138 * The default execution context for [watch]es [observe]ers, and [eval]uation. | |
139 */ | |
140 final context; | |
141 | |
142 /** | |
143 * The [RootScope] of the application. | |
144 */ | |
145 final RootScope rootScope; | |
146 | |
147 Scope _parentScope; | |
148 | |
149 /** | |
150 * The parent [Scope]. | |
151 */ | |
152 Scope get parentScope => _parentScope; | |
153 | |
154 /** | |
155 * Return `true` if the scope has been destroyed. Once scope is destroyed | |
156 * No operations are allowed on it. | |
157 */ | |
158 bool get isDestroyed { | |
159 var scope = this; | |
160 while(scope != null) { | |
161 if (scope == rootScope) return false; | |
162 scope = scope._parentScope; | |
163 } | |
164 return true; | |
165 } | |
166 | |
167 /** | |
168 * Returns true if the scope is still attached to the [RootScope]. | |
169 */ | |
170 bool get isAttached => !isDestroyed; | |
171 | |
172 // TODO(misko): WatchGroup should be private. | |
173 // Instead we should expose performance stats about the watches | |
174 // such as # of watches, checks/1ms, field checks, function checks, etc | |
175 final WatchGroup _readWriteGroup; | |
176 final WatchGroup _readOnlyGroup; | |
177 | |
178 Scope _childHead, _childTail, _next, _prev; | |
179 _Streams _streams; | |
180 | |
181 /// Do not use. Exposes internal state for testing. | |
182 bool get hasOwnStreams => _streams != null && _streams._scope == this; | |
183 | |
184 Scope(Object this.context, this.rootScope, this._parentScope, | |
185 this._readWriteGroup, this._readOnlyGroup); | |
186 | |
187 /** | |
188 * A [watch] sets up a watch in the [digest] phase of the [apply] cycle. | |
189 * | |
190 * Use [watch] if the reaction function can cause updates to model. In your | |
191 * controller code you will most likely use [watch]. | |
192 */ | |
193 Watch watch(expression, ReactionFn reactionFn, | |
194 {context, FilterMap filters, bool readOnly: false}) { | |
195 assert(isAttached); | |
196 assert(expression != null); | |
197 AST ast; | |
198 Watch watch; | |
199 ReactionFn fn = reactionFn; | |
200 if (expression is AST) { | |
201 ast = expression; | |
202 } else if (expression is String) { | |
203 if (expression.startsWith('::')) { | |
204 expression = expression.substring(2); | |
205 fn = (value, last) { | |
206 if (value != null) { | |
207 watch.remove(); | |
208 return reactionFn(value, last); | |
209 } | |
210 }; | |
211 } else if (expression.startsWith(':')) { | |
212 expression = expression.substring(1); | |
213 fn = (value, last) => value == null ? null : reactionFn(value, last); | |
214 } | |
215 ast = rootScope._astParser(expression, context: context, filters: filters)
; | |
216 } else { | |
217 throw 'expressions must be String or AST got $expression.'; | |
218 } | |
219 return watch = (readOnly ? _readOnlyGroup : _readWriteGroup).watch(ast, fn); | |
220 } | |
221 | |
222 dynamic eval(expression, [Map locals]) { | |
223 assert(isAttached); | |
224 assert(expression == null || | |
225 expression is String || | |
226 expression is Function); | |
227 if (expression is String && expression.isNotEmpty) { | |
228 var obj = locals == null ? context : new ScopeLocals(context, locals); | |
229 return rootScope._parser(expression).eval(obj); | |
230 } | |
231 | |
232 assert(locals == null); | |
233 if (expression is EvalFunction1) return expression(context); | |
234 if (expression is EvalFunction0) return expression(); | |
235 return null; | |
236 } | |
237 | |
238 dynamic applyInZone([expression, Map locals]) => | |
239 rootScope._zone.run(() => apply(expression, locals)); | |
240 | |
241 dynamic apply([expression, Map locals]) { | |
242 _assertInternalStateConsistency(); | |
243 rootScope._transitionState(null, RootScope.STATE_APPLY); | |
244 try { | |
245 return eval(expression, locals); | |
246 } catch (e, s) { | |
247 rootScope._exceptionHandler(e, s); | |
248 } finally { | |
249 rootScope | |
250 .._transitionState(RootScope.STATE_APPLY, null) | |
251 ..digest() | |
252 ..flush(); | |
253 } | |
254 } | |
255 | |
256 ScopeEvent emit(String name, [data]) { | |
257 assert(isAttached); | |
258 return _Streams.emit(this, name, data); | |
259 } | |
260 ScopeEvent broadcast(String name, [data]) { | |
261 assert(isAttached); | |
262 return _Streams.broadcast(this, name, data); | |
263 } | |
264 ScopeStream on(String name) { | |
265 assert(isAttached); | |
266 return _Streams.on(this, rootScope._exceptionHandler, name); | |
267 } | |
268 | |
269 Scope createChild(Object childContext) { | |
270 assert(isAttached); | |
271 var child = new Scope(childContext, rootScope, this, | |
272 _readWriteGroup.newGroup(childContext), | |
273 _readOnlyGroup.newGroup(childContext)); | |
274 var next = null; | |
275 var prev = _childTail; | |
276 child._next = next; | |
277 child._prev = prev; | |
278 if (prev == null) _childHead = child; else prev._next = child; | |
279 if (next == null) _childTail = child; else next._prev = child; | |
280 return child; | |
281 } | |
282 | |
283 void destroy() { | |
284 assert(isAttached); | |
285 broadcast(ScopeEvent.DESTROY); | |
286 _Streams.destroy(this); | |
287 | |
288 if (_prev == null) { | |
289 _parentScope._childHead = _next; | |
290 } else { | |
291 _prev._next = _next; | |
292 } | |
293 if (_next == null) { | |
294 _parentScope._childTail = _prev; | |
295 } else { | |
296 _next._prev = _prev; | |
297 } | |
298 | |
299 _next = _prev = null; | |
300 | |
301 _readWriteGroup.remove(); | |
302 _readOnlyGroup.remove(); | |
303 _parentScope = null; | |
304 _assertInternalStateConsistency(); | |
305 } | |
306 | |
307 _assertInternalStateConsistency() { | |
308 assert((() { | |
309 rootScope._verifyStreams(null, '', []); | |
310 return true; | |
311 })()); | |
312 } | |
313 | |
314 Map<bool,int> _verifyStreams(parentScope, prefix, log) { | |
315 assert(_parentScope == parentScope); | |
316 var counts = {}; | |
317 var typeCounts = _streams == null ? {} : _streams._typeCounts; | |
318 var connection = _streams != null && _streams._scope == this ? '=' : '-'; | |
319 log..add(prefix)..add(hashCode)..add(connection)..add(typeCounts)..add('\n')
; | |
320 if (_streams == null) { | |
321 } else if (_streams._scope == this) { | |
322 _streams._streams.forEach((k, ScopeStream stream){ | |
323 if (stream.subscriptions.isNotEmpty) { | |
324 counts[k] = 1 + (counts.containsKey(k) ? counts[k] : 0); | |
325 } | |
326 }); | |
327 } | |
328 var childScope = _childHead; | |
329 while(childScope != null) { | |
330 childScope._verifyStreams(this, ' $prefix', log).forEach((k, v) { | |
331 counts[k] = v + (counts.containsKey(k) ? counts[k] : 0); | |
332 }); | |
333 childScope = childScope._next; | |
334 } | |
335 if (!_mapEqual(counts, typeCounts)) { | |
336 throw 'Streams actual: $counts != bookkeeping: $typeCounts\n' | |
337 'Offending scope: [scope: ${this.hashCode}]\n' | |
338 '${log.join('')}'; | |
339 } | |
340 return counts; | |
341 } | |
342 } | |
343 | |
344 _mapEqual(Map a, Map b) => a.length == b.length && | |
345 a.keys.every((k) => b.containsKey(k) && a[k] == b[k]); | |
346 | |
347 class ScopeStats { | |
348 bool report = true; | |
349 final nf = new NumberFormat.decimalPattern(); | |
350 | |
351 final digestFieldStopwatch = new AvgStopwatch(); | |
352 final digestEvalStopwatch = new AvgStopwatch(); | |
353 final digestProcessStopwatch = new AvgStopwatch(); | |
354 int _digestLoopNo = 0; | |
355 | |
356 final flushFieldStopwatch = new AvgStopwatch(); | |
357 final flushEvalStopwatch = new AvgStopwatch(); | |
358 final flushProcessStopwatch = new AvgStopwatch(); | |
359 | |
360 ScopeStats({this.report: false}) { | |
361 nf.maximumFractionDigits = 0; | |
362 } | |
363 | |
364 void digestStart() { | |
365 _digestStopwatchReset(); | |
366 _digestLoopNo = 0; | |
367 } | |
368 | |
369 _digestStopwatchReset() { | |
370 digestFieldStopwatch.reset(); | |
371 digestEvalStopwatch.reset(); | |
372 digestProcessStopwatch.reset(); | |
373 } | |
374 | |
375 void digestLoop(int changeCount) { | |
376 _digestLoopNo++; | |
377 if (report) { | |
378 print(this); | |
379 } | |
380 _digestStopwatchReset(); | |
381 } | |
382 | |
383 String _stat(AvgStopwatch s) { | |
384 return '${nf.format(s.count)}' | |
385 ' / ${nf.format(s.elapsedMicroseconds)} us' | |
386 ' = ${nf.format(s.ratePerMs)} #/ms'; | |
387 } | |
388 | |
389 void digestEnd() { | |
390 } | |
391 | |
392 toString() => | |
393 'digest #$_digestLoopNo:' | |
394 'Field: ${_stat(digestFieldStopwatch)} ' | |
395 'Eval: ${_stat(digestEvalStopwatch)} ' | |
396 'Process: ${_stat(digestProcessStopwatch)}'; | |
397 } | |
398 | |
399 | |
400 class RootScope extends Scope { | |
401 static final STATE_APPLY = 'apply'; | |
402 static final STATE_DIGEST = 'digest'; | |
403 static final STATE_FLUSH = 'digest'; | |
404 | |
405 final ExceptionHandler _exceptionHandler; | |
406 final AstParser _astParser; | |
407 final Parser _parser; | |
408 final ScopeDigestTTL _ttl; | |
409 final ExpressionVisitor visitor = new ExpressionVisitor(); // TODO(misko): del
ete me | |
410 final NgZone _zone; | |
411 | |
412 _FunctionChain _runAsyncHead, _runAsyncTail; | |
413 _FunctionChain _domWriteHead, _domWriteTail; | |
414 _FunctionChain _domReadHead, _domReadTail; | |
415 | |
416 final ScopeStats _scopeStats; | |
417 | |
418 String _state; | |
419 | |
420 RootScope(Object context, this._astParser, this._parser, | |
421 GetterCache cacheGetter, FilterMap filterMap, | |
422 this._exceptionHandler, this._ttl, this._zone, | |
423 this._scopeStats) | |
424 : super(context, null, null, | |
425 new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), con
text), | |
426 new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), con
text)) | |
427 { | |
428 _zone.onTurnDone = apply; | |
429 _zone.onError = (e, s, ls) => _exceptionHandler(e, s); | |
430 } | |
431 | |
432 RootScope get rootScope => this; | |
433 bool get isAttached => true; | |
434 | |
435 void digest() { | |
436 _transitionState(null, STATE_DIGEST); | |
437 try { | |
438 var rootWatchGroup = (_readWriteGroup as RootWatchGroup); | |
439 | |
440 int digestTTL = _ttl.ttl; | |
441 const int LOG_COUNT = 3; | |
442 List log; | |
443 List digestLog; | |
444 var count; | |
445 ChangeLog changeLog; | |
446 _scopeStats.digestStart(); | |
447 do { | |
448 while(_runAsyncHead != null) { | |
449 try { | |
450 _runAsyncHead.fn(); | |
451 } catch (e, s) { | |
452 _exceptionHandler(e, s); | |
453 } | |
454 _runAsyncHead = _runAsyncHead._next; | |
455 } | |
456 | |
457 digestTTL--; | |
458 count = rootWatchGroup.detectChanges( | |
459 exceptionHandler: _exceptionHandler, | |
460 changeLog: changeLog, | |
461 fieldStopwatch: _scopeStats.digestFieldStopwatch, | |
462 evalStopwatch: _scopeStats.digestEvalStopwatch, | |
463 processStopwatch: _scopeStats.digestProcessStopwatch); | |
464 | |
465 if (digestTTL <= LOG_COUNT) { | |
466 if (changeLog == null) { | |
467 log = []; | |
468 digestLog = []; | |
469 changeLog = (e, c, p) => digestLog.add('$e: $c <= $p'); | |
470 } else { | |
471 log.add(digestLog.join(', ')); | |
472 digestLog.clear(); | |
473 } | |
474 } | |
475 if (digestTTL == 0) { | |
476 throw 'Model did not stabilize in ${_ttl.ttl} digests. ' | |
477 'Last $LOG_COUNT iterations:\n${log.join('\n')}'; | |
478 } | |
479 _scopeStats.digestLoop(count); | |
480 } while (count > 0); | |
481 } finally { | |
482 _scopeStats.digestEnd(); | |
483 _transitionState(STATE_DIGEST, null); | |
484 } | |
485 } | |
486 | |
487 void flush() { | |
488 _transitionState(null, STATE_FLUSH); | |
489 var observeGroup = this._readOnlyGroup as RootWatchGroup; | |
490 bool runObservers = true; | |
491 try { | |
492 do { | |
493 while(_domWriteHead != null) { | |
494 try { | |
495 _domWriteHead.fn(); | |
496 } catch (e, s) { | |
497 _exceptionHandler(e, s); | |
498 } | |
499 _domWriteHead = _domWriteHead._next; | |
500 } | |
501 if (runObservers) { | |
502 runObservers = false; | |
503 observeGroup.detectChanges(exceptionHandler:_exceptionHandler); | |
504 } | |
505 while(_domReadHead != null) { | |
506 try { | |
507 _domReadHead.fn(); | |
508 } catch (e, s) { | |
509 _exceptionHandler(e, s); | |
510 } | |
511 _domReadHead = _domReadHead._next; | |
512 } | |
513 } while (_domWriteHead != null || _domReadHead != null); | |
514 assert((() { | |
515 var watchLog = []; | |
516 var observeLog = []; | |
517 (_readWriteGroup as RootWatchGroup).detectChanges( | |
518 changeLog: (s, c, p) => watchLog.add('$s: $c <= $p')); | |
519 (observeGroup as RootWatchGroup).detectChanges( | |
520 changeLog: (s, c, p) => watchLog.add('$s: $c <= $p')); | |
521 if (watchLog.isNotEmpty || observeLog.isNotEmpty) { | |
522 throw 'Observer reaction functions should not change model. \n' | |
523 'These watch changes were detected: ${watchLog.join('; ')}\n' | |
524 'These observe changes were detected: ${observeLog.join('; ')}'; | |
525 } | |
526 return true; | |
527 })()); | |
528 } finally { | |
529 _transitionState(STATE_FLUSH, null); | |
530 } | |
531 | |
532 } | |
533 | |
534 // QUEUES | |
535 void runAsync(fn()) { | |
536 var chain = new _FunctionChain(fn); | |
537 if (_runAsyncHead == null) { | |
538 _runAsyncHead = _runAsyncTail = chain; | |
539 } else { | |
540 _runAsyncTail = _runAsyncTail._next = chain; | |
541 } | |
542 } | |
543 | |
544 void domWrite(fn()) { | |
545 var chain = new _FunctionChain(fn); | |
546 if (_domWriteHead == null) { | |
547 _domWriteHead = _domWriteTail = chain; | |
548 } else { | |
549 _domWriteTail = _domWriteTail._next = chain; | |
550 } | |
551 } | |
552 | |
553 void domRead(fn()) { | |
554 var chain = new _FunctionChain(fn); | |
555 if (_domReadHead == null) { | |
556 _domReadHead = _domReadTail = chain; | |
557 } else { | |
558 _domReadTail = _domReadTail._next = chain; | |
559 } | |
560 } | |
561 | |
562 void destroy() {} | |
563 | |
564 void _transitionState(String from, String to) { | |
565 assert(isAttached); | |
566 if (_state != from) throw "$_state already in progress can not enter $to."; | |
567 _state = to; | |
568 } | |
569 } | |
570 | |
571 /** | |
572 * Keeps track of Streams for each Scope. When emitting events | |
573 * we would need to walk the whole tree. Its faster if we can prune | |
574 * the Scopes we have to visit. | |
575 * | |
576 * Scope with no [_ScopeStreams] has no events registered on itself or children | |
577 * | |
578 * We keep track of [Stream]s, and also child scope [Stream]s. To save | |
579 * memory we use the same stream object on all of our parents if they don't | |
580 * have one. But that means that we have to keep track if the stream belongs | |
581 * to the node. | |
582 * | |
583 * Scope with [_ScopeStreams] but who's [_scope] does not match the scope | |
584 * is only inherited | |
585 * | |
586 * Only [Scope] with [_ScopeStreams] who's [_scope] matches the [Scope] | |
587 * instance is the actual scope. | |
588 * | |
589 * Once the [Stream] is created it can not be removed even if all listeners | |
590 * are canceled. That is because we don't know if someone still has reference | |
591 * to it. | |
592 */ | |
593 class _Streams { | |
594 final ExceptionHandler _exceptionHandler; | |
595 /// Scope we belong to. | |
596 final Scope _scope; | |
597 /// [Stream]s for [_scope] only | |
598 final _streams = new Map<String, ScopeStream>(); | |
599 /// Child [Scope] event counts. | |
600 final Map<String, int> _typeCounts; | |
601 | |
602 _Streams(this._scope, this._exceptionHandler, _Streams inheritStreams) | |
603 : _typeCounts = inheritStreams == null | |
604 ? <String, int>{} | |
605 : new Map.from(inheritStreams._typeCounts); | |
606 | |
607 static ScopeEvent emit(Scope scope, String name, data) { | |
608 var event = new ScopeEvent(name, scope, data); | |
609 var scopeCursor = scope; | |
610 while(scopeCursor != null) { | |
611 if (scopeCursor._streams != null && | |
612 scopeCursor._streams._scope == scopeCursor) { | |
613 ScopeStream stream = scopeCursor._streams._streams[name]; | |
614 if (stream != null) { | |
615 event._currentScope = scopeCursor; | |
616 stream._fire(event); | |
617 if (event.propagationStopped) return event; | |
618 } | |
619 } | |
620 scopeCursor = scopeCursor._parentScope; | |
621 } | |
622 return event; | |
623 } | |
624 | |
625 static ScopeEvent broadcast(Scope scope, String name, data) { | |
626 _Streams scopeStreams = scope._streams; | |
627 var event = new ScopeEvent(name, scope, data); | |
628 if (scopeStreams != null && scopeStreams._typeCounts.containsKey(name)) { | |
629 var queue = new Queue()..addFirst(scopeStreams._scope); | |
630 while (queue.isNotEmpty) { | |
631 scope = queue.removeFirst(); | |
632 scopeStreams = scope._streams; | |
633 assert(scopeStreams._scope == scope); | |
634 if (scopeStreams._streams.containsKey(name)) { | |
635 var stream = scopeStreams._streams[name]; | |
636 event._currentScope = scope; | |
637 stream._fire(event); | |
638 } | |
639 // Reverse traversal so that when the queue is read it is correct order. | |
640 var childScope = scope._childTail; | |
641 while(childScope != null) { | |
642 scopeStreams = childScope._streams; | |
643 if (scopeStreams != null && | |
644 scopeStreams._typeCounts.containsKey(name)) { | |
645 queue.addFirst(scopeStreams._scope); | |
646 } | |
647 childScope = childScope._prev; | |
648 } | |
649 } | |
650 } | |
651 return event; | |
652 } | |
653 | |
654 static ScopeStream on(Scope scope, | |
655 ExceptionHandler _exceptionHandler, | |
656 String name) { | |
657 _forceNewScopeStream(scope, _exceptionHandler); | |
658 return scope._streams._get(scope, name); | |
659 } | |
660 | |
661 static void _forceNewScopeStream(scope, _exceptionHandler) { | |
662 _Streams streams = scope._streams; | |
663 Scope scopeCursor = scope; | |
664 bool splitMode = false; | |
665 while(scopeCursor != null) { | |
666 _Streams cursorStreams = scopeCursor._streams; | |
667 var hasStream = cursorStreams != null; | |
668 var hasOwnStream = hasStream && cursorStreams._scope == scopeCursor; | |
669 if (hasOwnStream) return; | |
670 | |
671 if (!splitMode && (streams == null || (hasStream && !hasOwnStream))) { | |
672 if (hasStream && !hasOwnStream) { | |
673 splitMode = true; | |
674 } | |
675 streams = new _Streams(scopeCursor, _exceptionHandler, cursorStreams); | |
676 } | |
677 scopeCursor._streams = streams; | |
678 scopeCursor = scopeCursor._parentScope; | |
679 } | |
680 } | |
681 | |
682 static void destroy(Scope scope) { | |
683 var toBeDeletedStreams = scope._streams; | |
684 if (toBeDeletedStreams == null) return; // no streams to clean up | |
685 var parentScope = scope._parentScope; // skip current scope as not to delete
listeners | |
686 // find the parent-most scope which still has our stream to be deleted. | |
687 while (parentScope != null && parentScope._streams == toBeDeletedStreams) { | |
688 parentScope._streams = null; | |
689 parentScope = parentScope._parentScope; | |
690 } | |
691 // At this point scope is the parent-most scope which has its own typeCounts | |
692 if (parentScope == null) return; | |
693 var parentStreams = parentScope._streams; | |
694 assert(parentStreams != toBeDeletedStreams); | |
695 // remove typeCounts from the scope to be destroyed from the parent | |
696 // typeCounts | |
697 toBeDeletedStreams._typeCounts.forEach( | |
698 (name, count) => parentStreams._addCount(name, -count)); | |
699 } | |
700 | |
701 async.Stream _get(Scope scope, String name) { | |
702 assert(scope._streams == this); | |
703 assert(scope._streams._scope == scope); | |
704 assert(_exceptionHandler != null); | |
705 return _streams.putIfAbsent(name, () => | |
706 new ScopeStream(this, _exceptionHandler, name)); | |
707 } | |
708 | |
709 void _addCount(String name, int amount) { | |
710 // decrement the counters on all parent scopes | |
711 _Streams lastStreams = null; | |
712 var scope = _scope; | |
713 while (scope != null) { | |
714 if (lastStreams != scope._streams) { | |
715 // we have a transition, need to decrement it | |
716 lastStreams = scope._streams; | |
717 int count = lastStreams._typeCounts[name]; | |
718 count = count == null ? amount : count + amount; | |
719 assert(count >= 0); | |
720 if (count == 0) { | |
721 lastStreams._typeCounts.remove(name); | |
722 if (_scope == scope) _streams.remove(name); | |
723 } else { | |
724 lastStreams._typeCounts[name] = count; | |
725 } | |
726 } | |
727 scope = scope._parentScope; | |
728 } | |
729 } | |
730 } | |
731 | |
732 class ScopeStream extends async.Stream<ScopeEvent> { | |
733 final ExceptionHandler _exceptionHandler; | |
734 final _Streams _streams; | |
735 final String _name; | |
736 final subscriptions = <ScopeStreamSubscription>[]; | |
737 | |
738 ScopeStream(this._streams, this._exceptionHandler, this._name); | |
739 | |
740 ScopeStreamSubscription listen(void onData(ScopeEvent event), | |
741 { Function onError, | |
742 void onDone(), | |
743 bool cancelOnError }) { | |
744 if (subscriptions.isEmpty) _streams._addCount(_name, 1); | |
745 var subscription = new ScopeStreamSubscription(this, onData); | |
746 subscriptions.add(subscription); | |
747 return subscription; | |
748 } | |
749 | |
750 void _fire(ScopeEvent event) { | |
751 for (ScopeStreamSubscription subscription in subscriptions) { | |
752 try { | |
753 subscription._onData(event); | |
754 } catch (e, s) { | |
755 _exceptionHandler(e, s); | |
756 } | |
757 } | |
758 } | |
759 | |
760 void _remove(ScopeStreamSubscription subscription) { | |
761 assert(subscription._scopeStream == this); | |
762 if (subscriptions.remove(subscription)) { | |
763 if (subscriptions.isEmpty) _streams._addCount(_name, -1); | |
764 } else { | |
765 throw new StateError('AlreadyCanceled'); | |
766 } | |
767 } | |
768 } | |
769 | |
770 class ScopeStreamSubscription implements async.StreamSubscription<ScopeEvent> { | |
771 final ScopeStream _scopeStream; | |
772 final Function _onData; | |
773 ScopeStreamSubscription(this._scopeStream, this._onData); | |
774 | |
775 // TODO(vbe) should return a Future | |
776 cancel() => _scopeStream._remove(this); | |
777 | |
778 void onData(void handleData(ScopeEvent data)) => NOT_IMPLEMENTED(); | |
779 void onError(Function handleError) => NOT_IMPLEMENTED(); | |
780 void onDone(void handleDone()) => NOT_IMPLEMENTED(); | |
781 void pause([async.Future resumeSignal]) => NOT_IMPLEMENTED(); | |
782 void resume() => NOT_IMPLEMENTED(); | |
783 bool get isPaused => NOT_IMPLEMENTED(); | |
784 async.Future asFuture([var futureValue]) => NOT_IMPLEMENTED(); | |
785 } | |
786 | |
787 class _FunctionChain { | |
788 final Function fn; | |
789 _FunctionChain _next; | |
790 | |
791 _FunctionChain(fn()) | |
792 : fn = fn | |
793 { | |
794 assert(fn != null); | |
795 } | |
796 } | |
797 | |
798 class AstParser { | |
799 final Parser _parser; | |
800 int _id = 0; | |
801 ExpressionVisitor _visitor = new ExpressionVisitor(); | |
802 | |
803 AstParser(this._parser); | |
804 | |
805 AST call(String exp, { FilterMap filters, | |
806 bool collection:false, | |
807 Object context:null }) { | |
808 _visitor.filters = filters; | |
809 AST contextRef = _visitor.contextRef; | |
810 try { | |
811 if (context != null) { | |
812 _visitor.contextRef = new ConstantAST(context, '#${_id++}'); | |
813 } | |
814 var ast = _parser(exp); | |
815 return collection ? _visitor.visitCollection(ast) : _visitor.visit(ast); | |
816 } finally { | |
817 _visitor.contextRef = contextRef; | |
818 _visitor.filters = null; | |
819 } | |
820 } | |
821 } | |
822 | |
823 class ExpressionVisitor implements Visitor { | |
824 static final ContextReferenceAST scopeContextRef = new ContextReferenceAST(); | |
825 AST contextRef = scopeContextRef; | |
826 | |
827 AST ast; | |
828 FilterMap filters; | |
829 | |
830 AST visit(Expression exp) { | |
831 exp.accept(this); | |
832 assert(this.ast != null); | |
833 try { | |
834 return ast; | |
835 } finally { | |
836 ast = null; | |
837 } | |
838 } | |
839 | |
840 AST visitCollection(Expression exp) => new CollectionAST(visit(exp)); | |
841 AST _mapToAst(Expression expression) => visit(expression); | |
842 | |
843 List<AST> _toAst(List<Expression> expressions) => | |
844 expressions.map(_mapToAst).toList(); | |
845 | |
846 void visitCallScope(CallScope exp) { | |
847 ast = new MethodAST(contextRef, exp.name, _toAst(exp.arguments)); | |
848 } | |
849 void visitCallMember(CallMember exp) { | |
850 ast = new MethodAST(visit(exp.object), exp.name, _toAst(exp.arguments)); | |
851 } | |
852 visitAccessScope(AccessScope exp) { | |
853 ast = new FieldReadAST(contextRef, exp.name); | |
854 } | |
855 visitAccessMember(AccessMember exp) { | |
856 ast = new FieldReadAST(visit(exp.object), exp.name); | |
857 } | |
858 visitBinary(Binary exp) { | |
859 ast = new PureFunctionAST(exp.operation, | |
860 _operationToFunction(exp.operation), | |
861 [visit(exp.left), visit(exp.right)]); | |
862 } | |
863 void visitPrefix(Prefix exp) { | |
864 ast = new PureFunctionAST(exp.operation, | |
865 _operationToFunction(exp.operation), | |
866 [visit(exp.expression)]); | |
867 } | |
868 void visitConditional(Conditional exp) { | |
869 ast = new PureFunctionAST('?:', _operation_ternary, | |
870 [visit(exp.condition), visit(exp.yes), | |
871 visit(exp.no)]); | |
872 } | |
873 void visitAccessKeyed(AccessKeyed exp) { | |
874 ast = new PureFunctionAST('[]', _operation_bracket, | |
875 [visit(exp.object), visit(exp.key)]); | |
876 } | |
877 void visitLiteralPrimitive(LiteralPrimitive exp) { | |
878 ast = new ConstantAST(exp.value); | |
879 } | |
880 void visitLiteralString(LiteralString exp) { | |
881 ast = new ConstantAST(exp.value); | |
882 } | |
883 void visitLiteralArray(LiteralArray exp) { | |
884 List<AST> items = _toAst(exp.elements); | |
885 ast = new PureFunctionAST('[${items.join(', ')}]', new ArrayFn(), items); | |
886 } | |
887 | |
888 void visitLiteralObject(LiteralObject exp) { | |
889 List<String> keys = exp.keys; | |
890 List<AST> values = _toAst(exp.values); | |
891 assert(keys.length == values.length); | |
892 var kv = <String>[]; | |
893 for (var i = 0; i < keys.length; i++) { | |
894 kv.add('${keys[i]}: ${values[i]}'); | |
895 } | |
896 ast = new PureFunctionAST('{${kv.join(', ')}}', new MapFn(keys), values); | |
897 } | |
898 | |
899 void visitFilter(Filter exp) { | |
900 Function filterFunction = filters(exp.name); | |
901 List<AST> args = [visitCollection(exp.expression)]; | |
902 args.addAll(_toAst(exp.arguments).map((ast) => new CollectionAST(ast))); | |
903 ast = new PureFunctionAST('|${exp.name}', | |
904 new _FilterWrapper(filterFunction, args.length), args); | |
905 } | |
906 | |
907 // TODO(misko): this is a corner case. Choosing not to implement for now. | |
908 void visitCallFunction(CallFunction exp) { | |
909 _notSupported("function's returing functions"); | |
910 } | |
911 void visitAssign(Assign exp) { | |
912 _notSupported('assignement'); | |
913 } | |
914 void visitLiteral(Literal exp) { | |
915 _notSupported('literal'); | |
916 } | |
917 void visitExpression(Expression exp) { | |
918 _notSupported('?'); | |
919 } | |
920 void visitChain(Chain exp) { | |
921 _notSupported(';'); | |
922 } | |
923 | |
924 void _notSupported(String name) { | |
925 throw new StateError("Can not watch expression containing '$name'."); | |
926 } | |
927 } | |
928 | |
929 Function _operationToFunction(String operation) { | |
930 switch(operation) { | |
931 case '!' : return _operation_negate; | |
932 case '+' : return _operation_add; | |
933 case '-' : return _operation_subtract; | |
934 case '*' : return _operation_multiply; | |
935 case '/' : return _operation_divide; | |
936 case '~/' : return _operation_divide_int; | |
937 case '%' : return _operation_remainder; | |
938 case '==' : return _operation_equals; | |
939 case '!=' : return _operation_not_equals; | |
940 case '<' : return _operation_less_then; | |
941 case '>' : return _operation_greater_then; | |
942 case '<=' : return _operation_less_or_equals_then; | |
943 case '>=' : return _operation_greater_or_equals_then; | |
944 case '^' : return _operation_power; | |
945 case '&' : return _operation_bitwise_and; | |
946 case '&&' : return _operation_logical_and; | |
947 case '||' : return _operation_logical_or; | |
948 default: throw new StateError(operation); | |
949 } | |
950 } | |
951 | |
952 _operation_negate(value) => !toBool(value); | |
953 _operation_add(left, right) => autoConvertAdd(left, right); | |
954 _operation_subtract(left, right) => left - right; | |
955 _operation_multiply(left, right) => left * right; | |
956 _operation_divide(left, right) => left / right; | |
957 _operation_divide_int(left, right) => left ~/ right; | |
958 _operation_remainder(left, right) => left % right; | |
959 _operation_equals(left, right) => left == right; | |
960 _operation_not_equals(left, right) => left != right; | |
961 _operation_less_then(left, right) => left < right; | |
962 _operation_greater_then(left, right) => (left == null || right == null
) ? false : left > right; | |
963 _operation_less_or_equals_then(left, right) => left <= right; | |
964 _operation_greater_or_equals_then(left, right) => left >= right; | |
965 _operation_power(left, right) => left ^ right; | |
966 _operation_bitwise_and(left, right) => left & right; | |
967 // TODO(misko): these should short circuit the evaluation. | |
968 _operation_logical_and(left, right) => toBool(left) && toBool(right); | |
969 _operation_logical_or(left, right) => toBool(left) || toBool(right); | |
970 | |
971 _operation_ternary(condition, yes, no) => toBool(condition) ? yes : no; | |
972 _operation_bracket(obj, key) => obj == null ? null : obj[key]; | |
973 | |
974 class ArrayFn extends FunctionApply { | |
975 // TODO(misko): figure out why do we need to make a copy? | |
976 apply(List args) => new List.from(args); | |
977 } | |
978 | |
979 class MapFn extends FunctionApply { | |
980 final List<String> keys; | |
981 | |
982 MapFn(this.keys); | |
983 | |
984 apply(List values) { | |
985 // TODO(misko): figure out why do we need to make a copy instead of reusing
instance? | |
986 assert(values.length == keys.length); | |
987 return new Map.fromIterables(keys, values); | |
988 } | |
989 } | |
990 | |
991 class _FilterWrapper extends FunctionApply { | |
992 final Function filterFn; | |
993 final List args; | |
994 final List<Watch> argsWatches; | |
995 _FilterWrapper(this.filterFn, length): | |
996 args = new List(length), | |
997 argsWatches = new List(length); | |
998 | |
999 apply(List values) { | |
1000 for (var i=0; i < values.length; i++) { | |
1001 var value = values[i]; | |
1002 var lastValue = args[i]; | |
1003 if (!identical(value, lastValue)) { | |
1004 if (value is CollectionChangeRecord) { | |
1005 args[i] = (value as CollectionChangeRecord).iterable; | |
1006 } else { | |
1007 args[i] = value; | |
1008 } | |
1009 } | |
1010 } | |
1011 var value = Function.apply(filterFn, args); | |
1012 if (value is Iterable) { | |
1013 // Since filters are pure we can guarantee that this well never change. | |
1014 // By wrapping in UnmodifiableListView we can hint to the dirty checker | |
1015 // and short circuit the iterator. | |
1016 value = new UnmodifiableListView(value); | |
1017 } | |
1018 return value; | |
1019 } | |
1020 } | |
OLD | NEW |