diff --git a/lib/change_detection/change_detection.dart b/lib/change_detection/change_detection.dart index 70fa9ce2a..71f1b97dc 100644 --- a/lib/change_detection/change_detection.dart +++ b/lib/change_detection/change_detection.dart @@ -114,19 +114,11 @@ abstract class MapChangeRecord { /// The underlying map object Map get map; - /// A list of [CollectionKeyValue]s which are in the iteration order. */ - KeyValue get mapHead; - PreviousKeyValue get previousMapHead; - /// A list of changed items. - ChangedKeyValue get changesHead; - /// A list of new added items. - AddedKeyValue get additionsHead; - /// A list of removed items - RemovedKeyValue get removalsHead; - - void forEachChange(void f(ChangedKeyValue change)); - void forEachAddition(void f(AddedKeyValue addition)); - void forEachRemoval(void f(RemovedKeyValue removal)); + void forEachItem(void f(MapKeyValue item)); + void forEachPreviousItem(void f(MapKeyValue previousItem)); + void forEachChange(void f(MapKeyValue change)); + void forEachAddition(void f(MapKeyValue addition)); + void forEachRemoval(void f(MapKeyValue removal)); } /** @@ -144,26 +136,6 @@ abstract class MapKeyValue { V get currentValue; } -abstract class KeyValue extends MapKeyValue { - KeyValue get nextKeyValue; -} - -abstract class PreviousKeyValue extends MapKeyValue { - PreviousKeyValue get previousNextKeyValue; -} - -abstract class AddedKeyValue extends MapKeyValue { - AddedKeyValue get nextAddedKeyValue; -} - -abstract class RemovedKeyValue extends MapKeyValue { - RemovedKeyValue get nextRemovedKeyValue; -} - -abstract class ChangedKeyValue extends MapKeyValue { - ChangedKeyValue get nextChangedKeyValue; -} - /** * If the [ChangeDetector] is watching an [Iterable] then the [currentValue] of * [Record] will contain an instance of [CollectionChangeRecord]. The diff --git a/lib/change_detection/dirty_checking_change_detector.dart b/lib/change_detection/dirty_checking_change_detector.dart index 05c43a7b8..ca25d8465 100644 --- a/lib/change_detection/dirty_checking_change_detector.dart +++ b/lib/change_detection/dirty_checking_change_detector.dart @@ -517,20 +517,16 @@ class DirtyCheckingRecord implements Record, WatchRecord { final Object _INITIAL_ = new Object(); class _MapChangeRecord implements MapChangeRecord { - final Map _records = new Map(); + final _records = new Map(); Map _map; - KeyValueRecord _mapHead; - KeyValueRecord _previousMapHead; - KeyValueRecord _changesHead, _changesTail; - KeyValueRecord _additionsHead, _additionsTail; - KeyValueRecord _removalsHead, _removalsTail; Map get map => _map; - KeyValue get mapHead => _mapHead; - PreviousKeyValue get previousMapHead => _previousMapHead; - ChangedKeyValue get changesHead => _changesHead; - AddedKeyValue get additionsHead => _additionsHead; - RemovedKeyValue get removalsHead => _removalsHead; + + KeyValueRecord _mapHead; + KeyValueRecord _previousMapHead; + KeyValueRecord _changesHead, _changesTail; + KeyValueRecord _additionsHead, _additionsTail; + KeyValueRecord _removalsHead, _removalsTail; get isDirty => _additionsHead != null || _changesHead != null || @@ -544,40 +540,47 @@ class _MapChangeRecord implements MapChangeRecord { int i = 0; for (record = _mapHead = _previousMapHead; record != null; - prev = record, record = record._previousNextKeyValue, ++i) { + prev = record, record = record._nextPrevious, ++i) { record._currentValue = record._previousValue; if (prev != null) { - prev._nextKeyValue = prev._previousNextKeyValue = record; + prev._next = prev._nextPrevious = record; } } - prev._nextKeyValue = null; + prev._next = null; _undoDeltas(); } - void forEachChange(void f(ChangedKeyValue change)) { - KeyValueRecord record = _changesHead; - while (record != null) { - f(record); - record = record._nextChangedKeyValue; + KeyValueRecord r; + + void forEachItem(void f(MapKeyValue change)) { + for (r = _mapHead; r != null; r = r._next) { + f(r); } } - void forEachAddition(void f(AddedKeyValue addition)){ - KeyValueRecord record = _additionsHead; - while (record != null) { - f(record); - record = record._nextAddedKeyValue; + void forEachPreviousItem(void f(MapKeyValue change)) { + for (r = _previousMapHead; r != null; r = r._nextPrevious) { + f(r); } } - void forEachRemoval(void f(RemovedKeyValue removal)){ - KeyValueRecord record = _removalsHead; - while (record != null) { - f(record); - record = record._nextRemovedKeyValue; + void forEachChange(void f(MapKeyValue change)) { + for (r = _changesHead; r != null; r = r._nextChanged) { + f(r); } } + void forEachAddition(void f(MapKeyValue addition)){ + for (r = _additionsHead; r != null; r = r._nextAdded) { + f(r); + } + } + + void forEachRemoval(void f(MapKeyValue removal)){ + for (r = _removalsHead; r != null; r = r._nextRemoved) { + f(r); + } + } bool _check(Map map) { _reset(); @@ -603,7 +606,7 @@ class _MapChangeRecord implements MapChangeRecord { } else { seqChanged = true; if (oldSeqRecord != null) { - oldSeqRecord._nextKeyValue = null; + oldSeqRecord._next = null; _removeFromSeq(lastOldSeqRecord, oldSeqRecord); _addToRemovals(oldSeqRecord); } @@ -623,12 +626,12 @@ class _MapChangeRecord implements MapChangeRecord { if (lastNewSeqRecord == null) { _mapHead = newSeqRecord; } else { - lastNewSeqRecord._nextKeyValue = newSeqRecord; + lastNewSeqRecord._next = newSeqRecord; } } lastOldSeqRecord = oldSeqRecord; lastNewSeqRecord = newSeqRecord; - oldSeqRecord = oldSeqRecord == null ? null : oldSeqRecord._nextKeyValue; + oldSeqRecord = oldSeqRecord == null ? null : oldSeqRecord._next; }); _truncate(lastOldSeqRecord, oldSeqRecord); return isDirty; @@ -639,46 +642,44 @@ class _MapChangeRecord implements MapChangeRecord { // Record the state of the mapping for a possible _revertToPreviousState() for (KeyValueRecord record = _previousMapHead = _mapHead; record != null; - record = record._nextKeyValue) { - record._previousNextKeyValue = record._nextKeyValue; + record = record._next) { + record._nextPrevious = record._next; } _undoDeltas(); } } void _undoDeltas() { - var record = _changesHead; - while (record != null) { - record._previousValue = record._currentValue; - record = record._nextChangedKeyValue; + KeyValueRecord r; + + for (r = _changesHead; r != null; r = r._nextChanged) { + r._previousValue = r._currentValue; } - record = _additionsHead; - while (record != null) { - record._previousValue = record._currentValue; - record = record._nextAddedKeyValue; + for (r = _additionsHead; r != null; r = r._nextAdded) { + r._previousValue = r._currentValue; } assert((() { - var record = _changesHead; - while (record != null) { - var nextRecord = record._nextChangedKeyValue; - record._nextChangedKeyValue = null; - record = nextRecord; + var r = _changesHead; + while (r != null) { + var nextRecord = r._nextChanged; + r._nextChanged = null; + r = nextRecord; } - record = _additionsHead; - while (record != null) { - var nextRecord = record._nextAddedKeyValue; - record._nextAddedKeyValue = null; - record = nextRecord; + r = _additionsHead; + while (r != null) { + var nextRecord = r._nextAdded; + r._nextAdded = null; + r = nextRecord; } - record = _removalsHead; - while (record != null) { - var nextRecord = record._nextRemovedKeyValue; - record._nextRemovedKeyValue = null; - record = nextRecord; + r = _removalsHead; + while (r != null) { + var nextRecord = r._nextRemoved; + r._nextRemoved = null; + r = nextRecord; } return true; @@ -693,11 +694,11 @@ class _MapChangeRecord implements MapChangeRecord { if (lastRecord == null) { _mapHead = null; } else { - lastRecord._nextKeyValue = null; + lastRecord._next = null; } - var nextRecord = record._nextKeyValue; + var nextRecord = record._next; assert((() { - record._nextKeyValue = null; + record._next = null; return true; })()); _addToRemovals(record); @@ -705,55 +706,53 @@ class _MapChangeRecord implements MapChangeRecord { record = nextRecord; } - record = _removalsHead; - while (record != null) { - record._previousValue = record._currentValue; - record._currentValue = null; - _records.remove(record.key); - record = record._nextRemovedKeyValue; + for (var r = _removalsHead; r != null; r = r._nextRemoved) { + r._previousValue = r._currentValue; + r._currentValue = null; + _records.remove(r.key); } } bool _isInRemovals(KeyValueRecord record) => record == _removalsHead || - record._nextRemovedKeyValue != null || - record._prevRemovedKeyValue != null; + record._nextRemoved != null || + record._prevRemoved != null; void _addToRemovals(KeyValueRecord record) { - assert(record._nextKeyValue == null); - assert(record._nextAddedKeyValue == null); - assert(record._nextChangedKeyValue == null); - assert(record._nextRemovedKeyValue == null); - assert(record._prevRemovedKeyValue == null); + assert(record._next == null); + assert(record._nextAdded == null); + assert(record._nextChanged == null); + assert(record._nextRemoved == null); + assert(record._prevRemoved == null); if (_removalsHead == null) { _removalsHead = _removalsTail = record; } else { - _removalsTail._nextRemovedKeyValue = record; - record._prevRemovedKeyValue = _removalsTail; + _removalsTail._nextRemoved = record; + record._prevRemoved = _removalsTail; _removalsTail = record; } } void _removeFromSeq(KeyValueRecord prev, KeyValueRecord record) { - KeyValueRecord next = record._nextKeyValue; + KeyValueRecord next = record._next; if (prev == null) { _mapHead = next; } else { - prev._nextKeyValue = next; + prev._next = next; } assert((() { - record._nextKeyValue = null; + record._next = null; return true; })()); } void _removeFromRemovals(KeyValueRecord record) { - assert(record._nextKeyValue == null); - assert(record._nextAddedKeyValue == null); - assert(record._nextChangedKeyValue == null); + assert(record._next == null); + assert(record._nextAdded == null); + assert(record._nextChanged == null); - var prev = record._prevRemovedKeyValue; - var next = record._nextRemovedKeyValue; + var prev = record._prevRemoved; + var next = record._nextRemoved; if (prev == null) { _removalsHead = next; } else { @@ -764,53 +763,53 @@ class _MapChangeRecord implements MapChangeRecord { } else { next._prevRemovedKeyValue = prev; } - record._prevRemovedKeyValue = record._nextRemovedKeyValue = null; + record._prevRemoved = record._nextRemoved = null; } void _addToAdditions(KeyValueRecord record) { - assert(record._nextKeyValue == null); - assert(record._nextAddedKeyValue == null); - assert(record._nextChangedKeyValue == null); - assert(record._nextRemovedKeyValue == null); - assert(record._prevRemovedKeyValue == null); + assert(record._next == null); + assert(record._nextAdded == null); + assert(record._nextChanged == null); + assert(record._nextRemoved == null); + assert(record._prevRemoved == null); if (_additionsHead == null) { _additionsHead = _additionsTail = record; } else { - _additionsTail._nextAddedKeyValue = record; + _additionsTail._nextAdded = record; _additionsTail = record; } } void _addToChanges(KeyValueRecord record) { - assert(record._nextAddedKeyValue == null); - assert(record._nextChangedKeyValue == null); - assert(record._nextRemovedKeyValue == null); - assert(record._prevRemovedKeyValue == null); + assert(record._nextAdded == null); + assert(record._nextChanged == null); + assert(record._nextRemoved == null); + assert(record._prevRemoved == null); if (_changesHead == null) { _changesHead = _changesTail = record; } else { - _changesTail._nextChangedKeyValue = record; + _changesTail._nextChanged = record; _changesTail = record; } } String toString() { List itemsList = [], previousList = [], changesList = [], additionsList = [], removalsList = []; - KeyValueRecord record; - for (record = _mapHead; record != null; record = record._nextKeyValue) { - itemsList.add("$record"); + KeyValueRecord r; + for (r = _mapHead; r != null; r = r._next) { + itemsList.add("$r"); } - for (record = _previousMapHead; record != null; record = record._previousNextKeyValue) { - previousList.add("$record"); + for (r = _previousMapHead; r != null; r = r._nextPrevious) { + previousList.add("$r"); } - for (record = _changesHead; record != null; record = record._nextChangedKeyValue) { - changesList.add("$record"); + for (r = _changesHead; r != null; r = r._nextChanged) { + changesList.add("$r"); } - for (record = _additionsHead; record != null; record = record._nextAddedKeyValue) { - additionsList.add("$record"); + for (r = _additionsHead; r != null; r = r._nextAdded) { + additionsList.add("$r"); } - for (record = _removalsHead; record != null; record = record._nextRemovedKeyValue) { - removalsList.add("$record"); + for (r = _removalsHead; r != null; r = r._nextRemoved) { + removalsList.add("$r"); } return """ map: ${itemsList.join(", ")} @@ -822,33 +821,26 @@ removals: ${removalsList.join(", ")} } } -class KeyValueRecord implements KeyValue, PreviousKeyValue, - AddedKeyValue, RemovedKeyValue, ChangedKeyValue { +class KeyValueRecord implements MapKeyValue { final K key; V _previousValue, _currentValue; - KeyValueRecord _nextKeyValue; - KeyValueRecord _previousNextKeyValue; - KeyValueRecord _nextAddedKeyValue; - KeyValueRecord _nextRemovedKeyValue, _prevRemovedKeyValue; - KeyValueRecord _nextChangedKeyValue; - - KeyValueRecord(this.key); - V get previousValue => _previousValue; V get currentValue => _currentValue; - KeyValue get nextKeyValue => _nextKeyValue; - PreviousKeyValue get previousNextKeyValue => _previousNextKeyValue; - AddedKeyValue get nextAddedKeyValue => _nextAddedKeyValue; - RemovedKeyValue get nextRemovedKeyValue => _nextRemovedKeyValue; - ChangedKeyValue get nextChangedKeyValue => _nextChangedKeyValue; + + KeyValueRecord _nextPrevious; + KeyValueRecord _next; + KeyValueRecord _nextAdded; + KeyValueRecord _nextRemoved, _prevRemoved; + KeyValueRecord _nextChanged; + + KeyValueRecord(this.key); String toString() => _previousValue == _currentValue ? "$key" : '$key[$_previousValue -> $_currentValue]'; } - class _CollectionChangeRecord implements CollectionChangeRecord { Iterable _iterable; int _length; @@ -887,32 +879,32 @@ class _CollectionChangeRecord implements CollectionChangeRecord { } void forEachItem(void f(CollectionChangeItem item)) { - for (var record = _itHead; record != null; record = record._next) { - f(record); + for (var r = _itHead; r != null; r = r._next) { + f(r); } } void forEachPreviousItem(void f(CollectionChangeItem previousItem)) { - for (var record = _previousItHead; record != null; record = record._nextPrevious) { - f(record); + for (var r = _previousItHead; r != null; r = r._nextPrevious) { + f(r); } } void forEachAddition(void f(CollectionChangeItem addition)){ - for (var record = _additionsHead; record != null; record = record._nextAdded) { - f(record); + for (var r = _additionsHead; r != null; r = r._nextAdded) { + f(r); } } void forEachMove(void f(CollectionChangeItem change)) { - for (var record = _movesHead; record != null; record = record._nextMoved) { - f(record); + for (var r = _movesHead; r != null; r = r._nextMoved) { + f(r); } } void forEachRemoval(void f(CollectionChangeItem removal)){ - for (var record = _removalsHead; record != null; record = record._nextRemoved) { - f(record); + for (var r = _removalsHead; r != null; r = r._nextRemoved) { + f(r); } } @@ -1252,30 +1244,30 @@ class _CollectionChangeRecord implements CollectionChangeRecord { } String toString() { - ItemRecord record; + ItemRecord r; var list = []; - for (record = _itHead; record != null; record = record._next) { - list.add(record); + for (r = _itHead; r != null; r = r._next) { + list.add(r); } var previous = []; - for (record = _previousItHead; record != null; record = record._nextPrevious) { - previous.add(record); + for (r = _previousItHead; r != null; r = r._nextPrevious) { + previous.add(r); } var additions = []; - for (record = _additionsHead; record != null; record = record._nextAdded) { - additions.add(record); + for (r = _additionsHead; r != null; r = r._nextAdded) { + additions.add(r); } var moves = []; - for (record = _movesHead; record != null; record = record._nextMoved) { - moves.add(record); + for (r = _movesHead; r != null; r = r._nextMoved) { + moves.add(r); } var removals = []; - for (record = _removalsHead; record != null; record = record._nextRemoved) { - removals.add(record); + for (r = _removalsHead; r != null; r = r._nextRemoved) { + removals.add(r); } return """ diff --git a/lib/directive/ng_class.dart b/lib/directive/ng_class.dart index 6a17d515e..c3784309c 100644 --- a/lib/directive/ng_class.dart +++ b/lib/directive/ng_class.dart @@ -230,7 +230,7 @@ abstract class _NgClassBase { if (toBool(active)) _currentSet.add(cls); }); } else { - changes.forEachChange((ChangedKeyValue kv) { + changes.forEachChange((MapKeyValue kv) { var cls = kv.key; var active = toBool(kv.currentValue); var wasActive = toBool(kv.previousValue); @@ -242,10 +242,10 @@ abstract class _NgClassBase { } } }); - changes.forEachAddition((AddedKeyValue kv) { + changes.forEachAddition((MapKeyValue kv) { if (toBool(kv.currentValue)) _currentSet.add(kv.key); }); - changes.forEachRemoval((RemovedKeyValue kv) { + changes.forEachRemoval((MapKeyValue kv) { if (toBool(kv.previousValue)) _currentSet.remove(kv.key); }); } diff --git a/test/change_detection/dirty_checking_change_detector_spec.dart b/test/change_detection/dirty_checking_change_detector_spec.dart index 06dc50baa..3ef529ae4 100644 --- a/test/change_detection/dirty_checking_change_detector_spec.dart +++ b/test/change_detection/dirty_checking_change_detector_spec.dart @@ -733,19 +733,10 @@ class ChangeMatcher extends Matcher { } abstract class _CollectionMatcher extends Matcher { - List _getList(T item, T next(T)) { + List _getList(Function it) { var result = []; - for (; item != null; item = next(item)) { + it((item) { result.add(item); - } - return result; - } - - // todo(vicb) merge with _getList() once map is refactored - List _getCollectionList(Function it) { - var result = []; - it((CollectionChangeItem i) { - result.add(i); }); return result; } @@ -821,7 +812,7 @@ class CollectionRecordMatcher extends _CollectionMatcher { } bool checkCollection(CollectionChangeRecord changeRecord, List diffs) { - List items = _getCollectionList((fn) => changeRecord.forEachItem(fn)); + List items = _getList((fn) => changeRecord.forEachItem(fn)); bool equals = _compareLists("collection", collection, items, diffs); int iterableLength = changeRecord.iterable.toList().length; if (iterableLength != items.length) { @@ -832,22 +823,22 @@ class CollectionRecordMatcher extends _CollectionMatcher { } bool checkPrevious(CollectionChangeRecord changeRecord, List diffs) { - List items = _getCollectionList((fn) => changeRecord.forEachPreviousItem(fn)); + List items = _getList((fn) => changeRecord.forEachPreviousItem(fn)); return _compareLists("previous", previous, items, diffs); } bool checkAdditions(CollectionChangeRecord changeRecord, List diffs) { - List items = _getCollectionList((fn) => changeRecord.forEachAddition(fn)); + List items = _getList((fn) => changeRecord.forEachAddition(fn)); return _compareLists("additions", additions, items, diffs); } bool checkMoves(CollectionChangeRecord changeRecord, List diffs) { - List items = _getCollectionList((fn) => changeRecord.forEachMove(fn)); + List items = _getList((fn) => changeRecord.forEachMove(fn)); return _compareLists("moves", moves, items, diffs); } bool checkRemovals(CollectionChangeRecord changeRecord, List diffs) { - List items = _getCollectionList((fn) => changeRecord.forEachRemoval(fn)); + List items = _getList((fn) => changeRecord.forEachRemoval(fn)); return _compareLists("removes", removals, items, diffs); } } @@ -893,7 +884,7 @@ class MapRecordMatcher extends _CollectionMatcher { } bool checkMap(MapChangeRecord changeRecord, List diffs) { - List items = _getList(changeRecord.mapHead, (r) => r.nextKeyValue); + List items = _getList((fn) => changeRecord.forEachItem(fn)); bool equals = _compareLists("map", map, items, diffs); int mapLength = changeRecord.map.length; if (mapLength != items.length) { @@ -904,22 +895,22 @@ class MapRecordMatcher extends _CollectionMatcher { } bool checkPrevious(MapChangeRecord changeRecord, List diffs) { - List items = _getList(changeRecord.previousMapHead, (r) => r.previousNextKeyValue); + List items = _getList((fn) => changeRecord.forEachPreviousItem(fn)); return _compareLists("previous", previous, items, diffs); } bool checkAdditions(MapChangeRecord changeRecord, List diffs) { - List items = _getList(changeRecord.additionsHead, (r) => r.nextAddedKeyValue); + List items = _getList((fn) => changeRecord.forEachAddition(fn)); return _compareLists("additions", additions, items, diffs); } bool checkChanges(MapChangeRecord changeRecord, List diffs) { - List items = _getList(changeRecord.changesHead, (r) => r.nextChangedKeyValue); + List items = _getList((fn) => changeRecord.forEachChange(fn)); return _compareLists("changes", changes, items, diffs); } bool checkRemovals(MapChangeRecord changeRecord, List diffs) { - List items = _getList(changeRecord.removalsHead, (r) => r.nextRemovedKeyValue); + List items = _getList((fn) => changeRecord.forEachRemoval(fn)); return _compareLists("removals", removals, items, diffs); } }