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

Unified Diff: third_party/pkg/angular/lib/core/scope.dart

Issue 180843004: Revert revision 33053 (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 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 | « third_party/pkg/angular/lib/core/registry.dart ('k') | third_party/pkg/angular/lib/core/zone.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/pkg/angular/lib/core/scope.dart
===================================================================
--- third_party/pkg/angular/lib/core/scope.dart (revision 33054)
+++ third_party/pkg/angular/lib/core/scope.dart (working copy)
@@ -1,1020 +1,1037 @@
part of angular.core;
-NOT_IMPLEMENTED() {
- throw new StateError('Not Implemented');
-}
-typedef EvalFunction0();
-typedef EvalFunction1(context);
-
/**
- * Injected into the listener function within [Scope.on] to provide
- * event-specific details to the scope listener.
+ * Injected into the listener function within [Scope.$on] to provide event-specific
+ * details to the scope listener.
*/
class ScopeEvent {
- static final String DESTROY = 'ng-destroy';
/**
- * Data attached to the event. This would be the optional parameter
- * from [Scope.emit] and [Scope.broadcast].
- */
- final data;
-
- /**
* The name of the intercepted scope event.
*/
- final String name;
+ String name;
/**
- * The origin scope that triggered the event (via broadcast or emit).
+ * The origin scope that triggered the event (via $broadcast or $emit).
*/
- final Scope targetScope;
+ Scope targetScope;
/**
- * The destination scope that intercepted the event. As
- * the event traverses the scope hierarchy the the event instance
- * stays the same, but the [currentScope] reflects the scope
- * of the current listener which is firing.
+ * The destination scope that intercepted the event.
*/
- Scope get currentScope => _currentScope;
- Scope _currentScope;
+ Scope currentScope;
/**
- * true or false depending on if [stopPropagation] was executed.
+ * true or false depending on if stopPropagation() was executed.
*/
- bool get propagationStopped => _propagationStopped;
- bool _propagationStopped = false;
+ bool propagationStopped = false;
/**
- * true or false depending on if [preventDefault] was executed.
+ * true or false depending on if preventDefault() was executed.
*/
- bool get defaultPrevented => _defaultPrevented;
- bool _defaultPrevented = false;
+ bool defaultPrevented = false;
/**
- * [name] - The name of the scope event.
- * [targetScope] - The destination scope that is listening on the event.
+ ** [name] - The name of the scope event.
+ ** [targetScope] - The destination scope that is listening on the event.
*/
- ScopeEvent(this.name, this.targetScope, this.data);
+ ScopeEvent(this.name, this.targetScope);
/**
- * Prevents the intercepted event from propagating further to successive
- * scopes.
+ * Prevents the intercepted event from propagating further to successive scopes.
*/
- void stopPropagation () {
- _propagationStopped = true;
- }
+ stopPropagation () => propagationStopped = true;
/**
* Sets the defaultPrevented flag to true.
*/
- void preventDefault() {
- _defaultPrevented = true;
- }
+ preventDefault() => defaultPrevented = true;
}
/**
- * Allows the configuration of [Scope.digest] iteration maximum time-to-live
+ * Allows the configuration of [Scope.$digest] iteration maximum time-to-live
* value. Digest keeps checking the state of the watcher getters until it
* can execute one full iteration with no watchers triggering. TTL is used
* to prevent an infinite loop where watch A triggers watch B which in turn
- * triggers watch A. If the system does not stabilize in TTL iterations then
- * the digest is stopped and an exception is thrown.
+ * triggers watch A. If the system does not stabilize in TTL iteration then
+ * an digest is stop an an exception is thrown.
*/
@NgInjectableService()
class ScopeDigestTTL {
- final int ttl;
+ final num ttl;
ScopeDigestTTL(): ttl = 5;
- ScopeDigestTTL.value(this.ttl);
+ ScopeDigestTTL.value(num this.ttl);
}
-//TODO(misko): I don't think this should be in scope.
-class ScopeLocals implements Map {
- static wrapper(scope, Map<String, Object> locals) =>
- new ScopeLocals(scope, locals);
-
- Map _scope;
- Map<String, Object> _locals;
-
- ScopeLocals(this._scope, this._locals);
-
- void operator []=(String name, value) {
- _scope[name] = value;
- }
- dynamic operator [](String name) =>
- (_locals.containsKey(name) ? _locals : _scope)[name];
-
- bool get isEmpty => _scope.isEmpty && _locals.isEmpty;
- bool get isNotEmpty => _scope.isNotEmpty || _locals.isNotEmpty;
- List<String> get keys => _scope.keys;
- List get values => _scope.values;
- int get length => _scope.length;
-
- void forEach(fn) {
- _scope.forEach(fn);
- }
- dynamic remove(key) => _scope.remove(key);
- void clear() {
- _scope.clear;
- }
- bool containsKey(key) => _scope.containsKey(key);
- bool containsValue(key) => _scope.containsValue(key);
- void addAll(map) {
- _scope.addAll(map);
- }
- dynamic putIfAbsent(key, fn) => _scope.putIfAbsent(key, fn);
-}
-
/**
- * [Scope] is represents a collection of [watch]es [observe]ers, and [context]
- * for the watchers, observers and [eval]uations. Scopes structure loosely
- * mimics the DOM structure. Scopes and [Block]s are bound to each other.
- * As scopes are created and destroyed by [BlockFactory] they are responsible
- * for change detection, change processing and memory management.
+ * Scope has two responsibilities. 1) to keep track af watches and 2)
+ * to keep references to the model so that they are available for
+ * data-binding.
*/
-class Scope {
+@proxy
+@NgInjectableService()
+class Scope implements Map {
+ final ExceptionHandler _exceptionHandler;
+ final Parser _parser;
+ final NgZone _zone;
+ final num _ttl;
+ final Map<String, Object> _properties = {};
+ final _WatchList _watchers = new _WatchList();
+ final Map<String, List<Function>> _listeners = {};
+ final bool _isolate;
+ final bool _lazy;
+ final Profiler _perf;
/**
- * The default execution context for [watch]es [observe]ers, and [eval]uation.
+ * The direct parent scope that created this scope (this can also be the $rootScope)
*/
- final context;
+ final Scope $parent;
/**
- * The [RootScope] of the application.
+ * The auto-incremented ID of the scope
*/
- final RootScope rootScope;
+ String $id;
- Scope _parentScope;
-
/**
- * The parent [Scope].
+ * The topmost scope of the application (same as $rootScope).
*/
- Scope get parentScope => _parentScope;
+ Scope $root;
+ num _nextId = 0;
+ String _phase;
+ List _innerAsyncQueue;
+ List _outerAsyncQueue;
+ Scope _nextSibling, _prevSibling, _childHead, _childTail;
+ bool _skipAutoDigest = false;
+ bool _disabled = false;
- /**
- * Return `true` if the scope has been destroyed. Once scope is destroyed
- * No operations are allowed on it.
- */
- bool get isDestroyed {
- var scope = this;
- while(scope != null) {
- if (scope == rootScope) return false;
- scope = scope._parentScope;
- }
- return true;
+ _set$Properties() {
+ _properties[r'this'] = this;
+ _properties[r'$id'] = this.$id;
+ _properties[r'$parent'] = this.$parent;
+ _properties[r'$root'] = this.$root;
}
- /**
- * Returns true if the scope is still attached to the [RootScope].
- */
- bool get isAttached => !isDestroyed;
+ Scope(this._exceptionHandler, this._parser, ScopeDigestTTL ttl,
+ this._zone, this._perf):
+ $parent = null, _isolate = false, _lazy = false, _ttl = ttl.ttl {
+ $root = this;
+ $id = '_${$root._nextId++}';
+ _innerAsyncQueue = [];
+ _outerAsyncQueue = [];
- // TODO(misko): WatchGroup should be private.
- // Instead we should expose performance stats about the watches
- // such as # of watches, checks/1ms, field checks, function checks, etc
- final WatchGroup _readWriteGroup;
- final WatchGroup _readOnlyGroup;
+ // Set up the zone to auto digest this scope.
+ _zone.onTurnDone = _autoDigestOnTurnDone;
+ _zone.onError = (e, s, ls) => _exceptionHandler(e, s);
+ _set$Properties();
+ }
- Scope _childHead, _childTail, _next, _prev;
- _Streams _streams;
+ Scope._child(Scope parent, bool this._isolate, bool this._lazy, Profiler this._perf):
+ $parent = parent, _ttl = parent._ttl, _parser = parent._parser,
+ _exceptionHandler = parent._exceptionHandler, _zone = parent._zone {
+ $root = $parent.$root;
+ $id = '_${$root._nextId++}';
+ _innerAsyncQueue = $parent._innerAsyncQueue;
+ _outerAsyncQueue = $parent._outerAsyncQueue;
- /// Do not use. Exposes internal state for testing.
- bool get hasOwnStreams => _streams != null && _streams._scope == this;
-
- Scope(Object this.context, this.rootScope, this._parentScope,
- this._readWriteGroup, this._readOnlyGroup);
-
- /**
- * A [watch] sets up a watch in the [digest] phase of the [apply] cycle.
- *
- * Use [watch] if the reaction function can cause updates to model. In your
- * controller code you will most likely use [watch].
- */
- Watch watch(expression, ReactionFn reactionFn,
- {context, FilterMap filters, bool readOnly: false}) {
- assert(isAttached);
- assert(expression != null);
- AST ast;
- Watch watch;
- ReactionFn fn = reactionFn;
- if (expression is AST) {
- ast = expression;
- } else if (expression is String) {
- if (expression.startsWith('::')) {
- expression = expression.substring(2);
- fn = (value, last) {
- if (value != null) {
- watch.remove();
- return reactionFn(value, last);
- }
- };
- } else if (expression.startsWith(':')) {
- expression = expression.substring(1);
- fn = (value, last) => value == null ? null : reactionFn(value, last);
- }
- ast = rootScope._astParser(expression, context: context, filters: filters);
+ _prevSibling = $parent._childTail;
+ if ($parent._childHead != null) {
+ $parent._childTail._nextSibling = this;
+ $parent._childTail = this;
} else {
- throw 'expressions must be String or AST got $expression.';
+ $parent._childHead = $parent._childTail = this;
}
- return watch = (readOnly ? _readOnlyGroup : _readWriteGroup).watch(ast, fn);
+ _set$Properties();
}
- dynamic eval(expression, [Map locals]) {
- assert(isAttached);
- assert(expression == null ||
- expression is String ||
- expression is Function);
- if (expression is String && expression.isNotEmpty) {
- var obj = locals == null ? context : new ScopeLocals(context, locals);
- return rootScope._parser(expression).eval(obj);
+ _autoDigestOnTurnDone() {
+ if ($root._skipAutoDigest) {
+ $root._skipAutoDigest = false;
+ } else {
+ $digest();
}
-
- assert(locals == null);
- if (expression is EvalFunction1) return expression(context);
- if (expression is EvalFunction0) return expression();
- return null;
}
- dynamic applyInZone([expression, Map locals]) =>
- rootScope._zone.run(() => apply(expression, locals));
+ _identical(a, b) =>
+ identical(a, b) ||
+ (a is String && b is String && a == b) ||
+ (a is num && b is num && a.isNaN && b.isNaN);
- dynamic apply([expression, Map locals]) {
- _assertInternalStateConsistency();
- rootScope._transitionState(null, RootScope.STATE_APPLY);
- try {
- return eval(expression, locals);
- } catch (e, s) {
- rootScope._exceptionHandler(e, s);
- } finally {
- rootScope
- .._transitionState(RootScope.STATE_APPLY, null)
- ..digest()
- ..flush();
+ containsKey(String name) {
+ for (var scope = this; scope != null; scope = scope.$parent) {
+ if (scope._properties.containsKey(name)) {
+ return true;
+ } else if(scope._isolate) {
+ break;
+ }
}
+ return false;
}
- ScopeEvent emit(String name, [data]) {
- assert(isAttached);
- return _Streams.emit(this, name, data);
+ remove(String name) => this._properties.remove(name);
+ operator []=(String name, value) => _properties[name] = value;
+ operator [](String name) {
+ for (var scope = this; scope != null; scope = scope.$parent) {
+ if (scope._properties.containsKey(name)) {
+ return scope._properties[name];
+ } else if(scope._isolate) {
+ break;
+ }
+ }
+ return null;
}
- ScopeEvent broadcast(String name, [data]) {
- assert(isAttached);
- return _Streams.broadcast(this, name, data);
- }
- ScopeStream on(String name) {
- assert(isAttached);
- return _Streams.on(this, rootScope._exceptionHandler, name);
- }
- Scope createChild(Object childContext) {
- assert(isAttached);
- var child = new Scope(childContext, rootScope, this,
- _readWriteGroup.newGroup(childContext),
- _readOnlyGroup.newGroup(childContext));
- var next = null;
- var prev = _childTail;
- child._next = next;
- child._prev = prev;
- if (prev == null) _childHead = child; else prev._next = child;
- if (next == null) _childTail = child; else next._prev = child;
- return child;
+ noSuchMethod(Invocation invocation) {
+ var name = MirrorSystem.getName(invocation.memberName);
+ if (invocation.isGetter) {
+ return this[name];
+ } else if (invocation.isSetter) {
+ var value = invocation.positionalArguments[0];
+ name = name.substring(0, name.length - 1);
+ this[name] = value;
+ return value;
+ } else {
+ if (this[name] is Function) {
+ return this[name]();
+ } else {
+ super.noSuchMethod(invocation);
+ }
+ }
}
- void destroy() {
- assert(isAttached);
- broadcast(ScopeEvent.DESTROY);
- _Streams.destroy(this);
- if (_prev == null) {
- _parentScope._childHead = _next;
- } else {
- _prev._next = _next;
- }
- if (_next == null) {
- _parentScope._childTail = _prev;
- } else {
- _next._prev = _prev;
- }
+ /**
+ * Create a new child [Scope].
+ *
+ * * [isolate] - If set to true the child scope does not inherit properties from the parent scope.
+ * This in essence creates an independent (isolated) view for the users of the scope.
+ * * [lazy] - If set to true the scope digest will only run if the scope is marked as [$dirty].
+ * This is usefull if we expect that the bindings in the scope are constant and there is no need
+ * to check them on each digest. The digest can be forced by marking it [$dirty].
+ */
+ $new({bool isolate: false, bool lazy: false}) =>
+ new Scope._child(this, isolate, lazy, _perf);
- _next = _prev = null;
+ /**
+ * *EXPERIMENTAL:* This feature is experimental. We reserve the right to change or delete it.
+ *
+ * A dissabled scope will not be part of the [$digest] cycle until it is re-enabled.
+ */
+ set $disabled(value) => this._disabled = value;
+ get $disabled => this._disabled;
- _readWriteGroup.remove();
- _readOnlyGroup.remove();
- _parentScope = null;
- _assertInternalStateConsistency();
- }
+ /**
+ * Registers a listener callback to be executed whenever the [watchExpression] changes.
+ *
+ * The watchExpression is called on every call to [$digest] and should return the value that
+ * will be watched. (Since [$digest] reruns when it detects changes the watchExpression can
+ * execute multiple times per [$digest] and should be idempotent.)
+ *
+ * The listener is called only when the value from the current [watchExpression] and the
+ * previous call to [watchExpression] are not identical (with the exception of the initial run,
+ * see below).
+ *
+ * The watch listener may change the model, which may trigger other listeners to fire. This is
+ * achieved by rerunning the watchers until no changes are detected. The rerun iteration limit
+ * is 10 to prevent an infinite loop deadlock.
+ * If you want to be notified whenever [$digest] is called, you can register a [watchExpression]
+ * function with no listener. (Since [watchExpression] can execute multiple times per [$digest]
+ * cycle when a change is detected, be prepared for multiple calls to your listener.)
+ *
+ * After a watcher is registered with the scope, the listener fn is called asynchronously
+ * (via [$evalAsync]) to initialize the watcher. In rare cases, this is undesirable because the
+ * listener is called when the result of [watchExpression] didn't change. To detect this
+ * scenario within the listener fn, you can compare the newVal and oldVal. If these two values
+ * are identical then the listener was called due to initialization.
+ *
+ * * [watchExpression] - can be any one of these: a [Function] - `(Scope scope) => ...;` or a
+ * [String] - `expression` which is compiled with [Parser] service into a function
+ * * [listener] - A [Function] `(currentValue, previousValue, Scope scope) => ...;`
+ * * [watchStr] - Used as a debbuging hint to easier identify which expression is associated with
+ * this watcher.
+ */
+ $watch(watchExpression, [Function listener, String watchStr]) {
+ if (watchStr == null) {
+ watchStr = watchExpression.toString();
- _assertInternalStateConsistency() {
- assert((() {
- rootScope._verifyStreams(null, '', []);
- return true;
- })());
+ // Keep prod fast
+ assert((() {
+ watchStr = _source(watchExpression);
+ return true;
+ })());
+ }
+ var watcher = new _Watch(_compileToFn(listener), _initWatchVal,
+ _compileToFn(watchExpression), watchStr);
+ _watchers.addLast(watcher);
+ return () => _watchers.remove(watcher);
}
- Map<bool,int> _verifyStreams(parentScope, prefix, log) {
- assert(_parentScope == parentScope);
- var counts = {};
- var typeCounts = _streams == null ? {} : _streams._typeCounts;
- var connection = _streams != null && _streams._scope == this ? '=' : '-';
- log..add(prefix)..add(hashCode)..add(connection)..add(typeCounts)..add('\n');
- if (_streams == null) {
- } else if (_streams._scope == this) {
- _streams._streams.forEach((k, ScopeStream stream){
- if (stream.subscriptions.isNotEmpty) {
- counts[k] = 1 + (counts.containsKey(k) ? counts[k] : 0);
- }
+ /**
+ * A variant of [$watch] where it watches a collection of [watchExpressios]. If any
+ * one expression in the collection changes the [listener] is executed.
+ *
+ * * [watcherExpressions] - `List<String|(Scope scope){}>`
+ * * [Listener] - `(List newValues, List previousValues, Scope scope)`
+ */
+ $watchSet(List watchExpressions, [Function listener, String watchStr]) {
+ if (watchExpressions.length == 0) return () => null;
+
+ var lastValues = new List(watchExpressions.length);
+ var currentValues = new List(watchExpressions.length);
+
+ if (watchExpressions.length == 1) {
+ // Special case size of one.
+ return $watch(watchExpressions[0], (value, oldValue, scope) {
+ currentValues[0] = value;
+ lastValues[0] = oldValue;
+ listener(currentValues, lastValues, scope);
});
}
- var childScope = _childHead;
- while(childScope != null) {
- childScope._verifyStreams(this, ' $prefix', log).forEach((k, v) {
- counts[k] = v + (counts.containsKey(k) ? counts[k] : 0);
- });
- childScope = childScope._next;
+ var deregesterFns = [];
+ var changeCount = 0;
+ for(var i = 0, ii = watchExpressions.length; i < ii; i++) {
+ deregesterFns.add($watch(watchExpressions[i], (value, oldValue, __) {
+ currentValues[i] = value;
+ lastValues[i] = oldValue;
+ changeCount++;
+ }));
}
- if (!_mapEqual(counts, typeCounts)) {
- throw 'Streams actual: $counts != bookkeeping: $typeCounts\n'
- 'Offending scope: [scope: ${this.hashCode}]\n'
- '${log.join('')}';
- }
- return counts;
+ deregesterFns.add($watch((s) => changeCount, (c, o, scope) {
+ listener(currentValues, lastValues, scope);
+ }));
+ return () {
+ for(var i = 0, ii = deregesterFns.length; i < ii; i++) {
+ deregesterFns[i]();
+ }
+ };
}
-}
-_mapEqual(Map a, Map b) => a.length == b.length &&
- a.keys.every((k) => b.containsKey(k) && a[k] == b[k]);
+ /**
+ * Shallow watches the properties of an object and fires whenever any of the properties change
+ * (for arrays, this implies watching the array items; for object maps, this implies watching
+ * the properties). If a change is detected, the listener callback is fired.
+ *
+ * The obj collection is observed via standard [$watch] operation and is examined on every call
+ * to [$digest] to see if any items have been added, removed, or moved.
+ *
+ * The listener is called whenever anything within the obj has changed. Examples include
+ * adding, removing, and moving items belonging to an object or array.
+ */
+ $watchCollection(obj, listener, [String expression, bool shallow=false]) {
+ var oldValue;
+ var newValue;
+ int changeDetected = 0;
+ Function objGetter = _compileToFn(obj);
+ List internalArray = [];
+ Map internalMap = {};
+ int oldLength = 0;
+ int newLength;
+ var key;
+ List keysToRemove = [];
+ Function detectNewKeys = (key, value) {
+ newLength++;
+ if (oldValue.containsKey(key)) {
+ if (!_identical(oldValue[key], value)) {
+ changeDetected++;
+ oldValue[key] = value;
+ }
+ } else {
+ oldLength++;
+ oldValue[key] = value;
+ changeDetected++;
+ }
+ };
+ Function findMissingKeys = (key, _) {
+ if (!newValue.containsKey(key)) {
+ oldLength--;
+ keysToRemove.add(key);
+ }
+ };
-class ScopeStats {
- bool report = true;
- final nf = new NumberFormat.decimalPattern();
+ Function removeMissingKeys = (k) => oldValue.remove(k);
- final digestFieldStopwatch = new AvgStopwatch();
- final digestEvalStopwatch = new AvgStopwatch();
- final digestProcessStopwatch = new AvgStopwatch();
- int _digestLoopNo = 0;
+ var $watchCollectionWatch;
- final flushFieldStopwatch = new AvgStopwatch();
- final flushEvalStopwatch = new AvgStopwatch();
- final flushProcessStopwatch = new AvgStopwatch();
+ if (shallow) {
+ $watchCollectionWatch = (_) {
+ newValue = objGetter(this);
+ newLength = newValue == null ? 0 : newValue.length;
+ if (newLength != oldLength) {
+ oldLength = newLength;
+ changeDetected++;
+ }
+ if (!identical(oldValue, newValue)) {
+ oldValue = newValue;
+ changeDetected++;
+ }
+ return changeDetected;
+ };
+ } else {
+ $watchCollectionWatch = (_) {
+ newValue = objGetter(this);
- ScopeStats({this.report: false}) {
- nf.maximumFractionDigits = 0;
- }
+ if (newValue is! Map && newValue is! List) {
+ if (!_identical(oldValue, newValue)) {
+ oldValue = newValue;
+ changeDetected++;
+ }
+ } else if (newValue is Iterable) {
+ if (!_identical(oldValue, internalArray)) {
+ // we are transitioning from something which was not an array into array.
+ oldValue = internalArray;
+ oldLength = oldValue.length = 0;
+ changeDetected++;
+ }
- void digestStart() {
- _digestStopwatchReset();
- _digestLoopNo = 0;
- }
+ newLength = newValue.length;
- _digestStopwatchReset() {
- digestFieldStopwatch.reset();
- digestEvalStopwatch.reset();
- digestProcessStopwatch.reset();
- }
-
- void digestLoop(int changeCount) {
- _digestLoopNo++;
- if (report) {
- print(this);
+ if (oldLength != newLength) {
+ // if lengths do not match we need to trigger change notification
+ changeDetected++;
+ oldValue.length = oldLength = newLength;
+ }
+ // copy the items to oldValue and look for changes.
+ for (var i = 0; i < newLength; i++) {
+ if (!_identical(oldValue[i], newValue.elementAt(i))) {
+ changeDetected++;
+ oldValue[i] = newValue.elementAt(i);
+ }
+ }
+ } else { // Map
+ if (!_identical(oldValue, internalMap)) {
+ // we are transitioning from something which was not an object into object.
+ oldValue = internalMap = {};
+ oldLength = 0;
+ changeDetected++;
+ }
+ // copy the items to oldValue and look for changes.
+ newLength = 0;
+ newValue.forEach(detectNewKeys);
+ if (oldLength > newLength) {
+ // we used to have more keys, need to find them and destroy them.
+ changeDetected++;
+ oldValue.forEach(findMissingKeys);
+ keysToRemove.forEach(removeMissingKeys);
+ keysToRemove.clear();
+ }
+ }
+ return changeDetected;
+ };
}
- _digestStopwatchReset();
- }
- String _stat(AvgStopwatch s) {
- return '${nf.format(s.count)}'
- ' / ${nf.format(s.elapsedMicroseconds)} us'
- ' = ${nf.format(s.ratePerMs)} #/ms';
- }
+ var $watchCollectionAction = (_, __, ___) {
+ relaxFnApply(listener, [newValue, oldValue, this]);
+ };
- void digestEnd() {
+ return this.$watch($watchCollectionWatch,
+ $watchCollectionAction,
+ expression == null ? obj : expression);
}
- toString() =>
- 'digest #$_digestLoopNo:'
- 'Field: ${_stat(digestFieldStopwatch)} '
- 'Eval: ${_stat(digestEvalStopwatch)} '
- 'Process: ${_stat(digestProcessStopwatch)}';
-}
+ /**
+ * Add this function to your code if you want to add a $digest
+ * and want to assert that the digest will be called on this turn.
+ * This method will be deleted when we are comfortable with
+ * auto-digesting scope.
+ */
+ $$verifyDigestWillRun() {
+ assert(!$root._skipAutoDigest);
+ _zone.assertInTurn();
+ }
-class RootScope extends Scope {
- static final STATE_APPLY = 'apply';
- static final STATE_DIGEST = 'digest';
- static final STATE_FLUSH = 'digest';
+ /**
+ * *EXPERIMENTAL:* This feature is experimental. We reserve the right to change or delete it.
+ *
+ * Marks a scope as dirty. If the scope is lazy (see [$new]) then the scope will be included
+ * in the next [$digest].
+ *
+ * NOTE: This has no effect for non-lazy scopes.
+ */
+ $dirty() {
+ this._disabled = false;
+ }
- final ExceptionHandler _exceptionHandler;
- final AstParser _astParser;
- final Parser _parser;
- final ScopeDigestTTL _ttl;
- final ExpressionVisitor visitor = new ExpressionVisitor(); // TODO(misko): delete me
- final NgZone _zone;
+ /**
+ * Processes all of the watchers of the current scope and its children.
+ * Because a watcher's listener can change the model, the `$digest()` operation keeps calling
+ * the watchers no further response data has changed. This means that it is possible to get
+ * into an infinite loop. This function will throw `'Maximum iteration limit exceeded.'`
+ * if the number of iterations exceeds 10.
+ *
+ * There should really be no need to call $digest() in production code since everything is
+ * handled behind the scenes with zones and object mutation events. However, in testing
+ * both $digest and [$apply] are useful to control state and simulate the scope life cycle in
+ * a step-by-step manner.
+ *
+ * Refer to [$watch], [$watchSet] or [$watchCollection] to see how to register watchers that
+ * are executed during the digest cycle.
+ */
+ $digest() {
+ try {
+ _beginPhase('\$digest');
+ _digestWhileDirtyLoop();
+ } catch (e, s) {
+ _exceptionHandler(e, s);
+ } finally {
+ _clearPhase();
+ }
+ }
- _FunctionChain _runAsyncHead, _runAsyncTail;
- _FunctionChain _domWriteHead, _domWriteTail;
- _FunctionChain _domReadHead, _domReadTail;
- final ScopeStats _scopeStats;
+ _digestWhileDirtyLoop() {
+ _digestHandleQueue('ng.innerAsync', _innerAsyncQueue);
- String _state;
+ int timerId;
+ assert((timerId = _perf.startTimer('ng.dirty_check', 0)) != false);
+ _Watch lastDirtyWatch = _digestComputeLastDirty();
+ assert(_perf.stopTimer(timerId) != false);
- RootScope(Object context, this._astParser, this._parser,
- GetterCache cacheGetter, FilterMap filterMap,
- this._exceptionHandler, this._ttl, this._zone,
- this._scopeStats)
- : super(context, null, null,
- new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), context),
- new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), context))
- {
- _zone.onTurnDone = apply;
- _zone.onError = (e, s, ls) => _exceptionHandler(e, s);
- }
+ if (lastDirtyWatch == null) {
+ _digestHandleQueue('ng.outerAsync', _outerAsyncQueue);
+ return;
+ }
- RootScope get rootScope => this;
- bool get isAttached => true;
+ List<List<String>> watchLog = [];
+ for (int iteration = 1, ttl = _ttl; iteration < ttl; iteration++) {
+ _Watch stopWatch = _digestHandleQueue('ng.innerAsync', _innerAsyncQueue)
+ ? null // Evaluating async work requires re-evaluating all watchers.
+ : lastDirtyWatch;
+ lastDirtyWatch = null;
- void digest() {
- _transitionState(null, STATE_DIGEST);
- try {
- var rootWatchGroup = (_readWriteGroup as RootWatchGroup);
+ List<String> expressionLog;
+ if (ttl - iteration <= 3) {
+ expressionLog = <String>[];
+ watchLog.add(expressionLog);
+ }
- int digestTTL = _ttl.ttl;
- const int LOG_COUNT = 3;
- List log;
- List digestLog;
- var count;
- ChangeLog changeLog;
- _scopeStats.digestStart();
- do {
- while(_runAsyncHead != null) {
- try {
- _runAsyncHead.fn();
- } catch (e, s) {
- _exceptionHandler(e, s);
- }
- _runAsyncHead = _runAsyncHead._next;
- }
+ int timerId;
+ assert((timerId = _perf.startTimer('ng.dirty_check', iteration)) != false);
+ lastDirtyWatch = _digestComputeLastDirtyUntil(stopWatch, expressionLog);
+ assert(_perf.stopTimer(timerId) != false);
- digestTTL--;
- count = rootWatchGroup.detectChanges(
- exceptionHandler: _exceptionHandler,
- changeLog: changeLog,
- fieldStopwatch: _scopeStats.digestFieldStopwatch,
- evalStopwatch: _scopeStats.digestEvalStopwatch,
- processStopwatch: _scopeStats.digestProcessStopwatch);
-
- if (digestTTL <= LOG_COUNT) {
- if (changeLog == null) {
- log = [];
- digestLog = [];
- changeLog = (e, c, p) => digestLog.add('$e: $c <= $p');
- } else {
- log.add(digestLog.join(', '));
- digestLog.clear();
- }
- }
- if (digestTTL == 0) {
- throw 'Model did not stabilize in ${_ttl.ttl} digests. '
- 'Last $LOG_COUNT iterations:\n${log.join('\n')}';
- }
- _scopeStats.digestLoop(count);
- } while (count > 0);
- } finally {
- _scopeStats.digestEnd();
- _transitionState(STATE_DIGEST, null);
+ if (lastDirtyWatch == null) {
+ _digestComputePerfCounters();
+ _digestHandleQueue('ng.outerAsync', _outerAsyncQueue);
+ return;
+ }
}
- }
- void flush() {
- _transitionState(null, STATE_FLUSH);
- var observeGroup = this._readOnlyGroup as RootWatchGroup;
- bool runObservers = true;
- try {
- do {
- while(_domWriteHead != null) {
- try {
- _domWriteHead.fn();
- } catch (e, s) {
- _exceptionHandler(e, s);
- }
- _domWriteHead = _domWriteHead._next;
- }
- if (runObservers) {
- runObservers = false;
- observeGroup.detectChanges(exceptionHandler:_exceptionHandler);
- }
- while(_domReadHead != null) {
- try {
- _domReadHead.fn();
- } catch (e, s) {
- _exceptionHandler(e, s);
- }
- _domReadHead = _domReadHead._next;
- }
- } while (_domWriteHead != null || _domReadHead != null);
- assert((() {
- var watchLog = [];
- var observeLog = [];
- (_readWriteGroup as RootWatchGroup).detectChanges(
- changeLog: (s, c, p) => watchLog.add('$s: $c <= $p'));
- (observeGroup as RootWatchGroup).detectChanges(
- changeLog: (s, c, p) => watchLog.add('$s: $c <= $p'));
- if (watchLog.isNotEmpty || observeLog.isNotEmpty) {
- throw 'Observer reaction functions should not change model. \n'
- 'These watch changes were detected: ${watchLog.join('; ')}\n'
- 'These observe changes were detected: ${observeLog.join('; ')}';
- }
- return true;
- })());
- } finally {
- _transitionState(STATE_FLUSH, null);
- }
-
+ // I've seen things you people wouldn't believe. Attack ships on fire
+ // off the shoulder of Orion. I've watched C-beams glitter in the dark
+ // near the Tannhauser Gate. All those moments will be lost in time,
+ // like tears in rain. Time to die.
+ throw '$_ttl \$digest() iterations reached. Aborting!\n'
+ 'Watchers fired in the last ${watchLog.length} iterations: '
+ '${_toJson(watchLog)}';
}
- // QUEUES
- void runAsync(fn()) {
- var chain = new _FunctionChain(fn);
- if (_runAsyncHead == null) {
- _runAsyncHead = _runAsyncTail = chain;
- } else {
- _runAsyncTail = _runAsyncTail._next = chain;
- }
- }
- void domWrite(fn()) {
- var chain = new _FunctionChain(fn);
- if (_domWriteHead == null) {
- _domWriteHead = _domWriteTail = chain;
- } else {
- _domWriteTail = _domWriteTail._next = chain;
+ bool _digestHandleQueue(String timerName, List queue) {
+ if (queue.isEmpty) {
+ return false;
}
+ do {
+ var timerId;
+ try {
+ var workFn = queue.removeAt(0);
+ assert((timerId = _perf.startTimer(timerName, _source(workFn))) != false);
+ $root.$eval(workFn);
+ } catch (e, s) {
+ _exceptionHandler(e, s);
+ } finally {
+ assert(_perf.stopTimer(timerId) != false);
+ }
+ } while (queue.isNotEmpty);
+ return true;
}
- void domRead(fn()) {
- var chain = new _FunctionChain(fn);
- if (_domReadHead == null) {
- _domReadHead = _domReadTail = chain;
- } else {
- _domReadTail = _domReadTail._next = chain;
- }
+
+ _Watch _digestComputeLastDirty() {
+ int watcherCount = 0;
+ int scopeCount = 0;
+ Scope scope = this;
+ do {
+ _WatchList watchers = scope._watchers;
+ watcherCount += watchers.length;
+ scopeCount++;
+ for (_Watch watch = watchers.head; watch != null; watch = watch.next) {
+ var last = watch.last;
+ var value = watch.get(scope);
+ if (!_identical(value, last)) {
+ return _digestHandleDirty(scope, watch, last, value, null);
+ }
+ }
+ } while ((scope = _digestComputeNextScope(scope)) != null);
+ _digestUpdatePerfCounters(watcherCount, scopeCount);
+ return null;
}
- void destroy() {}
- void _transitionState(String from, String to) {
- assert(isAttached);
- if (_state != from) throw "$_state already in progress can not enter $to.";
- _state = to;
+ _Watch _digestComputeLastDirtyUntil(_Watch stopWatch, List<String> log) {
+ int watcherCount = 0;
+ int scopeCount = 0;
+ Scope scope = this;
+ do {
+ _WatchList watchers = scope._watchers;
+ watcherCount += watchers.length;
+ scopeCount++;
+ for (_Watch watch = watchers.head; watch != null; watch = watch.next) {
+ if (identical(stopWatch, watch)) return null;
+ var last = watch.last;
+ var value = watch.get(scope);
+ if (!_identical(value, last)) {
+ return _digestHandleDirty(scope, watch, last, value, log);
+ }
+ }
+ } while ((scope = _digestComputeNextScope(scope)) != null);
+ return null;
}
-}
-/**
- * Keeps track of Streams for each Scope. When emitting events
- * we would need to walk the whole tree. Its faster if we can prune
- * the Scopes we have to visit.
- *
- * Scope with no [_ScopeStreams] has no events registered on itself or children
- *
- * We keep track of [Stream]s, and also child scope [Stream]s. To save
- * memory we use the same stream object on all of our parents if they don't
- * have one. But that means that we have to keep track if the stream belongs
- * to the node.
- *
- * Scope with [_ScopeStreams] but who's [_scope] does not match the scope
- * is only inherited
- *
- * Only [Scope] with [_ScopeStreams] who's [_scope] matches the [Scope]
- * instance is the actual scope.
- *
- * Once the [Stream] is created it can not be removed even if all listeners
- * are canceled. That is because we don't know if someone still has reference
- * to it.
- */
-class _Streams {
- final ExceptionHandler _exceptionHandler;
- /// Scope we belong to.
- final Scope _scope;
- /// [Stream]s for [_scope] only
- final _streams = new Map<String, ScopeStream>();
- /// Child [Scope] event counts.
- final Map<String, int> _typeCounts;
- _Streams(this._scope, this._exceptionHandler, _Streams inheritStreams)
- : _typeCounts = inheritStreams == null
- ? <String, int>{}
- : new Map.from(inheritStreams._typeCounts);
-
- static ScopeEvent emit(Scope scope, String name, data) {
- var event = new ScopeEvent(name, scope, data);
- var scopeCursor = scope;
- while(scopeCursor != null) {
- if (scopeCursor._streams != null &&
- scopeCursor._streams._scope == scopeCursor) {
- ScopeStream stream = scopeCursor._streams._streams[name];
- if (stream != null) {
- event._currentScope = scopeCursor;
- stream._fire(event);
- if (event.propagationStopped) return event;
- }
+ _Watch _digestHandleDirty(Scope scope, _Watch watch, last, value, List<String> log) {
+ _Watch lastDirtyWatch;
+ while (true) {
+ if (!_identical(value, last)) {
+ lastDirtyWatch = watch;
+ if (log != null) log.add(watch.exp == null ? '[unknown]' : watch.exp);
+ watch.last = value;
+ var fireTimer;
+ assert((fireTimer = _perf.startTimer('ng.fire', watch.exp)) != false);
+ watch.fn(value, identical(_initWatchVal, last) ? value : last, scope);
+ assert(_perf.stopTimer(fireTimer) != false);
}
- scopeCursor = scopeCursor._parentScope;
+ watch = watch.next;
+ while (watch == null) {
+ scope = _digestComputeNextScope(scope);
+ if (scope == null) return lastDirtyWatch;
+ watch = scope._watchers.head;
+ }
+ last = watch.last;
+ value = watch.get(scope);
}
- return event;
}
- static ScopeEvent broadcast(Scope scope, String name, data) {
- _Streams scopeStreams = scope._streams;
- var event = new ScopeEvent(name, scope, data);
- if (scopeStreams != null && scopeStreams._typeCounts.containsKey(name)) {
- var queue = new Queue()..addFirst(scopeStreams._scope);
- while (queue.isNotEmpty) {
- scope = queue.removeFirst();
- scopeStreams = scope._streams;
- assert(scopeStreams._scope == scope);
- if (scopeStreams._streams.containsKey(name)) {
- var stream = scopeStreams._streams[name];
- event._currentScope = scope;
- stream._fire(event);
- }
- // Reverse traversal so that when the queue is read it is correct order.
- var childScope = scope._childTail;
- while(childScope != null) {
- scopeStreams = childScope._streams;
- if (scopeStreams != null &&
- scopeStreams._typeCounts.containsKey(name)) {
- queue.addFirst(scopeStreams._scope);
+
+ Scope _digestComputeNextScope(Scope scope) {
+ // Insanity Warning: scope depth-first traversal
+ // yes, this code is a bit crazy, but it works and we have tests to prove it!
+ // this piece should be kept in sync with the traversal in $broadcast
+ Scope target = this;
+ Scope childHead = scope._childHead;
+ while (childHead != null && childHead._disabled) {
+ childHead = childHead._nextSibling;
+ }
+ if (childHead == null) {
+ if (scope == target) {
+ return null;
+ } else {
+ Scope next = scope._nextSibling;
+ if (next == null) {
+ while (scope != target && (next = scope._nextSibling) == null) {
+ scope = scope.$parent;
}
- childScope = childScope._prev;
}
+ return next;
}
+ } else {
+ if (childHead._lazy) childHead._disabled = true;
+ return childHead;
}
- return event;
}
- static ScopeStream on(Scope scope,
- ExceptionHandler _exceptionHandler,
- String name) {
- _forceNewScopeStream(scope, _exceptionHandler);
- return scope._streams._get(scope, name);
+
+ void _digestComputePerfCounters() {
+ int watcherCount = 0, scopeCount = 0;
+ Scope scope = this;
+ do {
+ scopeCount++;
+ watcherCount += scope._watchers.length;
+ } while ((scope = _digestComputeNextScope(scope)) != null);
+ _digestUpdatePerfCounters(watcherCount, scopeCount);
}
- static void _forceNewScopeStream(scope, _exceptionHandler) {
- _Streams streams = scope._streams;
- Scope scopeCursor = scope;
- bool splitMode = false;
- while(scopeCursor != null) {
- _Streams cursorStreams = scopeCursor._streams;
- var hasStream = cursorStreams != null;
- var hasOwnStream = hasStream && cursorStreams._scope == scopeCursor;
- if (hasOwnStream) return;
- if (!splitMode && (streams == null || (hasStream && !hasOwnStream))) {
- if (hasStream && !hasOwnStream) {
- splitMode = true;
- }
- streams = new _Streams(scopeCursor, _exceptionHandler, cursorStreams);
- }
- scopeCursor._streams = streams;
- scopeCursor = scopeCursor._parentScope;
- }
+ void _digestUpdatePerfCounters(int watcherCount, int scopeCount) {
+ _perf.counters['ng.scope.watchers'] = watcherCount;
+ _perf.counters['ng.scopes'] = scopeCount;
}
- static void destroy(Scope scope) {
- var toBeDeletedStreams = scope._streams;
- if (toBeDeletedStreams == null) return; // no streams to clean up
- var parentScope = scope._parentScope; // skip current scope as not to delete listeners
- // find the parent-most scope which still has our stream to be deleted.
- while (parentScope != null && parentScope._streams == toBeDeletedStreams) {
- parentScope._streams = null;
- parentScope = parentScope._parentScope;
- }
- // At this point scope is the parent-most scope which has its own typeCounts
- if (parentScope == null) return;
- var parentStreams = parentScope._streams;
- assert(parentStreams != toBeDeletedStreams);
- // remove typeCounts from the scope to be destroyed from the parent
- // typeCounts
- toBeDeletedStreams._typeCounts.forEach(
- (name, count) => parentStreams._addCount(name, -count));
+
+ /**
+ * Removes the current scope (and all of its children) from the parent scope. Removal implies
+ * that calls to $digest() will no longer propagate to the current scope and its children.
+ * Removal also implies that the current scope is eligible for garbage collection.
+ *
+ * The `$destroy()` operation is usually used within directives that perform transclusion on
+ * multiple child elements (like ngRepeat) which create multiple child scopes.
+ *
+ * Just before a scope is destroyed, a `$destroy` event is broadcasted on this scope. This is
+ * a great way for child scopes (such as shared directives or controllers) to detect to and
+ * perform any necessary cleanup before the scope is removed from the application.
+ *
+ * Note that, in AngularDart, there is also a `$destroy` jQuery DOM event, which can be used to
+ * clean up DOM bindings before an element is removed from the DOM.
+ */
+ $destroy() {
+ if ($root == this) return; // we can't remove the root node;
+
+ $broadcast(r'$destroy');
+
+ if ($parent._childHead == this) $parent._childHead = _nextSibling;
+ if ($parent._childTail == this) $parent._childTail = _prevSibling;
+ if (_prevSibling != null) _prevSibling._nextSibling = _nextSibling;
+ if (_nextSibling != null) _nextSibling._prevSibling = _prevSibling;
}
- async.Stream _get(Scope scope, String name) {
- assert(scope._streams == this);
- assert(scope._streams._scope == scope);
- assert(_exceptionHandler != null);
- return _streams.putIfAbsent(name, () =>
- new ScopeStream(this, _exceptionHandler, name));
+
+ /**
+ * Evaluates the expression against the current scope and returns the result. Note that, the
+ * expression data is relative to the data within the scope. Therefore an expression such as
+ * `a + b` will deference variables `a` and `b` and return a result so long as `a` and `b`
+ * exist on the scope.
+ *
+ * * [expr] - The expression that will be evaluated. This can be both a Function or a String.
+ * * [locals] - An optional Map of key/value data that will override any matching scope members
+ * for the purposes of the evaluation.
+ */
+ $eval(expr, [locals]) {
+ return relaxFnArgs(_compileToFn(expr))(locals == null ? this : new ScopeLocals(this, locals));
}
- void _addCount(String name, int amount) {
- // decrement the counters on all parent scopes
- _Streams lastStreams = null;
- var scope = _scope;
- while (scope != null) {
- if (lastStreams != scope._streams) {
- // we have a transition, need to decrement it
- lastStreams = scope._streams;
- int count = lastStreams._typeCounts[name];
- count = count == null ? amount : count + amount;
- assert(count >= 0);
- if (count == 0) {
- lastStreams._typeCounts.remove(name);
- if (_scope == scope) _streams.remove(name);
- } else {
- lastStreams._typeCounts[name] = count;
- }
- }
- scope = scope._parentScope;
+
+ /**
+ * Evaluates the expression against the current scope at a later point in time. The $evalAsync
+ * operation may not get run right away (depending if an existing digest cycle is going on) and
+ * may therefore be issued later on (by a follow-up digest cycle). Note that at least one digest
+ * cycle will be performed after the expression is evaluated. However, If triggering an additional
+ * digest cycle is not desired then this can be avoided by placing `{outsideDigest: true}` as
+ * the 2nd parameter to the function.
+ *
+ * * [expr] - The expression that will be evaluated. This can be both a Function or a String.
+ * * [outsideDigest] - Whether or not to trigger a follow-up digest after evaluation.
+ */
+ $evalAsync(expr, {outsideDigest: false}) {
+ if (outsideDigest) {
+ _outerAsyncQueue.add(expr);
+ } else {
+ _innerAsyncQueue.add(expr);
}
}
-}
-class ScopeStream extends async.Stream<ScopeEvent> {
- final ExceptionHandler _exceptionHandler;
- final _Streams _streams;
- final String _name;
- final subscriptions = <ScopeStreamSubscription>[];
- ScopeStream(this._streams, this._exceptionHandler, this._name);
-
- ScopeStreamSubscription listen(void onData(ScopeEvent event),
- { Function onError,
- void onDone(),
- bool cancelOnError }) {
- if (subscriptions.isEmpty) _streams._addCount(_name, 1);
- var subscription = new ScopeStreamSubscription(this, onData);
- subscriptions.add(subscription);
- return subscription;
+ /**
+ * Skip running a $digest at the end of this turn.
+ * The primary use case is to skip the digest in the current VM turn because
+ * you just scheduled or are otherwise certain of an impending VM turn and the
+ * digest at the end of that turn is sufficient. You should be able to answer
+ * "No" to the question "Is there any other code that is aware that this VM
+ * turn occurred and therefore expected a digest?". If your answer is "Yes",
+ * then you run the risk that the very next VM turn is not for your event and
+ * now that other code runs in that turn and sees stale values.
+ *
+ * You might call this function, for instance, from an event listener where,
+ * though the event occurred, you need to wait for another event before you can
+ * perform something meaningful. You might schedule that other event,
+ * set a flag for the handler of the other event to recognize, etc. and then
+ * call this method to skip the digest this cycle. Note that you should call
+ * this function *after* you have successfully confirmed that the expected VM
+ * turn will occur (perhaps by scheduling it) to ensure that the digest
+ * actually does take place on that turn.
+ */
+ $skipAutoDigest() {
+ _zone.assertInTurn();
+ $root._skipAutoDigest = true;
}
- void _fire(ScopeEvent event) {
- for (ScopeStreamSubscription subscription in subscriptions) {
+
+ /**
+ * Triggers a digest operation much like [$digest] does, however, also accepts an
+ * optional expression to evaluate alongside the digest operation. The result of that
+ * expression will be returned afterwards. Much like with $digest, $apply should only be
+ * used within unit tests to simulate the life cycle of a scope. See [$digest] to learn
+ * more.
+ *
+ * * [expr] - optional expression which will be evaluated after the digest is performed. See [$eval]
+ * to learn more about expressions.
+ */
+ $apply([expr]) {
+ return _zone.run(() {
+ var timerId;
try {
- subscription._onData(event);
+ assert((timerId = _perf.startTimer('ng.\$apply', _source(expr))) != false);
+ return $eval(expr);
} catch (e, s) {
_exceptionHandler(e, s);
+ } finally {
+ assert(_perf.stopTimer(timerId) != false);
}
- }
+ });
}
- void _remove(ScopeStreamSubscription subscription) {
- assert(subscription._scopeStream == this);
- if (subscriptions.remove(subscription)) {
- if (subscriptions.isEmpty) _streams._addCount(_name, -1);
- } else {
- throw new StateError('AlreadyCanceled');
+
+ /**
+ * Registers a scope-based event listener to intercept events triggered by
+ * [$broadcast] (from any parent scopes) or [$emit] (from child scopes) that
+ * match the given event name. $on accepts two arguments:
+ *
+ * * [name] - Refers to the event name that the scope will listen on.
+ * * [listener] - Refers to the callback function which is executed when the event
+ * is intercepted.
+ *
+ *
+ * When the listener function is executed, an instance of [ScopeEvent] will be passed
+ * as the first parameter to the function.
+ *
+ * Any additional parameters available within the listener callback function are those that
+ * are set by the $broadcast or $emit scope methods (which are set by the origin scope which
+ * is the scope that first triggered the scope event).
+ */
+ $on(name, listener) {
+ var namedListeners = _listeners[name];
+ if (!_listeners.containsKey(name)) {
+ _listeners[name] = namedListeners = [];
}
+ namedListeners.add(listener);
+
+ return () {
+ namedListeners.remove(listener);
+ };
}
-}
-class ScopeStreamSubscription implements async.StreamSubscription<ScopeEvent> {
- final ScopeStream _scopeStream;
- final Function _onData;
- ScopeStreamSubscription(this._scopeStream, this._onData);
- // TODO(vbe) should return a Future
- cancel() => _scopeStream._remove(this);
+ /**
+ * Triggers a scope event referenced by the [name] parameters upwards towards the root of the
+ * scope tree. If intercepted, by a parent scope containing a matching scope event listener
+ * (which is registered via the [$on] scope method), then the event listener callback function
+ * will be executed.
+ *
+ * * [name] - The scope event name that will be triggered.
+ * * [args] - An optional list of arguments that will be fed into the listener callback function
+ * for any event listeners that are registered via [$on].
+ */
+ $emit(name, [List args]) {
+ var empty = [],
+ namedListeners,
+ scope = this,
+ event = new ScopeEvent(name, this),
+ listenerArgs = [event],
+ i;
- void onData(void handleData(ScopeEvent data)) => NOT_IMPLEMENTED();
- void onError(Function handleError) => NOT_IMPLEMENTED();
- void onDone(void handleDone()) => NOT_IMPLEMENTED();
- void pause([async.Future resumeSignal]) => NOT_IMPLEMENTED();
- void resume() => NOT_IMPLEMENTED();
- bool get isPaused => NOT_IMPLEMENTED();
- async.Future asFuture([var futureValue]) => NOT_IMPLEMENTED();
-}
+ if (args != null) {
+ listenerArgs.addAll(args);
+ }
-class _FunctionChain {
- final Function fn;
- _FunctionChain _next;
+ do {
+ namedListeners = scope._listeners[name];
+ if (namedListeners != null) {
+ event.currentScope = scope;
+ i = 0;
+ for (var length = namedListeners.length; i<length; i++) {
+ try {
+ relaxFnApply(namedListeners[i], listenerArgs);
+ if (event.propagationStopped) return event;
+ } catch (e, s) {
+ _exceptionHandler(e, s);
+ }
+ }
+ }
+ //traverse upwards
+ scope = scope.$parent;
+ } while (scope != null);
- _FunctionChain(fn())
- : fn = fn
- {
- assert(fn != null);
+ return event;
}
-}
-class AstParser {
- final Parser _parser;
- int _id = 0;
- ExpressionVisitor _visitor = new ExpressionVisitor();
- AstParser(this._parser);
+ /**
+ * Triggers a scope event referenced by the [name] parameters dowards towards the leaf nodes of the
+ * scope tree. If intercepted, by a child scope containing a matching scope event listener
+ * (which is registered via the [$on] scope method), then the event listener callback function
+ * will be executed.
+ *
+ * * [name] - The scope event name that will be triggered.
+ * * [listenerArgs] - An optional list of arguments that will be fed into the listener callback function
+ * for any event listeners that are registered via [$on].
+ */
+ $broadcast(String name, [List listenerArgs]) {
+ var target = this,
+ current = target,
+ next = target,
+ event = new ScopeEvent(name, this);
- AST call(String exp, { FilterMap filters,
- bool collection:false,
- Object context:null }) {
- _visitor.filters = filters;
- AST contextRef = _visitor.contextRef;
- try {
- if (context != null) {
- _visitor.contextRef = new ConstantAST(context, '#${_id++}');
+ //down while you can, then up and next sibling or up and next sibling until back at root
+ if (listenerArgs == null) {
+ listenerArgs = [];
+ }
+ listenerArgs.insert(0, event);
+ do {
+ current = next;
+ event.currentScope = current;
+ if (current._listeners.containsKey(name)) {
+ current._listeners[name].forEach((listener) {
+ try {
+ relaxFnApply(listener, listenerArgs);
+ } catch(e, s) {
+ _exceptionHandler(e, s);
+ }
+ });
}
- var ast = _parser(exp);
- return collection ? _visitor.visitCollection(ast) : _visitor.visit(ast);
- } finally {
- _visitor.contextRef = contextRef;
- _visitor.filters = null;
- }
- }
-}
-class ExpressionVisitor implements Visitor {
- static final ContextReferenceAST scopeContextRef = new ContextReferenceAST();
- AST contextRef = scopeContextRef;
+ // Insanity Warning: scope depth-first traversal
+ // yes, this code is a bit crazy, but it works and we have tests to prove it!
+ // this piece should be kept in sync with the traversal in $broadcast
+ if (current._childHead == null) {
+ if (current == target) {
+ next = null;
+ } else {
+ next = current._nextSibling;
+ if (next == null) {
+ while(current != target && (next = current._nextSibling) == null) {
+ current = current.$parent;
+ }
+ }
+ }
+ } else {
+ next = current._childHead;
+ }
+ } while ((current = next) != null);
- AST ast;
- FilterMap filters;
+ return event;
+ }
- AST visit(Expression exp) {
- exp.accept(this);
- assert(this.ast != null);
- try {
- return ast;
- } finally {
- ast = null;
+ _beginPhase(phase) {
+ if ($root._phase != null) {
+ // TODO(deboer): Remove the []s when dartbug.com/11999 is fixed.
+ throw ['${$root._phase} already in progress'];
}
+ assert(_perf.startTimer('ng.phase.${phase}') != false);
+
+ $root._phase = phase;
}
- AST visitCollection(Expression exp) => new CollectionAST(visit(exp));
- AST _mapToAst(Expression expression) => visit(expression);
-
- List<AST> _toAst(List<Expression> expressions) =>
- expressions.map(_mapToAst).toList();
-
- void visitCallScope(CallScope exp) {
- ast = new MethodAST(contextRef, exp.name, _toAst(exp.arguments));
+ _clearPhase() {
+ assert(_perf.stopTimer('ng.phase.${$root._phase}') != false);
+ $root._phase = null;
}
- void visitCallMember(CallMember exp) {
- ast = new MethodAST(visit(exp.object), exp.name, _toAst(exp.arguments));
- }
- visitAccessScope(AccessScope exp) {
- ast = new FieldReadAST(contextRef, exp.name);
- }
- visitAccessMember(AccessMember exp) {
- ast = new FieldReadAST(visit(exp.object), exp.name);
- }
- visitBinary(Binary exp) {
- ast = new PureFunctionAST(exp.operation,
- _operationToFunction(exp.operation),
- [visit(exp.left), visit(exp.right)]);
- }
- void visitPrefix(Prefix exp) {
- ast = new PureFunctionAST(exp.operation,
- _operationToFunction(exp.operation),
- [visit(exp.expression)]);
- }
- void visitConditional(Conditional exp) {
- ast = new PureFunctionAST('?:', _operation_ternary,
- [visit(exp.condition), visit(exp.yes),
- visit(exp.no)]);
- }
- void visitAccessKeyed(AccessKeyed exp) {
- ast = new PureFunctionAST('[]', _operation_bracket,
- [visit(exp.object), visit(exp.key)]);
- }
- void visitLiteralPrimitive(LiteralPrimitive exp) {
- ast = new ConstantAST(exp.value);
- }
- void visitLiteralString(LiteralString exp) {
- ast = new ConstantAST(exp.value);
- }
- void visitLiteralArray(LiteralArray exp) {
- List<AST> items = _toAst(exp.elements);
- ast = new PureFunctionAST('[${items.join(', ')}]', new ArrayFn(), items);
- }
- void visitLiteralObject(LiteralObject exp) {
- List<String> keys = exp.keys;
- List<AST> values = _toAst(exp.values);
- assert(keys.length == values.length);
- var kv = <String>[];
- for (var i = 0; i < keys.length; i++) {
- kv.add('${keys[i]}: ${values[i]}');
+ Function _compileToFn(exp) {
+ if (exp == null) {
+ return () => null;
+ } else if (exp is String) {
+ Expression expression = _parser(exp);
+ return expression.eval;
+ } else if (exp is Function) {
+ return exp;
+ } else {
+ throw 'Expecting String or Function';
}
- ast = new PureFunctionAST('{${kv.join(', ')}}', new MapFn(keys), values);
}
+}
- void visitFilter(Filter exp) {
- Function filterFunction = filters(exp.name);
- List<AST> args = [visitCollection(exp.expression)];
- args.addAll(_toAst(exp.arguments).map((ast) => new CollectionAST(ast)));
- ast = new PureFunctionAST('|${exp.name}',
- new _FilterWrapper(filterFunction, args.length), args);
- }
+@proxy
+class ScopeLocals implements Scope, Map {
+ static wrapper(dynamic scope, Map<String, Object> locals) => new ScopeLocals(scope, locals);
- // TODO(misko): this is a corner case. Choosing not to implement for now.
- void visitCallFunction(CallFunction exp) {
- _notSupported("function's returing functions");
- }
- void visitAssign(Assign exp) {
- _notSupported('assignement');
- }
- void visitLiteral(Literal exp) {
- _notSupported('literal');
- }
- void visitExpression(Expression exp) {
- _notSupported('?');
- }
- void visitChain(Chain exp) {
- _notSupported(';');
- }
+ dynamic _scope;
+ Map<String, Object> _locals;
- void _notSupported(String name) {
- throw new StateError("Can not watch expression containing '$name'.");
- }
-}
+ ScopeLocals(this._scope, this._locals);
-Function _operationToFunction(String operation) {
- switch(operation) {
- case '!' : return _operation_negate;
- case '+' : return _operation_add;
- case '-' : return _operation_subtract;
- case '*' : return _operation_multiply;
- case '/' : return _operation_divide;
- case '~/' : return _operation_divide_int;
- case '%' : return _operation_remainder;
- case '==' : return _operation_equals;
- case '!=' : return _operation_not_equals;
- case '<' : return _operation_less_then;
- case '>' : return _operation_greater_then;
- case '<=' : return _operation_less_or_equals_then;
- case '>=' : return _operation_greater_or_equals_then;
- case '^' : return _operation_power;
- case '&' : return _operation_bitwise_and;
- case '&&' : return _operation_logical_and;
- case '||' : return _operation_logical_or;
- default: throw new StateError(operation);
- }
+ operator []=(String name, value) => _scope[name] = value;
+ operator [](String name) => (_locals.containsKey(name) ? _locals : _scope)[name];
+
+ noSuchMethod(Invocation invocation) => mirror.reflect(_scope).delegate(invocation);
}
-_operation_negate(value) => !toBool(value);
-_operation_add(left, right) => autoConvertAdd(left, right);
-_operation_subtract(left, right) => left - right;
-_operation_multiply(left, right) => left * right;
-_operation_divide(left, right) => left / right;
-_operation_divide_int(left, right) => left ~/ right;
-_operation_remainder(left, right) => left % right;
-_operation_equals(left, right) => left == right;
-_operation_not_equals(left, right) => left != right;
-_operation_less_then(left, right) => left < right;
-_operation_greater_then(left, right) => (left == null || right == null) ? false : left > right;
-_operation_less_or_equals_then(left, right) => left <= right;
-_operation_greater_or_equals_then(left, right) => left >= right;
-_operation_power(left, right) => left ^ right;
-_operation_bitwise_and(left, right) => left & right;
-// TODO(misko): these should short circuit the evaluation.
-_operation_logical_and(left, right) => toBool(left) && toBool(right);
-_operation_logical_or(left, right) => toBool(left) || toBool(right);
+class _InitWatchVal { const _InitWatchVal(); }
+const _initWatchVal = const _InitWatchVal();
-_operation_ternary(condition, yes, no) => toBool(condition) ? yes : no;
-_operation_bracket(obj, key) => obj == null ? null : obj[key];
+class _Watch {
+ final Function fn;
+ final Function get;
+ final String exp;
+ var last;
-class ArrayFn extends FunctionApply {
- // TODO(misko): figure out why do we need to make a copy?
- apply(List args) => new List.from(args);
+ _Watch previous;
+ _Watch next;
+
+ _Watch(fn, this.last, getFn, this.exp)
+ : this.fn = relaxFnArgs3(fn)
+ , this.get = relaxFnArgs1(getFn);
}
-class MapFn extends FunctionApply {
- final List<String> keys;
+class _WatchList {
+ int length = 0;
+ _Watch head;
+ _Watch tail;
- MapFn(this.keys);
+ void addLast(_Watch watch) {
+ assert(watch.previous == null);
+ assert(watch.next == null);
+ if (tail == null) {
+ tail = head = watch;
+ } else {
+ watch.previous = tail;
+ tail.next = watch;
+ tail = watch;
+ }
+ length++;
+ }
- apply(List values) {
- // TODO(misko): figure out why do we need to make a copy instead of reusing instance?
- assert(values.length == keys.length);
- return new Map.fromIterables(keys, values);
+ void remove(_Watch watch) {
+ if (watch == head) {
+ _Watch next = watch.next;
+ if (next == null) tail = null;
+ else next.previous = null;
+ head = next;
+ } else if (watch == tail) {
+ _Watch previous = watch.previous;
+ previous.next = null;
+ tail = previous;
+ } else {
+ _Watch next = watch.next;
+ _Watch previous = watch.previous;
+ previous.next = next;
+ next.previous = previous;
+ }
+ length--;
}
}
-class _FilterWrapper extends FunctionApply {
- final Function filterFn;
- final List args;
- final List<Watch> argsWatches;
- _FilterWrapper(this.filterFn, length):
- args = new List(length),
- argsWatches = new List(length);
+_toJson(obj) {
+ try {
+ return JSON.encode(obj);
+ } catch(e) {
+ var ret = "NOT-JSONABLE";
+ // Keep prod fast.
+ assert((() {
+ var mirror = reflect(obj);
+ if (mirror is ClosureMirror) {
+ // work-around dartbug.com/14130
+ try {
+ ret = mirror.function.source;
+ } on NoSuchMethodError catch (e) {
+ } on UnimplementedError catch (e) {
+ }
+ }
+ return true;
+ })());
+ return ret;
+ }
+}
- apply(List values) {
- for (var i=0; i < values.length; i++) {
- var value = values[i];
- var lastValue = args[i];
- if (!identical(value, lastValue)) {
- if (value is CollectionChangeRecord) {
- args[i] = (value as CollectionChangeRecord).iterable;
- } else {
- args[i] = value;
- }
+String _source(obj) {
+ if (obj is Function) {
+ var m = reflect(obj);
+ if (m is ClosureMirror) {
+ // work-around dartbug.com/14130
+ try {
+ return "FN: ${m.function.source}";
+ } on NoSuchMethodError catch (e) {
+ } on UnimplementedError catch (e) {
}
}
- var value = Function.apply(filterFn, args);
- if (value is Iterable) {
- // Since filters are pure we can guarantee that this well never change.
- // By wrapping in UnmodifiableListView we can hint to the dirty checker
- // and short circuit the iterator.
- value = new UnmodifiableListView(value);
- }
- return value;
}
+ return '$obj';
}
« no previous file with comments | « third_party/pkg/angular/lib/core/registry.dart ('k') | third_party/pkg/angular/lib/core/zone.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698