From ef40c32ac24b33aa9fdf9731459cd0d9ccafbbfe Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 10 Feb 2014 16:23:20 +0100 Subject: [PATCH] style: code cleanup --- lib/change_detection/ast.dart | 12 +- lib/change_detection/change_detection.dart | 50 +++--- .../dirty_checking_change_detector.dart | 43 ++++-- lib/change_detection/prototype_map.dart | 39 +++-- lib/change_detection/watch_group.dart | 47 +++--- lib/core/scope.dart | 145 ++++++++++-------- .../dirty_checking_change_detector_spec.dart | 102 ++++++------ 7 files changed, 242 insertions(+), 196 deletions(-) diff --git a/lib/change_detection/ast.dart b/lib/change_detection/ast.dart index 088884389..ea635bad5 100644 --- a/lib/change_detection/ast.dart +++ b/lib/change_detection/ast.dart @@ -59,9 +59,9 @@ class FieldReadAST extends AST { final String name; FieldReadAST(lhs, name) - : super('$lhs.$name'), - lhs = lhs, - name = name; + : lhs = lhs, + name = name, + super('$lhs.$name'); WatchRecord<_Handler> setupWatch(WatchGroup watchGroup) => watchGroup.addFieldWatch(lhs, name, expression); @@ -111,14 +111,14 @@ class MethodAST extends AST { class CollectionAST extends AST { final AST valueAST; CollectionAST(valueAST) - : super('#collection($valueAST)'), - valueAST = valueAST; + : valueAST = valueAST, + super('#collection($valueAST)'); WatchRecord<_Handler> setupWatch(WatchGroup watchGroup) => watchGroup.addCollectionWatch(valueAST); } -_argList(List items) => items.join(', '); +String _argList(List items) => items.join(', '); /** * The name is a bit oxymoron, but it is essentially the NullObject pattern. diff --git a/lib/change_detection/change_detection.dart b/lib/change_detection/change_detection.dart index 34052671b..80c4a0369 100644 --- a/lib/change_detection/change_detection.dart +++ b/lib/change_detection/change_detection.dart @@ -13,8 +13,8 @@ abstract class ChangeDetectorGroup { * Watch a specific [field] on an [object]. * * If the [field] is: - * - _name_ - Name of the field to watch. (If the [object] is a Map then - * treat it as a key.) + * - _name_ - Name of the property to watch. (If the [object] is a Map then + * treat the name as a key.) * - _[]_ - Watch all items in an array. * - _{}_ - Watch all items in a Map. * - _._ - Watch the actual object identity. @@ -51,8 +51,8 @@ abstract class ChangeDetectorGroup { abstract class ChangeDetector extends ChangeDetectorGroup { /** * This method does the work of collecting the changes and returns them as a - * linked list of [ChangeRecord]s. The [ChangeRecord]s are to be returned in - * the same order as they were registered. + * linked list of [ChangeRecord]s. The [ChangeRecord]s are returned in the + * same order as they were registered. */ ChangeRecord collectChanges([EvalExceptionHandler exceptionHandler]); } @@ -93,7 +93,7 @@ abstract class WatchRecord extends Record { /** * Check to see if the field on the object has changed. Returns [null] if no - * change, or a [ChangeRecord] if the change has been detected. + * change, or a [ChangeRecord] if a change has been detected. */ ChangeRecord check(); @@ -111,11 +111,11 @@ abstract class ChangeRecord extends Record { } /** - * If [ChangeDetector] is watching a an [Map] then the - * [currentValue] of [Record] will contain this object. The object contains a - * summary of changes to the map since the last execution. The changes - * are reported as a list of [MapKeyValue]s which contain the current - * and previous value in the list as well as the key. + * If the [ChangeDetector] is watching a [Map] then the [currentValue] of + * [Record] will contain an instance of this object. A [MapChangeRecord] + * contains the changes to the map since the last execution. The changes are + * reported as a list of [MapKeyValue]s which contain the key as well as its + * current and previous value. */ abstract class MapChangeRecord { /// The underlying iterable object @@ -168,11 +168,11 @@ abstract class ChangedKeyValue extends MapKeyValue { /** - * If [ChangeDetector] is watching a an [Iterable] then the - * [currentValue] of [Record] will contain this object. The object contains a - * summary of changes to the collection since the last execution. The changes - * are reported as a list of [CollectionChangeItem]s which contain the current - * and previous position in the list as well as the item. + * If the [ChangeDetector] is watching an [Iterable] then the [currentValue] of + * [Record] will contain this object. The [CollectionChangeRecord] contains the + * changes to the collection since the last execution. The changes are reported + * as a list of [CollectionChangeItem]s which contain the item as well as its + * current and previous position in the list. */ abstract class CollectionChangeRecord { /** The underlying iterable object */ @@ -193,8 +193,8 @@ abstract class CollectionChangeRecord { } /** - * Each item in collection is wrapped in [CollectionChangeItem], which can track - * the [item]s [currentKey] and [previousKey] location. + * Each changed item in the collection is wrapped in a [CollectionChangeItem], + * which tracks the [item]s [currentKey] and [previousKey] location. */ abstract class CollectionChangeItem { // TODO(misko): change to since K is int. /** Previous item location in the list or [null] if addition. */ @@ -208,32 +208,32 @@ abstract class CollectionChangeItem { // TODO(misko): change to } /** - * Used to create a linked list of collection items. - * These items are always in the iteration order of the collection. + * Used to create a linked list of collection items. These items are always in + * the iteration order of the collection. */ abstract class CollectionItem extends CollectionChangeItem { CollectionItem get nextCollectionItem; } /** - * A linked list of new items added to the collection. - * These items are always in the iteration order of the collection. + * A linked list of new items added to the collection. These items are always in + * the iteration order of the collection. */ abstract class AddedItem extends CollectionChangeItem { AddedItem get nextAddedItem; } /** - * A linked list of moved items in to the collection. - * These items are always in the iteration order of the collection. + * A linked list of items moved in the collection. These items are always in + * the iteration order of the collection. */ abstract class MovedItem extends CollectionChangeItem { MovedItem get nextMovedItem; } /** - * A linked list of removed items in to the collection. - * These items are always in the iteration order of the collection. + * A linked list of items removed from the collection. These items are always + * in the iteration order of the collection. */ abstract class RemovedItem extends CollectionChangeItem { RemovedItem get nextRemovedItem; diff --git a/lib/change_detection/dirty_checking_change_detector.dart b/lib/change_detection/dirty_checking_change_detector.dart index a7c13ebcc..8e812fd99 100644 --- a/lib/change_detection/dirty_checking_change_detector.dart +++ b/lib/change_detection/dirty_checking_change_detector.dart @@ -557,11 +557,10 @@ class _MapChangeRecord implements MapChangeRecord { } } - bool _isInRemovals(KeyValueRecord record) { - return record == _removalsHead || - record._nextRemovedKeyValue != null || - record._prevRemovedKeyValue != null; - } + bool _isInRemovals(KeyValueRecord record) => + record == _removalsHead || + record._nextRemovedKeyValue != null || + record._prevRemovedKeyValue != null; void _addToRemovals(KeyValueRecord record) { assert(record._nextKeyValue == null); @@ -580,7 +579,11 @@ class _MapChangeRecord implements MapChangeRecord { void _removeFromSeq(KeyValueRecord prev, KeyValueRecord record) { KeyValueRecord next = record._nextKeyValue; - if (prev == null) _mapHead = next; else prev._nextKeyValue = next; + if (prev == null) { + _mapHead = next; + } else { + prev._nextKeyValue = next; + } assert((() { record._nextKeyValue = null; return true; @@ -594,8 +597,16 @@ class _MapChangeRecord implements MapChangeRecord { var prev = record._prevRemovedKeyValue; var next = record._nextRemovedKeyValue; - if (prev == null) _removalsHead = next; else prev._nextRemovedKeyValue = next; - if (next == null) _removalsTail = prev; else next._prevRemovedKeyValue = prev; + if (prev == null) { + _removalsHead = next; + } else { + prev._nextRemovedKeyValue = next; + } + if (next == null) { + _removalsTail = prev; + } else { + next._prevRemovedKeyValue = prev; + } record._prevRemovedKeyValue = record._nextRemovedKeyValue = null; } @@ -627,7 +638,8 @@ class _MapChangeRecord implements MapChangeRecord { } } -class KeyValueRecord implements KeyValue, AddedKeyValue, RemovedKeyValue, ChangedKeyValue { +class KeyValueRecord implements KeyValue, AddedKeyValue, + RemovedKeyValue, ChangedKeyValue { final K key; V _previousValue, _currentValue; @@ -706,7 +718,7 @@ class _CollectionChangeRecord implements CollectionChangeRecord { return false; } else if (collection is List) { List list = collection; - for(int index = 0, length = list.length; index < length; index++) { + for(int index = 0; index < list.length; index++) { var item = list[index]; if (record == null || !identical(item, record.item)) { record = mismatch(record, item, index); @@ -779,7 +791,7 @@ class _CollectionChangeRecord implements CollectionChangeRecord { * - [item] is the current item in the collection * - [index] is the position of the item in the collection */ - ItemRecord mismatch(ItemRecord record, dynamic item, int index) { + ItemRecord mismatch(ItemRecord record, item, int index) { // Guard against bogus String changes if (record != null && item is String && record.item is String && record.item == item) { @@ -1081,7 +1093,11 @@ class _DuplicateItemRecordList { var next = beforeRecord; record._prevDupRec = prev; record._nextDupRec = next; - if (prev == null) head = record; else prev._nextDupRec = record; + if (prev == null) { + head = record; + } else { + prev._nextDupRec = record; + } next._prevDupRec = record; } } @@ -1135,8 +1151,7 @@ class _DuplicateItemRecordList { * key. */ class DuplicateMap { - final Map map = - new Map(); + final map = {}; void put(ItemRecord record, [ItemRecord beforeRecord = null]) { assert(record._nextDupRec == null); diff --git a/lib/change_detection/prototype_map.dart b/lib/change_detection/prototype_map.dart index 5c0b7f298..130444184 100644 --- a/lib/change_detection/prototype_map.dart +++ b/lib/change_detection/prototype_map.dart @@ -3,22 +3,35 @@ part of angular.watch_group; class PrototypeMap implements Map { final Map prototype; final Map self = new Map(); + PrototypeMap(this.prototype); - operator []=(name, value) => self[name] = value; - operator [](name) => self.containsKey(name) ? self[name] : prototype[name]; + void operator []=(name, value) { + self[name] = value; + } + V operator [](name) => self.containsKey(name) ? self[name] : prototype[name]; - get isEmpty => self.isEmpty && prototype.isEmpty; - get isNotEmpty => self.isNotEmpty || prototype.isNotEmpty; - get keys => self.keys; - get values => self.values; - get length => self.length; + bool get isEmpty => self.isEmpty && prototype.isEmpty; + bool get isNotEmpty => self.isNotEmpty || prototype.isNotEmpty; + // todo(vbe) include prototype keys ? + Iterable get keys => self.keys; + // todo(vbe) include prototype values ? + Iterable get values => self.values; + int get length => self.length; - forEach(fn) => self.forEach(fn); - remove(key) => self.remove(key); + void forEach(fn) { + // todo(vbe) include prototype ? + self.forEach(fn); + } + V remove(key) => self.remove(key); clear() => self.clear; - containsKey(key) => self.containsKey(key); - containsValue(key) => self.containsValue(key); - addAll(map) => self.addAll(map); - putIfAbsent(key, fn) => self.putIfAbsent(key, fn); + // todo(vbe) include prototype ? + bool containsKey(key) => self.containsKey(key); + // todo(vbe) include prototype ? + bool containsValue(key) => self.containsValue(key); + void addAll(map) { + self.addAll(map); + } + // todo(vbe) include prototype ? + V putIfAbsent(key, fn) => self.putIfAbsent(key, fn); } diff --git a/lib/change_detection/watch_group.dart b/lib/change_detection/watch_group.dart index 1388b7e7d..ed6274813 100644 --- a/lib/change_detection/watch_group.dart +++ b/lib/change_detection/watch_group.dart @@ -96,8 +96,8 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList { _nextWatchGroup; final WatchGroup _parentWatchGroup; - WatchGroup._child(_parentWatchGroup, this._changeDetector, - this.context, this._cache, this._rootGroup) + WatchGroup._child(_parentWatchGroup, this._changeDetector, this.context, + this._cache, this._rootGroup) : _parentWatchGroup = _parentWatchGroup, id = '${_parentWatchGroup.id}.${_parentWatchGroup._nextChildId++}' { @@ -224,7 +224,7 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList { } WatchGroup get _childWatchGroupTail { - WatchGroup tail = this, nextTail; + var tail = this, nextTail; while ((nextTail = tail._watchGroupTail) != null) { tail = nextTail; } @@ -245,7 +245,7 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList { this, _changeDetector.newGroup(), context == null ? this.context : context, - context == null ? this._cache: new Map>(), + context == null ? this._cache: >{}, _rootGroup == null ? this : _rootGroup); _WatchGroupList._add(this, childGroup); var marker = childGroup._marker; @@ -321,7 +321,7 @@ class RootWatchGroup extends WatchGroup { RootWatchGroup(ChangeDetector changeDetector, Object context): super._root(changeDetector, context); - WatchGroup get _rootGroup => this; + RootWatchGroup get _rootGroup => this; /** * Detect changes and process the [ReactionFn]s. @@ -334,14 +334,14 @@ class RootWatchGroup extends WatchGroup { * Each step is called in sequence. ([ReactionFn]s are not called until all * previous steps are completed). */ - int detectChanges({EvalExceptionHandler exceptionHandler, ChangeLog changeLog}) { + int detectChanges({EvalExceptionHandler exceptionHandler, + ChangeLog changeLog}) { // Process the ChangeRecords from the change detector ChangeRecord<_Handler> changeRecord = - (_changeDetector as ChangeDetector<_Handler>).collectChanges(exceptionHandler); + (_changeDetector as ChangeDetector<_Handler>) + .collectChanges(exceptionHandler); while (changeRecord != null) { - if (changeLog != null) { - changeLog(changeRecord.handler.expression); - } + if (changeLog != null) changeLog(changeRecord.handler.expression); changeRecord.handler.onChange(changeRecord); changeRecord = changeRecord.nextChange; } @@ -369,7 +369,6 @@ class RootWatchGroup extends WatchGroup { count++; try { dirtyWatch.invoke(); - } catch (e, s) { if (exceptionHandler == null) rethrow; else exceptionHandler(e, s); } @@ -566,7 +565,8 @@ class _ArgHandler extends _Handler { _releaseWatch() => null; _ArgHandler(WatchGroup watchGrp, this.watchRecord, int index) - : super(watchGrp, 'arg[$index]'), index = index; + : index = index, + super(watchGrp, 'arg[$index]'); void acceptValue(object) { watchRecord.dirtyArgs = true; @@ -628,11 +628,14 @@ class _EvalWatchRecord implements WatchRecord<_Handler>, ChangeRecord<_Handler> _EvalWatchRecord(this.watchGrp, this.handler, this.fn, name, int arity) : args = new List(arity), name = name, - symbol = name == null ? null : new Symbol(name) - { - if (fn is FunctionApply) mode = _MODE_FUNCTION_APPLY_; - else if (fn is Function) mode = _MODE_FUNCTION_; - else mode = _MODE_NULL_; + symbol = name == null ? null : new Symbol(name) { + if (fn is FunctionApply) { + mode = _MODE_FUNCTION_APPLY_; + } else if (fn is Function) { + mode = _MODE_FUNCTION_; + } else { + mode = _MODE_NULL_; + } } _EvalWatchRecord.marker() @@ -673,11 +676,9 @@ class _EvalWatchRecord implements WatchRecord<_Handler>, ChangeRecord<_Handler> mode = _MODE_MAP_CLOSURE_; } else { _instanceMirror = reflect(value); - if(_hasMethod(_instanceMirror.type, symbol)) { - mode = _MODE_METHOD_; - } else { - mode = _MODE_FIELD_CLOSURE_; - } + mode = _hasMethod(_instanceMirror.type, symbol) + ? _MODE_METHOD_ + : _MODE_FIELD_CLOSURE_; } } } @@ -737,7 +738,7 @@ class _EvalWatchRecord implements WatchRecord<_Handler>, ChangeRecord<_Handler> _EvalWatchList._remove(watchGrp, this); } - toString() { + String toString() { if (mode == _MODE_MARKER_) return 'MARKER[$currentValue]'; return '${watchGrp.id}:${handler.expression}'; } diff --git a/lib/core/scope.dart b/lib/core/scope.dart index b72c8af1d..5254c5ad0 100644 --- a/lib/core/scope.dart +++ b/lib/core/scope.dart @@ -71,12 +71,12 @@ class ScopeEvent { * 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 iteration then - * an digest is stop an an exception is thrown. + * triggers watch A. If the system does not stabilize in TTL iterations then + * the digest is stopped and an exception is thrown. */ @NgInjectableService() class ScopeDigestTTL { - final num ttl; + final int ttl; ScopeDigestTTL(): ttl = 5; ScopeDigestTTL.value(this.ttl); } @@ -91,22 +91,31 @@ class ScopeLocals implements Map { ScopeLocals(this._scope, this._locals); - operator []=(String name, value) => _scope[name] = value; - operator [](String name) => (_locals.containsKey(name) ? _locals : _scope)[name]; - - get isEmpty => _scope.isEmpty && _locals.isEmpty; - get isNotEmpty => _scope.isNotEmpty || _locals.isNotEmpty; - get keys => _scope.keys; - get values => _scope.values; - get length => _scope.length; - - forEach(fn) => _scope.forEach(fn); - remove(key) => _scope.remove(key); - clear() => _scope.clear; - containsKey(key) => _scope.containsKey(key); - containsValue(key) => _scope.containsValue(key); - addAll(map) => _scope.addAll(map); - putIfAbsent(key, fn) => _scope.putIfAbsent(key, fn); + 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 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); } class Scope { @@ -131,7 +140,7 @@ class Scope { // A better way to do this is to remove the parser from the scope. Watch watchSet(List exprs, Function reactionFn) { var expr = '{{${exprs.join('}}?{{')}}}'; - List items = exprs.map(rootScope._parse).toList(); + var items = exprs.map(rootScope._parse).toList(); AST ast = new PureFunctionAST(expr, new ArrayFn(), items); return watchGroup.watch(ast, reactionFn); } @@ -157,12 +166,10 @@ class Scope { if (expression is String && expression.isNotEmpty) { var obj = locals == null ? context : new ScopeLocals(context, locals); return rootScope._parser(expression).eval(obj); - } else if (expression is EvalFunction1) { - assert(locals == null); - return expression(context); - } else if (expression is EvalFunction0) { + } else { assert(locals == null); - return expression(); + if (expression is EvalFunction1) return expression(context); + if (expression is EvalFunction0) return expression(); } } @@ -176,9 +183,10 @@ class Scope { } catch (e, s) { rootScope._exceptionHandler(e, s); } finally { - rootScope._transitionState(RootScope.STATE_APPLY, null); - rootScope.digest(); - rootScope.flush(); + rootScope + .._transitionState(RootScope.STATE_APPLY, null) + ..digest() + ..flush(); } } @@ -204,10 +212,18 @@ class Scope { } void destroy() { - var prev = this._prev; - var next = this._next; - if (prev == null) _parentScope._childHead = next; else prev._next = next; - if (next == null) _parentScope._childTail = prev; else next._prev = prev; + var prev = _prev; + var next = _next; + if (prev == null) { + _parentScope._childHead = next; + } else { + prev._next = next; + } + if (next == null) { + _parentScope._childTail = prev; + } else { + next._prev = prev; + } this._next = this._prev = null; @@ -265,15 +281,17 @@ class RootScope extends Scope { ChangeLog changeLog; do { while(_runAsyncHead != null) { - try { _runAsyncHead.fn(); } - catch (e, s) { _exceptionHandler(e, s); } + try { + _runAsyncHead.fn(); + } catch (e, s) { + _exceptionHandler(e, s); + } _runAsyncHead = _runAsyncHead._next; } digestTTL--; count = rootWatchGroup.detectChanges( - exceptionHandler: _exceptionHandler, - changeLog: changeLog); + exceptionHandler: _exceptionHandler, changeLog: changeLog); if (digestTTL <= LOG_COUNT) { if (changeLog == null) { @@ -286,7 +304,7 @@ class RootScope extends Scope { } } if (digestTTL == 0) { - throw 'Model did not stabilize in ${_ttl.ttl} digests. ' + + throw 'Model did not stabilize in ${_ttl.ttl} digests. ' 'Last $LOG_COUNT iterations:\n${log.join('\n')}'; } } while (count > 0); @@ -302,8 +320,12 @@ class RootScope extends Scope { try { do { while(_domWriteHead != null) { - try { _domWriteHead.fn(); } - catch (e, s) { _exceptionHandler(e, s); } + try { + _domWriteHead.fn(); + } + catch (e, s) { + _exceptionHandler(e, s); + } _domWriteHead = _domWriteHead._next; } if (runObservers) { @@ -311,8 +333,12 @@ class RootScope extends Scope { observeGroup.detectChanges(exceptionHandler:_exceptionHandler); } while(_domReadHead != null) { - try { _domReadHead.fn(); } - catch (e, s) { _exceptionHandler(e, s); } + try { + _domReadHead.fn(); + } + catch (e, s) { + _exceptionHandler(e, s); + } _domReadHead = _domReadHead._next; } } while (_domWriteHead != null || _domReadHead != null); @@ -362,7 +388,6 @@ class RootScope extends Scope { } } - AST _parse(expression) => visitor.visit(_parser.call(expression)); void destroy() {} @@ -384,7 +409,7 @@ class RootScope extends Scope { * 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] dose not match the scope + * 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] @@ -399,18 +424,18 @@ class _Streams { /// Scope we belong to. final Scope _scope; /// [Stream]s for [_scope] only - final Map _streams = new Map(); + final _streams = new Map(); /// Child [Scope] event counts. final Map _typeCounts; _Streams(this._scope, this._exceptionHandler, _Streams inheritStreams) : _typeCounts = inheritStreams == null - ? new Map() + ? {} : new Map.from(inheritStreams._typeCounts); static ScopeEvent emit(Scope scope, String name, data) { - ScopeEvent event = new ScopeEvent(name, scope, data); - Scope scopeCursor = scope; + var event = new ScopeEvent(name, scope, data); + var scopeCursor = scope; while(scopeCursor != null) { if (scopeCursor._streams._scope == scopeCursor) { ScopeStream stream = scopeCursor._streams._streams[name]; @@ -427,10 +452,10 @@ class _Streams { static ScopeEvent broadcast(Scope scope, String name, data) { _Streams scopeStreams = scope._streams; - ScopeEvent event = new ScopeEvent(name, scope, data); + var event = new ScopeEvent(name, scope, data); if (scopeStreams != null && scopeStreams._typeCounts.containsKey(name)) { - Queue queue = new Queue()..addFirst(scopeStreams._scope); - while(queue.isNotEmpty) { + var queue = new Queue()..addFirst(scopeStreams._scope); + while (queue.isNotEmpty) { scope = queue.removeFirst(); scopeStreams = scope._streams; assert(scopeStreams._scope == scope); @@ -442,9 +467,7 @@ class _Streams { var childScope = scope._childTail; while(childScope != null) { scopeStreams = childScope._streams; - if (scopeStreams != null) { - queue.addFirst(scopeStreams._scope); - } + if (scopeStreams != null) queue.addFirst(scopeStreams._scope); childScope = childScope._prev; } } @@ -495,7 +518,7 @@ class _Streams { void _addCount(String name, int amount) { // decrement the counters on all parent scopes _Streams lastStreams = null; - Scope scope = _scope; + var scope = _scope; while (scope != null) { if (lastStreams != scope._streams) { // we have a transition, need to decrement it @@ -518,7 +541,7 @@ class ScopeStream extends async.Stream { final ExceptionHandler _exceptionHandler; final _Streams _streams; final String _name; - final List subscriptions = []; + final subscriptions = []; ScopeStream(this._streams, this._exceptionHandler, this._name); @@ -707,7 +730,7 @@ class ExpressionVisitor implements Visitor { } } -_operationToFunction(String operation) { +Function _operationToFunction(String operation) { switch(operation) { case '!' : return _operation_negate; case '+' : return _operation_add; @@ -764,12 +787,8 @@ class MapFn extends FunctionApply { apply(List values) { // TODO(misko): figure out why do we need to make a copy instead of reusing instance? - Map map = {}; assert(values.length == keys.length); - for(var i = 0; i < keys.length; i++) { - map[keys[i]] = values[i]; - } - return map; + return new Map.fromIterables(keys, values); } } @@ -796,8 +815,8 @@ class _FilterWrapper extends FunctionApply { 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. + // By wrapping in UnmodifiableListView we can hint to the dirty checker + // and short circuit the iterator. value = new UnmodifiableListView(value); } return value; diff --git a/test/change_detection/dirty_checking_change_detector_spec.dart b/test/change_detection/dirty_checking_change_detector_spec.dart index 72a398a46..4c6509be0 100644 --- a/test/change_detection/dirty_checking_change_detector_spec.dart +++ b/test/change_detection/dirty_checking_change_detector_spec.dart @@ -26,14 +26,15 @@ main() => describe('DirtyCheckingChangeDetector', () { var user = new _User('', ''); var change; - detector.watch(user, 'first', null); - detector.watch(user, 'last', null); - detector.collectChanges(); // throw away first set + detector + ..watch(user, 'first', null) + ..watch(user, 'last', null) + ..collectChanges(); // throw away first set change = detector.collectChanges(); expect(change).toEqual(null); - user.first = 'misko'; - user.last = 'hevery'; + user..first = 'misko' + ..last = 'hevery'; change = detector.collectChanges(); expect(change.currentValue).toEqual('misko'); @@ -59,8 +60,7 @@ main() => describe('DirtyCheckingChangeDetector', () { it('should ignore NaN != NaN', () { var user = new _User(); user.age = double.NAN; - detector.watch(user, 'age', null); - detector.collectChanges(); // throw away first set + detector..watch(user, 'age', null)..collectChanges(); // throw away first set var changes = detector.collectChanges(); expect(changes).toEqual(null); @@ -121,7 +121,8 @@ main() => describe('DirtyCheckingChangeDetector', () { child2.watch(obj,'a', '2A'); obj['a'] = 1; - expect(detector.collectChanges(), toEqualChanges(['0a', '0A', '1a', '1A', '2A', '1b'])); + expect(detector.collectChanges(), + toEqualChanges(['0a', '0A', '1a', '1A', '2A', '1b'])); obj['a'] = 2; child1a.remove(); // should also remove child2 @@ -397,8 +398,7 @@ main() => describe('DirtyCheckingChangeDetector', () { it('should do basic operations', () { var k1 = 'a'; - var r1 = new ItemRecord(k1); - r1.currentKey = 1; + var r1 = new ItemRecord(k1)..currentKey = 1; map.put(r1); expect(map.get(k1, 2)).toEqual(null); expect(map.get(k1, 1)).toEqual(null); @@ -409,12 +409,9 @@ main() => describe('DirtyCheckingChangeDetector', () { it('should do basic operations on duplicate keys', () { var k1 = 'a'; - var r1 = new ItemRecord(k1); - var r2 = new ItemRecord(k1); - r1.currentKey = 1; - r2.currentKey = 2; - map.put(r1); - map.put(r2); + var r1 = new ItemRecord(k1)..currentKey = 1; + var r2 = new ItemRecord(k1)..currentKey = 2; + map..put(r1)..put(r2); expect(map.get(k1, 0)).toEqual(r1); expect(map.get(k1, 1)).toEqual(r2); expect(map.get(k1, 2)).toEqual(null); @@ -439,7 +436,7 @@ Matcher toEqualCollectionRecord({collection, additions, moves, removals}) => moves:moves, removals:removals); Matcher toEqualMapRecord({map, additions, changes, removals}) => new MapRecordMatcher(map:map, additions:additions, - changes:changes, removals:removals); + changes:changes, removals:removals); Matcher toEqualChanges(List changes) => new ChangeMatcher(changes); class ChangeMatcher extends Matcher { @@ -447,9 +444,11 @@ class ChangeMatcher extends Matcher { ChangeMatcher(this.expected); - Description describe(Description description) => description..add(expected.toString()); + Description describe(Description description) => + description..add(expected.toString()); - Description describeMismatch(changes, Description mismatchDescription, Map matchState, bool verbose) { + Description describeMismatch(changes, Description mismatchDescription, + Map matchState, bool verbose) { List list = []; while(changes != null) { list.add(changes.handler); @@ -469,14 +468,16 @@ class ChangeMatcher extends Matcher { } class CollectionRecordMatcher extends Matcher { - List collection; - List additions; - List moves; - List removals; + final List collection; + final List additions; + final List moves; + final List removals; - CollectionRecordMatcher({this.collection, this.additions, this.moves, this.removals}); + CollectionRecordMatcher({this.collection, this.additions, this.moves, + this.removals}); - Description describeMismatch(changes, Description mismatchDescription, Map matchState, bool verbose) { + Description describeMismatch(changes, Description mismatchDescription, + Map matchState, bool verbose) { List diffs = matchState['diffs']; return mismatchDescription..add(diffs.join('\n')); } @@ -496,16 +497,14 @@ class CollectionRecordMatcher extends Matcher { } bool matches(CollectionChangeRecord changeRecord, Map matchState) { - List diffs = matchState['diffs'] = []; - var equals = true; - equals = equals && checkCollection(changeRecord, diffs); - equals = equals && checkAdditions(changeRecord, diffs); - equals = equals && checkMoves(changeRecord, diffs); - equals = equals && checkRemovals(changeRecord, diffs); - return equals; + var diffs = matchState['diffs'] = []; + return checkCollection(changeRecord, diffs) && + checkAdditions(changeRecord, diffs) && + checkMoves(changeRecord, diffs) && + checkRemovals(changeRecord, diffs); } - checkCollection(CollectionChangeRecord changeRecord, List diffs) { + bool checkCollection(CollectionChangeRecord changeRecord, List diffs) { var equals = true; if (collection != null) { CollectionItem collectionItem = changeRecord.collectionHead; @@ -529,7 +528,7 @@ class CollectionRecordMatcher extends Matcher { return equals; } - checkAdditions(CollectionChangeRecord changeRecord, List diffs) { + bool checkAdditions(CollectionChangeRecord changeRecord, List diffs) { var equals = true; if (additions != null) { AddedItem addedItem = changeRecord.additionsHead; @@ -553,7 +552,7 @@ class CollectionRecordMatcher extends Matcher { return equals; } - checkMoves(CollectionChangeRecord changeRecord, List diffs) { + bool checkMoves(CollectionChangeRecord changeRecord, List diffs) { var equals = true; if (moves != null) { MovedItem movedItem = changeRecord.movesHead; @@ -577,7 +576,7 @@ class CollectionRecordMatcher extends Matcher { return equals; } - checkRemovals(CollectionChangeRecord changeRecord, List diffs) { + bool checkRemovals(CollectionChangeRecord changeRecord, List diffs) { var equals = true; if (removals != null) { RemovedItem removedItem = changeRecord.removalsHead; @@ -603,14 +602,15 @@ class CollectionRecordMatcher extends Matcher { } class MapRecordMatcher extends Matcher { - List map; - List additions; - List changes; - List removals; + final List map; + final List additions; + final List changes; + final List removals; MapRecordMatcher({this.map, this.additions, this.changes, this.removals}); - Description describeMismatch(changes, Description mismatchDescription, Map matchState, bool verbose) { + Description describeMismatch(changes, Description mismatchDescription, + Map matchState, bool verbose) { List diffs = matchState['diffs']; return mismatchDescription..add(diffs.join('\n')); } @@ -630,16 +630,14 @@ class MapRecordMatcher extends Matcher { } bool matches(MapChangeRecord changeRecord, Map matchState) { - List diffs = matchState['diffs'] = []; - var equals = true; - equals = equals && checkMap(changeRecord, diffs); - equals = equals && checkAdditions(changeRecord, diffs); - equals = equals && checkChanges(changeRecord, diffs); - equals = equals && checkRemovals(changeRecord, diffs); - return equals; + var diffs = matchState['diffs'] = []; + return checkMap(changeRecord, diffs) && + checkAdditions(changeRecord, diffs) && + checkChanges(changeRecord, diffs) && + checkRemovals(changeRecord, diffs); } - checkMap(MapChangeRecord changeRecord, List diffs) { + bool checkMap(MapChangeRecord changeRecord, List diffs) { var equals = true; if (map != null) { KeyValue mapKeyValue = changeRecord.mapHead; @@ -663,7 +661,7 @@ class MapRecordMatcher extends Matcher { return equals; } - checkAdditions(MapChangeRecord changeRecord, List diffs) { + bool checkAdditions(MapChangeRecord changeRecord, List diffs) { var equals = true; if (additions != null) { AddedKeyValue addedKeyValue = changeRecord.additionsHead; @@ -687,7 +685,7 @@ class MapRecordMatcher extends Matcher { return equals; } - checkChanges(MapChangeRecord changeRecord, List diffs) { + bool checkChanges(MapChangeRecord changeRecord, List diffs) { var equals = true; if (changes != null) { ChangedKeyValue movedKeyValue = changeRecord.changesHead; @@ -711,7 +709,7 @@ class MapRecordMatcher extends Matcher { return equals; } - checkRemovals(MapChangeRecord changeRecord, List diffs) { + bool checkRemovals(MapChangeRecord changeRecord, List diffs) { var equals = true; if (removals != null) { RemovedKeyValue removedKeyValue = changeRecord.removalsHead;