diff --git a/modules/change_detection/src/watch_group.js b/modules/change_detection/src/watch_group.js index 84ff7411dc..861cf73358 100644 --- a/modules/change_detection/src/watch_group.js +++ b/modules/change_detection/src/watch_group.js @@ -50,7 +50,6 @@ export class ProtoWatchGroup { if (this.headRecord !== null) { this._createRecords(watchGroup, formatters); this._setDestination(); - } return watchGroup; } @@ -88,6 +87,11 @@ export class WatchGroup { this.headEnabledRecord = null; this.tailEnabledRecord = null; this.context = null; + + this.childHead = null; + this.childTail = null; + this.next = null; + this.prev = null; } addRecord(record:Record) { @@ -149,12 +153,35 @@ export class WatchGroup { } } - insertChildGroup(newChild:WatchGroup, insertAfter:WatchGroup) { - throw 'not implemented'; + addChild(child:WatchGroup) { + if (isBlank(this.childTail)) { + this.childHead = this.childTail = child; + this._attachRecordsFromWatchGroup(child); + + } else { + this.childTail.next = child; + child.prev = this.childTail; + this.childTail = child; + this._attachRecordsFromWatchGroup(child); + } } - remove() { - throw 'not implemented'; + _attachRecordsFromWatchGroup(child:WatchGroup) { + if (isPresent(this.tailRecord)) { + if (isPresent(child.headRecord)) { + this.tailRecord.next = child.headRecord; + this.tailRecord.nextEnabled = child.headRecord; + + child.headRecord.prev = this.tailRecord; + child.headRecord.prevEnabled = this.tailRecord; + } + } else { + this.headRecord = child.headRecord; + this.headEnabledRecord = child.headEnabledRecord; + } + + this.tailRecord = child.tailRecord; + this.tailEnabledRecord = child.tailEnabledRecord; } /** @@ -172,6 +199,14 @@ export class WatchGroup { record.updateContext(context); } } + + get _tailRecordIncludingChildren():Record { + var lastGroup = this; + while (lastGroup.childTail !== null) { + lastGroup = lastGroup.childTail; + } + return lastGroup.tailRecord; + } } export class WatchGroupDispatcher { diff --git a/modules/change_detection/test/watch_group_spec.js b/modules/change_detection/test/watch_group_spec.js index b189f4904b..205d4d2fc1 100644 --- a/modules/change_detection/test/watch_group_spec.js +++ b/modules/change_detection/test/watch_group_spec.js @@ -53,6 +53,92 @@ export function main() { }); }); + describe("adding children", () => { + it("should add child watch group", () => { + var parent = new WatchGroup(null, null); + var child1 = new WatchGroup(null, null); + var child2 = new WatchGroup(null, null); + parent.addChild(child1); + parent.addChild(child2); + + expect(parent.childHead).toBe(child1); + expect(parent.childTail).toBe(child2); + + expect(child1.next).toBe(child2); + expect(child2.prev).toBe(child1); + }); + + it("should link all records", () => { + var parent = new WatchGroup(null, null); + var parentRecord = createRecord(parent); + parent.addRecord(parentRecord); + + var child = new WatchGroup(null, null); + var childRecord = createRecord(child); + child.addRecord(childRecord); + + parent.addChild(child); + + expect(parent.headRecord).toBe(parentRecord); + expect(parent.tailRecord).toBe(childRecord); + + expect(parent.headEnabledRecord).toBe(parentRecord); + expect(parent.tailEnabledRecord).toBe(childRecord); + + expect(parentRecord.next).toBe(childRecord); + expect(childRecord.prev).toBe(parentRecord); + }); + + it("should work when parent has no records", () => { + var parent = new WatchGroup(null, null); + + var child = new WatchGroup(null, null); + var childRecord = createRecord(child); + child.addRecord(childRecord); + + parent.addChild(child); + + expect(parent.headRecord).toBe(childRecord); + expect(parent.tailRecord).toBe(childRecord); + + expect(parent.headEnabledRecord).toBe(childRecord); + expect(parent.tailEnabledRecord).toBe(childRecord); + }); + + it("should work when parent has no records and first child has no records", () => { + var parent = new WatchGroup(null, null); + var firstChild = new WatchGroup(null, null); + parent.addChild(firstChild); + + var child = new WatchGroup(null, null); + var childRecord = createRecord(child); + child.addRecord(childRecord); + + parent.addChild(child); + + expect(parent.headRecord).toBe(childRecord); + expect(parent.tailRecord).toBe(childRecord); + + expect(parent.headEnabledRecord).toBe(childRecord); + expect(parent.tailEnabledRecord).toBe(childRecord); + }); + + it("should work when second child has no records", () => { + var parent = new WatchGroup(null, null); + + var firstChild = new WatchGroup(null, null); + var childRecord = createRecord(firstChild); + firstChild.addRecord(childRecord); + parent.addChild(firstChild); + + var secondChild = new WatchGroup(null, null); + parent.addChild(secondChild); + + expect(parent.childHead).toBe(firstChild); + expect(parent.childTail).toBe(secondChild); + }); + }); + describe("enabling/disabling records", () => { it("should disable a single record", () => { var wg = new WatchGroup(null, null);