feat(Change Detection): Child watch groups

This commit is contained in:
Victor Berchet 2014-10-31 11:49:55 +01:00 committed by vsavkin
parent 7482b682d6
commit 384f0ae858
2 changed files with 126 additions and 5 deletions

View File

@ -50,7 +50,6 @@ export class ProtoWatchGroup {
if (this.headRecord !== null) { if (this.headRecord !== null) {
this._createRecords(watchGroup, formatters); this._createRecords(watchGroup, formatters);
this._setDestination(); this._setDestination();
} }
return watchGroup; return watchGroup;
} }
@ -88,6 +87,11 @@ export class WatchGroup {
this.headEnabledRecord = null; this.headEnabledRecord = null;
this.tailEnabledRecord = null; this.tailEnabledRecord = null;
this.context = null; this.context = null;
this.childHead = null;
this.childTail = null;
this.next = null;
this.prev = null;
} }
addRecord(record:Record) { addRecord(record:Record) {
@ -149,12 +153,35 @@ export class WatchGroup {
} }
} }
insertChildGroup(newChild:WatchGroup, insertAfter:WatchGroup) { addChild(child:WatchGroup) {
throw 'not implemented'; 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() { _attachRecordsFromWatchGroup(child:WatchGroup) {
throw 'not implemented'; 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); record.updateContext(context);
} }
} }
get _tailRecordIncludingChildren():Record {
var lastGroup = this;
while (lastGroup.childTail !== null) {
lastGroup = lastGroup.childTail;
}
return lastGroup.tailRecord;
}
} }
export class WatchGroupDispatcher { export class WatchGroupDispatcher {

View File

@ -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", () => { describe("enabling/disabling records", () => {
it("should disable a single record", () => { it("should disable a single record", () => {
var wg = new WatchGroup(null, null); var wg = new WatchGroup(null, null);