refactor(ChangeDetector): rename WatchGroup into RecordRange

This commit is contained in:
vsavkin 2014-11-19 15:52:01 -08:00
parent 862c6412c4
commit 2980eb5b0b
14 changed files with 411 additions and 384 deletions

View File

@ -1,19 +1,20 @@
import {ProtoWatchGroup, WatchGroup} from './watch_group'; import {ProtoRecordRange, RecordRange} from './record_range';
import {ProtoRecord, Record} from './record'; import {ProtoRecord, Record} from './record';
import {FIELD, int, isPresent} from 'facade/lang'; import {FIELD, int, isPresent} from 'facade/lang';
export * from './record'; export * from './record';
export * from './watch_group' export * from './record_range'
export class ChangeDetector { export class ChangeDetector {
@FIELD('final _rootWatchGroup:WatchGroup') @FIELD('final _rootRecordRange:RecordRange')
constructor(watchGroup:WatchGroup) { constructor(recordRange:RecordRange) {
this._rootWatchGroup = watchGroup; this._rootRecordRange = recordRange;
} }
detectChanges():int { detectChanges():int {
var count:int = 0; var count:int = 0;
for (var record = this._rootWatchGroup.findFirstEnabledRecord(); for (var record = this._rootRecordRange.findFirstEnabledRecord();
isPresent(record); isPresent(record);
record = record.nextEnabled) { record = record.nextEnabled) {
if (record.check()) { if (record.check()) {

View File

@ -1,4 +1,4 @@
import {ProtoWatchGroup, WatchGroup} from './watch_group'; import {ProtoRecordRange, RecordRange} from './record_range';
import {FIELD, isPresent, isBlank, int, StringWrapper, FunctionWrapper, BaseException} from 'facade/lang'; import {FIELD, isPresent, isBlank, int, StringWrapper, FunctionWrapper, BaseException} from 'facade/lang';
import {ListWrapper, MapWrapper} from 'facade/collection'; import {ListWrapper, MapWrapper} from 'facade/collection';
import {ClosureMap} from 'change_detection/parser/closure_map'; import {ClosureMap} from 'change_detection/parser/closure_map';
@ -17,7 +17,7 @@ export const PROTO_RECORD_PROPERTY = 'property';
* real world numbers show that it does not provide significant benefits. * real world numbers show that it does not provide significant benefits.
*/ */
export class ProtoRecord { export class ProtoRecord {
@FIELD('final watchGroup:wg.ProtoWatchGroup') @FIELD('final recordRange:ProtoRecordRange')
@FIELD('final context:Object') @FIELD('final context:Object')
@FIELD('final funcOrValue:Object') @FIELD('final funcOrValue:Object')
@FIELD('final arity:int') @FIELD('final arity:int')
@ -26,13 +26,13 @@ export class ProtoRecord {
@FIELD('next:ProtoRecord') @FIELD('next:ProtoRecord')
@FIELD('prev:ProtoRecord') @FIELD('prev:ProtoRecord')
@FIELD('recordInConstruction:Record') @FIELD('recordInConstruction:Record')
constructor(watchGroup:ProtoWatchGroup, constructor(recordRange:ProtoRecordRange,
recordType:string, recordType:string,
funcOrValue, funcOrValue,
arity:int, arity:int,
dest) { dest) {
this.watchGroup = watchGroup; this.recordRange = recordRange;
this.recordType = recordType; this.recordType = recordType;
this.funcOrValue = funcOrValue; this.funcOrValue = funcOrValue;
this.arity = arity; this.arity = arity;
@ -61,7 +61,7 @@ export class ProtoRecord {
* - Keep this object as lean as possible. (Lean in number of fields) * - Keep this object as lean as possible. (Lean in number of fields)
*/ */
export class Record { export class Record {
@FIELD('final watchGroup:WatchGroup') @FIELD('final recordRange:RecordRange')
@FIELD('final protoRecord:ProtoRecord') @FIELD('final protoRecord:ProtoRecord')
@FIELD('next:Record') @FIELD('next:Record')
@FIELD('prev:Record') @FIELD('prev:Record')
@ -86,8 +86,8 @@ export class Record {
// Otherwise it is the context used by WatchGroupDispatcher. // Otherwise it is the context used by WatchGroupDispatcher.
@FIELD('dest') @FIELD('dest')
constructor(watchGroup:WatchGroup, protoRecord:ProtoRecord, formatters:Map) { constructor(recordRange:RecordRange, protoRecord:ProtoRecord, formatters:Map) {
this.watchGroup = watchGroup; this.recordRange = recordRange;
this.protoRecord = protoRecord; this.protoRecord = protoRecord;
this.next = null; this.next = null;
@ -140,8 +140,8 @@ export class Record {
} }
} }
static createMarker(wg:WatchGroup) { static createMarker(rr:RecordRange) {
var r = new Record(wg, null, null); var r = new Record(rr, null, null);
r.disabled = true; r.disabled = true;
return r; return r;
} }
@ -166,7 +166,7 @@ export class Record {
this.dest.updateContext(this.currentValue); this.dest.updateContext(this.currentValue);
} }
} else { } else {
this.watchGroup.dispatcher.onRecordChange(this, this.protoRecord.dest); this.recordRange.dispatcher.onRecordChange(this, this.protoRecord.dest);
} }
} }
@ -183,11 +183,11 @@ export class Record {
return FunctionWrapper.apply(this.context, this.args); return FunctionWrapper.apply(this.context, this.args);
case MODE_STATE_INVOKE_PURE_FUNCTION: case MODE_STATE_INVOKE_PURE_FUNCTION:
this.watchGroup.disableRecord(this); this.recordRange.disableRecord(this);
return FunctionWrapper.apply(this.funcOrValue, this.args); return FunctionWrapper.apply(this.funcOrValue, this.args);
case MODE_STATE_CONST: case MODE_STATE_CONST:
this.watchGroup.disableRecord(this); this.recordRange.disableRecord(this);
return this.funcOrValue; return this.funcOrValue;
case MODE_STATE_MARKER: case MODE_STATE_MARKER:
@ -206,18 +206,18 @@ export class Record {
updateArg(value, position:int) { updateArg(value, position:int) {
this.args[position] = value; this.args[position] = value;
this.watchGroup.enableRecord(this); this.recordRange.enableRecord(this);
} }
updateContext(value) { updateContext(value) {
this.context = value; this.context = value;
if (! this.isMarkerRecord) { if (!this.isMarkerRecord) {
this.watchGroup.enableRecord(this); this.recordRange.enableRecord(this);
} }
} }
get isMarkerRecord() { get isMarkerRecord() {
return isBlank(this.protoRecord); return this.mode == MODE_STATE_MARKER;
} }
} }

View File

@ -6,7 +6,8 @@ import {AST, AccessMember, ImplicitReceiver, AstVisitor, LiteralPrimitive,
Binary, Formatter, MethodCall, FunctionCall, PrefixNot, Conditional, Binary, Formatter, MethodCall, FunctionCall, PrefixNot, Conditional,
LiteralArray, LiteralMap, KeyedAccess, Chain, Assignment} from './parser/ast'; LiteralArray, LiteralMap, KeyedAccess, Chain, Assignment} from './parser/ast';
export class ProtoWatchGroup {
export class ProtoRecordRange {
@FIELD('headRecord:ProtoRecord') @FIELD('headRecord:ProtoRecord')
@FIELD('tailRecord:ProtoRecord') @FIELD('tailRecord:ProtoRecord')
constructor() { constructor() {
@ -15,14 +16,14 @@ export class ProtoWatchGroup {
} }
/** /**
* Parses [ast] into [ProtoRecord]s and adds them to [ProtoWatchGroup]. * Parses [ast] into [ProtoRecord]s and adds them to [ProtoRecordRange].
* *
* @param ast The expression to watch * @param ast The expression to watch
* @param memento an opaque object which will be passed to WatchGroupDispatcher on * @param memento an opaque object which will be passed to WatchGroupDispatcher on
* detecting a change. * detecting a change.
* @param shallow Should collections be shallow watched * @param shallow Should collections be shallow watched
*/ */
watch(ast:AST, addRecordsFromAST(ast:AST,
memento, memento,
shallow = false) shallow = false)
{ {
@ -45,20 +46,20 @@ export class ProtoWatchGroup {
// TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher. // TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher.
// but @Implements is not ready yet. // but @Implements is not ready yet.
instantiate(dispatcher, formatters:Map):WatchGroup { instantiate(dispatcher, formatters:Map):RecordRange {
var watchGroup:WatchGroup = new WatchGroup(this, dispatcher); var recordRange:RecordRange = new RecordRange(this, dispatcher);
if (this.headRecord !== null) { if (this.headRecord !== null) {
this._createRecords(watchGroup, formatters); this._createRecords(recordRange, formatters);
this._setDestination(); this._setDestination();
} }
return watchGroup; return recordRange;
} }
_createRecords(watchGroup:WatchGroup, formatters:Map) { _createRecords(recordRange:RecordRange, formatters:Map) {
for (var proto = this.headRecord; proto != null; proto = proto.next) { for (var proto = this.headRecord; proto != null; proto = proto.next) {
var record = new Record(watchGroup, proto, formatters); var record = new Record(recordRange, proto, formatters);
proto.recordInConstruction = record; proto.recordInConstruction = record;
watchGroup.addRecord(record); recordRange.addRecord(record);
} }
} }
@ -72,16 +73,16 @@ export class ProtoWatchGroup {
} }
} }
export class WatchGroup { export class RecordRange {
@FIELD('final protoWatchGroup:ProtoWatchGroup') @FIELD('final protoRecordRange:ProtoRecordRange')
@FIELD('final dispatcher:WatchGroupDispatcher') @FIELD('final dispatcher:WatchGroupDispatcher')
@FIELD('final headRecord:Record') @FIELD('final headRecord:Record')
@FIELD('final tailRecord:Record') @FIELD('final tailRecord:Record')
@FIELD('final disabled:boolean') @FIELD('final disabled:boolean')
// TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher. // TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher.
// but @Implements is not ready yet. // but @Implements is not ready yet.
constructor(protoWatchGroup:ProtoWatchGroup, dispatcher) { constructor(protoRecordRange:ProtoRecordRange, dispatcher) {
this.protoWatchGroup = protoWatchGroup; this.protoRecordRange = protoRecordRange;
this.dispatcher = dispatcher; this.dispatcher = dispatcher;
this.disabled = false; this.disabled = false;
@ -89,64 +90,53 @@ export class WatchGroup {
this.headRecord = Record.createMarker(this); this.headRecord = Record.createMarker(this);
this.tailRecord = Record.createMarker(this); this.tailRecord = Record.createMarker(this);
this.headRecord.next = this.tailRecord; _glue(this.headRecord, this.tailRecord);
this.tailRecord.prev = this.headRecord;
} }
/// addRecord assumes that all records are enabled /// addRecord assumes that the record is newly created, so it is enabled.
addRecord(record:Record) { addRecord(record:Record) {
var lastRecord = this.tailRecord.prev; var lastRecord = this.tailRecord.prev;
lastRecord.next = record; _glue(lastRecord, record);
lastRecord.nextEnabled = record; _glueEnabled(lastRecord, record);
_glue(record, this.tailRecord);
record.prev = lastRecord;
record.prevEnabled = lastRecord;
record.next = this.tailRecord;
this.tailRecord.prev = record;
} }
addChild(child:WatchGroup) { addRange(child:RecordRange) {
var lastRecord = this.tailRecord.prev; var lastRecord = this.tailRecord.prev;
var lastEnabledRecord = this.findLastEnabledRecord(); var lastEnabledRecord = this.findLastEnabledRecord();
var firstEnabledChildRecord = child.findFirstEnabledRecord(); var firstEnabledChildRecord = child.findFirstEnabledRecord();
lastRecord.next = child.headRecord; _glue(lastRecord, child.headRecord);
child.headRecord.prev = lastRecord; _glue(child.tailRecord, this.tailRecord);
child.tailRecord.next = this.tailRecord;
this.tailRecord.prev = child.tailRecord;
if (isPresent(lastEnabledRecord) && isPresent(firstEnabledChildRecord)) { if (isPresent(lastEnabledRecord) && isPresent(firstEnabledChildRecord)) {
lastEnabledRecord.nextEnabled = firstEnabledChildRecord; _glueEnabled(lastEnabledRecord, firstEnabledChildRecord);
firstEnabledChildRecord.prevEnabled = lastEnabledRecord;
} }
} }
removeChild(child:WatchGroup) { removeRange(child:RecordRange) {
var firstEnabledChildRecord = child.findFirstEnabledRecord(); var firstEnabledChildRecord = child.findFirstEnabledRecord();
var lastEnabledChildRecord = child.findLastEnabledRecord(); var lastEnabledChildRecord = child.findLastEnabledRecord();
var next = child.tailRecord.next; var next = child.tailRecord.next;
var prev = child.headRecord.prev; var prev = child.headRecord.prev;
next.prev = prev; _glue(prev, next);
prev.next = next;
var nextEnabled = lastEnabledChildRecord.nextEnabled; var nextEnabled = lastEnabledChildRecord.nextEnabled;
var prevEnabled = firstEnabledChildRecord.prevEnabled; var prevEnabled = firstEnabledChildRecord.prevEnabled;
if (isPresent(nextEnabled)) nextEnabled.prev = prevEnabled; if (isPresent(nextEnabled)) nextEnabled.prevEnabled = prevEnabled;
if (isPresent(prevEnabled)) prevEnabled.next = nextEnabled; if (isPresent(prevEnabled)) prevEnabled.nextEnabled = nextEnabled;
} }
findFirstEnabledRecord() { findFirstEnabledRecord() {
return this._nextEnabled(this.headRecord); return this._nextEnabledInCurrentRange(this.headRecord);
} }
findLastEnabledRecord() { findLastEnabledRecord() {
return this._prevEnabled(this.tailRecord); return this._prevEnabledInCurrentRange(this.tailRecord);
} }
disableRecord(record:Record) { disableRecord(record:Record) {
@ -162,8 +152,8 @@ export class WatchGroup {
enableRecord(record:Record) { enableRecord(record:Record) {
if (!record.disabled) return; if (!record.disabled) return;
var prevEnabled = this._prevEnabled(record); var prevEnabled = this._prevEnabledInCurrentRange(record);
var nextEnabled = this._nextEnabled(record); var nextEnabled = this._nextEnabledInCurrentRange(record);
record.prevEnabled = prevEnabled; record.prevEnabled = prevEnabled;
record.nextEnabled = nextEnabled; record.nextEnabled = nextEnabled;
@ -174,7 +164,7 @@ export class WatchGroup {
record.disabled = false; record.disabled = false;
} }
disableGroup(child:WatchGroup) { disableRange(child:RecordRange) {
var firstEnabledChildRecord = child.findFirstEnabledRecord(); var firstEnabledChildRecord = child.findFirstEnabledRecord();
var lastEnabledChildRecord = child.findLastEnabledRecord(); var lastEnabledChildRecord = child.findLastEnabledRecord();
@ -187,31 +177,32 @@ export class WatchGroup {
child.disabled = true; child.disabled = true;
} }
enableGroup(child:WatchGroup) { enableRange(child:RecordRange) {
var prevEnabledRecord = this._prevEnabled(child.headRecord); var prevEnabledRecord = this._prevEnabledInCurrentRange(child.headRecord);
var nextEnabledRecord = this._nextEnabled(child.tailRecord); var nextEnabledRecord = this._nextEnabledInCurrentRange(child.tailRecord);
var firstEnabledChildRecord = child.findFirstEnabledRecord(); var firstEnabledChildRecord = child.findFirstEnabledRecord();
var lastEnabledChildRecord = child.findLastEnabledRecord(); var lastEnabledChildRecord = child.findLastEnabledRecord();
if (isPresent(firstEnabledChildRecord) && isPresent(prevEnabledRecord)){ if (isPresent(firstEnabledChildRecord) && isPresent(prevEnabledRecord)){
firstEnabledChildRecord.prevEnabled = prevEnabledRecord; _glueEnabled(prevEnabledRecord, firstEnabledChildRecord);
prevEnabledRecord.nextEnabled = firstEnabledChildRecord;
} }
if (isPresent(lastEnabledChildRecord) && isPresent(nextEnabledRecord)){ if (isPresent(lastEnabledChildRecord) && isPresent(nextEnabledRecord)){
lastEnabledChildRecord.nextEnabled = nextEnabledRecord; _glueEnabled(lastEnabledChildRecord, nextEnabledRecord);
nextEnabledRecord.prevEnabled = lastEnabledChildRecord;
} }
child.disabled = false; child.disabled = false;
} }
_nextEnabled(record:Record) { /// Returns the next enabled record in the current range. If no such record, returns null.
_nextEnabledInCurrentRange(record:Record) {
if (record === this.tailRecord) return null;
record = record.next; record = record.next;
while (isPresent(record) && record !== this.tailRecord && record.disabled) { while (isPresent(record) && record !== this.tailRecord && record.disabled) {
if (record.isMarkerRecord && record.watchGroup.disabled) { if (record.isMarkerRecord && record.recordRange.disabled) {
record = record.watchGroup.tailRecord.next; record = record.recordRange.tailRecord.next;
} else { } else {
record = record.next; record = record.next;
} }
@ -219,11 +210,14 @@ export class WatchGroup {
return record === this.tailRecord ? null : record; return record === this.tailRecord ? null : record;
} }
_prevEnabled(record:Record) { /// Returns the prev enabled record in the current range. If no such record, returns null.
_prevEnabledInCurrentRange(record:Record) {
if (record === this.headRecord) return null;
record = record.prev; record = record.prev;
while (isPresent(record) && record !== this.headRecord && record.disabled) { while (isPresent(record) && record !== this.headRecord && record.disabled) {
if (record.isMarkerRecord && record.watchGroup.disabled) { if (record.isMarkerRecord && record.recordRange.disabled) {
record = record.watchGroup.headRecord.prev; record = record.recordRange.headRecord.prev;
} else { } else {
record = record.prev; record = record.prev;
} }
@ -233,10 +227,10 @@ export class WatchGroup {
/** /**
* Sets the context (the object) on which the change detection expressions will * Sets the context (the object) on which the change detection expressions will
* dereference themselves on. Since the WatchGroup can be reused the context * dereference themselves on. Since the RecordRange can be reused the context
* can be re-set many times during the lifetime of the WatchGroup. * can be re-set many times during the lifetime of the RecordRange.
* *
* @param context the new context for change detection for the current WatchGroup * @param context the new context for change detection for the current RecordRange
*/ */
setContext(context) { setContext(context) {
for (var record:Record = this.headRecord; for (var record:Record = this.headRecord;
@ -248,6 +242,16 @@ export class WatchGroup {
} }
} }
function _glue(a:Record, b:Record) {
a.next = b;
b.prev = a;
}
function _glueEnabled(a:Record, b:Record) {
a.nextEnabled = b;
b.prevEnabled = a;
}
export class WatchGroupDispatcher { export class WatchGroupDispatcher {
// The record holds the previous value at the time of the call // The record holds the previous value at the time of the call
onRecordChange(record:Record, context) {} onRecordChange(record:Record, context) {}
@ -264,11 +268,11 @@ class Destination {
@IMPLEMENTS(AstVisitor) @IMPLEMENTS(AstVisitor)
class ProtoRecordCreator { class ProtoRecordCreator {
@FIELD('final protoWatchGroup:ProtoWatchGroup') @FIELD('final protoRecordRange:ProtoRecordRange')
@FIELD('headRecord:ProtoRecord') @FIELD('headRecord:ProtoRecord')
@FIELD('tailRecord:ProtoRecord') @FIELD('tailRecord:ProtoRecord')
constructor(protoWatchGroup) { constructor(protoRecordRange) {
this.protoWatchGroup = protoWatchGroup; this.protoRecordRange = protoRecordRange;
this.headRecord = null; this.headRecord = null;
this.tailRecord = null; this.tailRecord = null;
} }
@ -365,7 +369,7 @@ class ProtoRecordCreator {
} }
construct(recordType, funcOrValue, arity, dest) { construct(recordType, funcOrValue, arity, dest) {
return new ProtoRecord(this.protoWatchGroup, recordType, funcOrValue, arity, dest); return new ProtoRecord(this.protoRecordRange, recordType, funcOrValue, arity, dest);
} }
add(protoRecord:ProtoRecord) { add(protoRecord:ProtoRecord) {

View File

@ -8,8 +8,8 @@ import {ClosureMap} from 'change_detection/parser/closure_map';
import { import {
ChangeDetector, ChangeDetector,
ProtoWatchGroup, ProtoRecordRange,
WatchGroup, RecordRange,
WatchGroupDispatcher, WatchGroupDispatcher,
ProtoRecord ProtoRecord
} from 'change_detection/change_detector'; } from 'change_detection/change_detector';
@ -23,14 +23,14 @@ export function main() {
} }
function createChangeDetector(memo:string, exp:string, context = null, formatters = null) { function createChangeDetector(memo:string, exp:string, context = null, formatters = null) {
var pwg = new ProtoWatchGroup(); var prr = new ProtoRecordRange();
pwg.watch(ast(exp), memo, false); prr.addRecordsFromAST(ast(exp), memo, false);
var dispatcher = new LoggingDispatcher(); var dispatcher = new LoggingDispatcher();
var wg = pwg.instantiate(dispatcher, formatters); var rr = prr.instantiate(dispatcher, formatters);
wg.setContext(context); rr.setContext(context);
var cd = new ChangeDetector(wg); var cd = new ChangeDetector(rr);
return {"changeDetector" : cd, "dispatcher" : dispatcher}; return {"changeDetector" : cd, "dispatcher" : dispatcher};
} }

View File

@ -0,0 +1,265 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
import {List, ListWrapper, MapWrapper} from 'facade/collection';
import {isPresent} from 'facade/lang';
import {Parser} from 'change_detection/parser/parser';
import {Lexer} from 'change_detection/parser/lexer';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {
ChangeDetector,
ProtoRecordRange,
RecordRange,
WatchGroupDispatcher,
ProtoRecord
} from 'change_detection/change_detector';
import {Record} from 'change_detection/record';
export function main() {
function humanize(rr:RecordRange, names:List) {
var lookupName = (item) =>
ListWrapper.last(
ListWrapper.find(names, (pair) => pair[0] === item));
var res = [];
var record = rr.findFirstEnabledRecord();
while (isPresent(record)) {
ListWrapper.push(res, lookupName(record));
record = record.nextEnabled;
}
return res;
}
function createRecord(rr) {
return new Record(rr, new ProtoRecord(null, null, null, null, null), null);
}
describe('record range', () => {
it('should add records', () => {
var rr = new RecordRange(null, null);
var record1 = createRecord(rr);
var record2 = createRecord(rr);
rr.addRecord(record1);
rr.addRecord(record2);
expect(humanize(rr, [
[record1, 'record1'],
[record2, 'record2']
])).toEqual(['record1', 'record2']);
});
describe('adding/removing record ranges', () => {
var parent, child1, child2, child3;
var childRecord1, childRecord2, childRecord3;
var recordNames;
beforeEach(() => {
parent = new RecordRange(null, null);
child1 = new RecordRange(null, null);
childRecord1 = createRecord(child1);
child1.addRecord(childRecord1);
child2 = new RecordRange(null, null);
childRecord2 = createRecord(child2);
child2.addRecord(childRecord2);
child3 = new RecordRange(null, null);
childRecord3 = createRecord(child3);
child3.addRecord(childRecord3);
recordNames = [
[childRecord1, 'record1'],
[childRecord2, 'record2'],
[childRecord3, 'record3']
];
});
it('should add record ranges', () => {
parent.addRange(child1);
parent.addRange(child2);
expect(humanize(parent, recordNames)).toEqual(['record1', 'record2']);
});
it('should add nested record ranges', () => {
parent.addRange(child1);
child1.addRange(child2);
expect(humanize(parent, recordNames)).toEqual(['record1', 'record2']);
});
it('should remove record ranges', () => {
parent.addRange(child1);
parent.addRange(child2);
parent.removeRange(child1);
expect(humanize(parent, recordNames)).toEqual(['record2']);
parent.removeRange(child2);
expect(humanize(parent, recordNames)).toEqual([]);
});
it('should remove a record range surrounded by other ranges', () => {
parent.addRange(child1);
parent.addRange(child2);
parent.addRange(child3);
parent.removeRange(child2);
expect(humanize(parent, recordNames)).toEqual(['record1', 'record3']);
});
});
describe('enabling/disabling records', () => {
var rr;
var record1, record2, record3, record4;
var recordNames;
beforeEach(() => {
rr = new RecordRange(null, null);
record1 = createRecord(rr);
record2 = createRecord(rr);
record3 = createRecord(rr);
record4 = createRecord(rr);
recordNames = [
[record1, 'record1'],
[record2, 'record2'],
[record3, 'record3'],
[record4, 'record4']
];
});
it('should disable a single record', () => {
rr.addRecord(record1);
rr.disableRecord(record1);
expect(humanize(rr, recordNames)).toEqual([]);
});
it('should enable a single record', () => {
rr.addRecord(record1);
rr.disableRecord(record1);
rr.enableRecord(record1);
expect(humanize(rr, recordNames)).toEqual(['record1']);
});
it('should disable a record', () => {
rr.addRecord(record1);
rr.addRecord(record2);
rr.addRecord(record3);
rr.addRecord(record4);
rr.disableRecord(record2);
rr.disableRecord(record3);
expect(record2.disabled).toBeTruthy();
expect(record3.disabled).toBeTruthy();
expect(humanize(rr, recordNames)).toEqual(['record1', 'record4']);
});
it('should enable a record', () => {
rr.addRecord(record1);
rr.addRecord(record2);
rr.addRecord(record3);
rr.addRecord(record4);
rr.disableRecord(record2);
rr.disableRecord(record3);
rr.enableRecord(record2);
rr.enableRecord(record3);
expect(humanize(rr, recordNames)).toEqual(['record1', 'record2', 'record3', 'record4']);
});
});
describe('enabling/disabling record ranges', () => {
var child1, child2, child3, child4;
var record1, record2, record3, record4;
var recordNames;
beforeEach(() => {
child1 = new RecordRange(null, null);
record1 = createRecord(child1);
child1.addRecord(record1);
child2 = new RecordRange(null, null);
record2 = createRecord(child2);
child2.addRecord(record2);
child3 = new RecordRange(null, null);
record3 = createRecord(child3);
child3.addRecord(record3);
child4 = new RecordRange(null, null);
record4 = createRecord(child4);
child4.addRecord(record4);
recordNames = [
[record1, 'record1'],
[record2, 'record2'],
[record3, 'record3'],
[record4, 'record4']
];
});
it('should disable a single record range', () => {
var parent = new RecordRange(null, null);
parent.addRange(child1);
parent.disableRange(child1);
expect(humanize(parent, recordNames)).toEqual([]);
});
it('should enable a single record range', () => {
var parent = new RecordRange(null, null);
parent.addRange(child1);
parent.disableRange(child1);
parent.enableRange(child1);
expect(humanize(parent, recordNames)).toEqual(['record1']);
});
it('should disable a record range', () => {
var parent = new RecordRange(null, null);
parent.addRange(child1);
parent.addRange(child2);
parent.addRange(child3);
parent.addRange(child4);
parent.disableRange(child2);
parent.disableRange(child3);
expect(humanize(parent, recordNames)).toEqual(['record1', 'record4']);
});
it('should enable a record range', () => {
var parent = new RecordRange(null, null);
parent.addRange(child1);
parent.addRange(child2);
parent.addRange(child3);
parent.addRange(child4);
parent.disableRange(child2);
parent.disableRange(child3);
parent.enableRange(child2);
parent.enableRange(child3);
expect(humanize(parent, recordNames)).toEqual([
'record1', 'record2', 'record3', 'record4'
]);
});
});
});
}

View File

@ -1,243 +0,0 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
import {List, ListWrapper, MapWrapper} from 'facade/collection';
import {isPresent} from 'facade/lang';
import {Parser} from 'change_detection/parser/parser';
import {Lexer} from 'change_detection/parser/lexer';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {
ChangeDetector,
ProtoWatchGroup,
WatchGroup,
WatchGroupDispatcher,
ProtoRecord
} from 'change_detection/change_detector';
import {Record} from 'change_detection/record';
export function main() {
function humanize(wg:WatchGroup, names:List) {
var lookupName = (item) =>
ListWrapper.last(
ListWrapper.find(names, (pair) => pair[0] === item));
var res = [];
var record = wg.findFirstEnabledRecord();
while (isPresent(record)) {
ListWrapper.push(res, lookupName(record));
record = record.nextEnabled;
}
return res;
}
function createRecord(wg) {
return new Record(wg, new ProtoRecord(null, null, null, null, null), null);
}
describe('watch group', () => {
it("should add records", () => {
var wg = new WatchGroup(null, null);
var record1 = createRecord(wg);
var record2 = createRecord(wg);
wg.addRecord(record1);
wg.addRecord(record2);
expect(humanize(wg, [
[record1, 'record1'],
[record2, 'record2']
])).toEqual(['record1', 'record2']);
});
describe("adding/removing child groups", () => {
var parent, child1, child2;
var childRecord1, childRecord2;
var recordNames;
beforeEach(() => {
parent = new WatchGroup(null, null);
child1 = new WatchGroup(null, null);
childRecord1 = createRecord(child1);
child1.addRecord(childRecord1);
child2 = new WatchGroup(null, null);
childRecord2 = createRecord(child2);
child2.addRecord(childRecord2);
recordNames = [
[childRecord1, 'record1'],
[childRecord2, 'record2'],
];
});
it("should add child groups", () => {
parent.addChild(child1);
parent.addChild(child2);
expect(humanize(parent, recordNames)).toEqual(['record1', 'record2']);
});
it("should remove children", () => {
parent.addChild(child1);
parent.addChild(child2);
parent.removeChild(child1);
expect(humanize(parent, recordNames)).toEqual(['record2']);
parent.removeChild(child2);
expect(humanize(parent, recordNames)).toEqual([]);
});
});
describe("enabling/disabling records", () => {
var wg;
var record1, record2, record3, record4;
var recordNames;
beforeEach(() => {
wg = new WatchGroup(null, null);
record1 = createRecord(wg);
record2 = createRecord(wg);
record3 = createRecord(wg);
record4 = createRecord(wg);
recordNames = [
[record1, 'record1'],
[record2, 'record2'],
[record3, 'record3'],
[record4, 'record4']
];
});
it("should disable a single record", () => {
wg.addRecord(record1);
wg.disableRecord(record1);
expect(humanize(wg, recordNames)).toEqual([]);
});
it("should enable a single record", () => {
wg.addRecord(record1);
wg.disableRecord(record1);
wg.enableRecord(record1);
expect(humanize(wg, recordNames)).toEqual(['record1']);
});
it("should disable a record", () => {
wg.addRecord(record1);
wg.addRecord(record2);
wg.addRecord(record3);
wg.addRecord(record4);
wg.disableRecord(record2);
wg.disableRecord(record3);
expect(record2.disabled).toBeTruthy();
expect(record3.disabled).toBeTruthy();
expect(humanize(wg, recordNames)).toEqual(['record1', 'record4']);
});
it("should enable a record", () => {
wg.addRecord(record1);
wg.addRecord(record2);
wg.addRecord(record3);
wg.addRecord(record4);
wg.disableRecord(record2);
wg.disableRecord(record3);
wg.enableRecord(record2);
wg.enableRecord(record3);
expect(humanize(wg, recordNames)).toEqual(['record1', 'record2', 'record3', 'record4']);
});
});
describe("enabling/disabling child groups", () => {
var child1, child2, child3, child4;
var record1, record2, record3, record4;
var recordNames;
beforeEach(() => {
child1 = new WatchGroup(null, null);
record1 = createRecord(child1);
child1.addRecord(record1);
child2 = new WatchGroup(null, null);
record2 = createRecord(child2);
child2.addRecord(record2);
child3 = new WatchGroup(null, null);
record3 = createRecord(child3);
child3.addRecord(record3);
child4 = new WatchGroup(null, null);
record4 = createRecord(child4);
child4.addRecord(record4);
recordNames = [
[record1, 'record1'],
[record2, 'record2'],
[record3, 'record3'],
[record4, 'record4']
];
});
it("should disable a single watch group", () => {
var parent = new WatchGroup(null, null);
parent.addChild(child1);
parent.disableGroup(child1);
expect(humanize(parent, recordNames)).toEqual([]);
});
it("should enable a single watch group", () => {
var parent = new WatchGroup(null, null);
parent.addChild(child1);
parent.disableGroup(child1);
parent.enableGroup(child1);
expect(humanize(parent, recordNames)).toEqual(['record1']);
});
it("should disable a watch group", () => {
var parent = new WatchGroup(null, null);
parent.addChild(child1);
parent.addChild(child2);
parent.addChild(child3);
parent.addChild(child4);
parent.disableGroup(child2);
parent.disableGroup(child3);
expect(humanize(parent, recordNames)).toEqual(['record1', 'record4']);
});
it("should enable a watch group", () => {
var parent = new WatchGroup(null, null);
parent.addChild(child1);
parent.addChild(child2);
parent.addChild(child3);
parent.addChild(child4);
parent.disableGroup(child2);
parent.disableGroup(child3);
parent.enableGroup(child2);
parent.enableGroup(child3);
expect(humanize(parent, recordNames)).toEqual([
'record1', 'record2', 'record3', 'record4'
]);
});
});
});
}

View File

@ -7,7 +7,7 @@ import {ClosureMap} from 'change_detection/parser/closure_map';
import {Parser} from 'change_detection/parser/parser'; import {Parser} from 'change_detection/parser/parser';
import {Lexer} from 'change_detection/parser/lexer'; import {Lexer} from 'change_detection/parser/lexer';
import {ChangeDetector} from 'change_detection/change_detector'; import {ChangeDetector} from 'change_detection/change_detector';
import {WatchGroup} from 'change_detection/watch_group'; import {RecordRange} from 'change_detection/record_range';
import {TemplateLoader} from './compiler/template_loader'; import {TemplateLoader} from './compiler/template_loader';
import {Reflector} from './compiler/reflector'; import {Reflector} from './compiler/reflector';
import {AnnotatedType} from './compiler/annotated_type'; import {AnnotatedType} from './compiler/annotated_type';
@ -56,7 +56,7 @@ export function documentDependentBindings(appComponentType) {
}); });
}, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]), }, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]),
bind(appWatchGroupToken).toFactory((rootView) => rootView.watchGroup, bind(appWatchGroupToken).toFactory((rootView) => rootView.recordRange,
[appViewToken]), [appViewToken]),
bind(ChangeDetector).toFactory((appWatchGroup) => bind(ChangeDetector).toFactory((appWatchGroup) =>
new ChangeDetector(appWatchGroup), [appWatchGroupToken]) new ChangeDetector(appWatchGroup), [appWatchGroupToken])

View File

@ -4,7 +4,7 @@ import {ListWrapper, List, MapWrapper, StringMapWrapper} from 'facade/collection
import {Parser} from 'change_detection/parser/parser'; import {Parser} from 'change_detection/parser/parser';
import {ClosureMap} from 'change_detection/parser/closure_map'; import {ClosureMap} from 'change_detection/parser/closure_map';
import {ProtoWatchGroup} from 'change_detection/watch_group'; import {ProtoRecordRange} from 'change_detection/record_range';
import {Directive} from '../../annotations/directive'; import {Directive} from '../../annotations/directive';
import {Component} from '../../annotations/component'; import {Component} from '../../annotations/component';
@ -20,7 +20,7 @@ import {CompileControl} from './compile_control';
/** /**
* Creates the ElementBinders and adds watches to the * Creates the ElementBinders and adds watches to the
* ProtoWatchGroup. * ProtoRecordRange.
* *
* Fills: * Fills:
* - CompileElement#inheritedElementBinder * - CompileElement#inheritedElementBinder

View File

@ -2,7 +2,7 @@ import {isPresent, BaseException} from 'facade/lang';
import {ListWrapper, MapWrapper} from 'facade/collection'; import {ListWrapper, MapWrapper} from 'facade/collection';
import {ProtoView} from '../view'; import {ProtoView} from '../view';
import {ProtoWatchGroup} from 'change_detection/watch_group'; import {ProtoRecordRange} from 'change_detection/record_range';
import {CompileStep} from './compile_step'; import {CompileStep} from './compile_step';
import {CompileElement} from './compile_element'; import {CompileElement} from './compile_element';
@ -21,7 +21,7 @@ export class ProtoViewBuilder extends CompileStep {
process(parent:CompileElement, current:CompileElement, control:CompileControl) { process(parent:CompileElement, current:CompileElement, control:CompileControl) {
var inheritedProtoView = null; var inheritedProtoView = null;
if (current.isViewRoot) { if (current.isViewRoot) {
inheritedProtoView = new ProtoView(current.element, new ProtoWatchGroup()); inheritedProtoView = new ProtoView(current.element, new ProtoRecordRange());
if (isPresent(parent)) { if (isPresent(parent)) {
if (isPresent(parent.inheritedElementBinder.nestedProtoView)) { if (isPresent(parent.inheritedElementBinder.nestedProtoView)) {
throw new BaseException('Only one nested view per element is allowed'); throw new BaseException('Only one nested view per element is allowed');

View File

@ -1,6 +1,6 @@
import {DOM, Element, Node, Text, DocumentFragment, TemplateElement} from 'facade/dom'; import {DOM, Element, Node, Text, DocumentFragment, TemplateElement} from 'facade/dom';
import {ListWrapper, MapWrapper} from 'facade/collection'; import {ListWrapper, MapWrapper} from 'facade/collection';
import {ProtoWatchGroup, WatchGroup, WatchGroupDispatcher} from 'change_detection/watch_group'; import {ProtoRecordRange, RecordRange, WatchGroupDispatcher} from 'change_detection/record_range';
import {Record} from 'change_detection/record'; import {Record} from 'change_detection/record';
import {AST} from 'change_detection/parser/ast'; import {AST} from 'change_detection/parser/ast';
@ -25,7 +25,7 @@ export class View {
@FIELD('final elementInjectors:List<ElementInjector>') @FIELD('final elementInjectors:List<ElementInjector>')
@FIELD('final bindElements:List<Element>') @FIELD('final bindElements:List<Element>')
@FIELD('final textNodes:List<Text>') @FIELD('final textNodes:List<Text>')
@FIELD('final watchGroup:WatchGroup') @FIELD('final recordRange:RecordRange')
/// When the view is part of render tree, the DocumentFragment is empty, which is why we need /// When the view is part of render tree, the DocumentFragment is empty, which is why we need
/// to keep track of the nodes. /// to keep track of the nodes.
@FIELD('final nodes:List<Node>') @FIELD('final nodes:List<Node>')
@ -33,15 +33,15 @@ export class View {
@FIELD('childViews: List<View>') @FIELD('childViews: List<View>')
constructor(nodes:List<Node>, elementInjectors:List, constructor(nodes:List<Node>, elementInjectors:List,
rootElementInjectors:List, textNodes:List, bindElements:List, rootElementInjectors:List, textNodes:List, bindElements:List,
protoWatchGroup:ProtoWatchGroup, context) { protoRecordRange:ProtoRecordRange, context) {
this.nodes = nodes; this.nodes = nodes;
this.elementInjectors = elementInjectors; this.elementInjectors = elementInjectors;
this.rootElementInjectors = rootElementInjectors; this.rootElementInjectors = rootElementInjectors;
this.onChangeDispatcher = null; this.onChangeDispatcher = null;
this.textNodes = textNodes; this.textNodes = textNodes;
this.bindElements = bindElements; this.bindElements = bindElements;
this.watchGroup = protoWatchGroup.instantiate(this, MapWrapper.create()); this.recordRange = protoRecordRange.instantiate(this, MapWrapper.create());
this.watchGroup.setContext(context); this.recordRange.setContext(context);
// TODO(rado): Since this is only used in tests for now, investigate whether // TODO(rado): Since this is only used in tests for now, investigate whether
// we can remove it. // we can remove it.
this.childViews = []; this.childViews = [];
@ -65,21 +65,21 @@ export class View {
addChild(childView: View) { addChild(childView: View) {
ListWrapper.push(this.childViews, childView); ListWrapper.push(this.childViews, childView);
this.watchGroup.addChild(childView.watchGroup); this.recordRange.addRange(childView.recordRange);
} }
} }
export class ProtoView { export class ProtoView {
@FIELD('final element:Element') @FIELD('final element:Element')
@FIELD('final elementBinders:List<ElementBinder>') @FIELD('final elementBinders:List<ElementBinder>')
@FIELD('final protoWatchGroup:ProtoWatchGroup') @FIELD('final protoRecordRange:ProtoRecordRange')
constructor( constructor(
template:Element, template:Element,
protoWatchGroup:ProtoWatchGroup) { protoRecordRange:ProtoRecordRange) {
this.element = template; this.element = template;
this.elementBinders = []; this.elementBinders = [];
this.variableBindings = MapWrapper.create(); this.variableBindings = MapWrapper.create();
this.protoWatchGroup = protoWatchGroup; this.protoRecordRange = protoRecordRange;
this.textNodesWithBindingCount = 0; this.textNodesWithBindingCount = 0;
this.elementsWithBindingCount = 0; this.elementsWithBindingCount = 0;
} }
@ -115,7 +115,7 @@ export class ProtoView {
viewNodes = [clone]; viewNodes = [clone];
} }
var view = new View(viewNodes, elementInjectors, rootElementInjectors, textNodes, var view = new View(viewNodes, elementInjectors, rootElementInjectors, textNodes,
bindElements, this.protoWatchGroup, context); bindElements, this.protoRecordRange, context);
ProtoView._instantiateDirectives( ProtoView._instantiateDirectives(
view, elements, elementInjectors, lightDomAppInjector, shadowAppInjectors); view, elements, elementInjectors, lightDomAppInjector, shadowAppInjectors);
@ -145,7 +145,7 @@ export class ProtoView {
elBinder.textNodeIndices = ListWrapper.create(); elBinder.textNodeIndices = ListWrapper.create();
} }
ListWrapper.push(elBinder.textNodeIndices, indexInParent); ListWrapper.push(elBinder.textNodeIndices, indexInParent);
this.protoWatchGroup.watch(expression, this.textNodesWithBindingCount++); this.protoRecordRange.addRecordsFromAST(expression, this.textNodesWithBindingCount++);
} }
/** /**
@ -157,7 +157,7 @@ export class ProtoView {
elBinder.hasElementPropertyBindings = true; elBinder.hasElementPropertyBindings = true;
this.elementsWithBindingCount++; this.elementsWithBindingCount++;
} }
this.protoWatchGroup.watch(expression, this.protoRecordRange.addRecordsFromAST(expression,
new ElementPropertyMemento( new ElementPropertyMemento(
this.elementsWithBindingCount-1, this.elementsWithBindingCount-1,
propertyName propertyName
@ -184,7 +184,7 @@ export class ProtoView {
expression:AST, expression:AST,
setterName:string, setterName:string,
setter:SetterFn) { setter:SetterFn) {
this.protoWatchGroup.watch( this.protoRecordRange.addRecordsFromAST(
expression, expression,
new DirectivePropertyMemento( new DirectivePropertyMemento(
this.elementBinders.length-1, this.elementBinders.length-1,
@ -288,7 +288,7 @@ export class ProtoView {
// Used for bootstrapping. // Used for bootstrapping.
static createRootProtoView(protoView: ProtoView, static createRootProtoView(protoView: ProtoView,
insertionElement, rootComponentAnnotatedType: AnnotatedType): ProtoView { insertionElement, rootComponentAnnotatedType: AnnotatedType): ProtoView {
var rootProtoView = new ProtoView(insertionElement, new ProtoWatchGroup()); var rootProtoView = new ProtoView(insertionElement, new ProtoRecordRange());
var binder = rootProtoView.bindElement( var binder = rootProtoView.bindElement(
new ProtoElementInjector(null, 0, [rootComponentAnnotatedType.type], true)); new ProtoElementInjector(null, 0, [rootComponentAnnotatedType.type], true));
binder.componentDirective = rootComponentAnnotatedType; binder.componentDirective = rootComponentAnnotatedType;

View File

@ -9,7 +9,7 @@ export * from './annotations/template_config';
export * from './application'; export * from './application';
export * from 'change_detection/change_detector'; export * from 'change_detection/change_detector';
export * from 'change_detection/watch_group'; export * from 'change_detection/record_range';
export * from 'change_detection/record'; export * from 'change_detection/record';
export * from './compiler/compiler'; export * from './compiler/compiler';

View File

@ -24,12 +24,12 @@ export function main() {
compiler = new Compiler(null, new Reflector(), new Parser(new Lexer(), closureMap), closureMap); compiler = new Compiler(null, new Reflector(), new Parser(new Lexer(), closureMap), closureMap);
}); });
describe('react to watch group changes', function() { describe('react to record changes', function() {
var view, ctx, cd; var view, ctx, cd;
function createView(pv) { function createView(pv) {
ctx = new MyComp(); ctx = new MyComp();
view = pv.instantiate(ctx, new Injector([]), null); view = pv.instantiate(ctx, new Injector([]), null);
cd = new ChangeDetector(view.watchGroup); cd = new ChangeDetector(view.recordRange);
} }
it('should consume text node changes', (done) => { it('should consume text node changes', (done) => {

View File

@ -16,7 +16,7 @@ import {ProtoView, ElementPropertyMemento, DirectivePropertyMemento} from 'core/
import {ProtoElementInjector} from 'core/compiler/element_injector'; import {ProtoElementInjector} from 'core/compiler/element_injector';
import {Reflector} from 'core/compiler/reflector'; import {Reflector} from 'core/compiler/reflector';
import {ProtoWatchGroup} from 'change_detection/watch_group'; import {ProtoRecordRange} from 'change_detection/record_range';
import {Parser} from 'change_detection/parser/parser'; import {Parser} from 'change_detection/parser/parser';
import {Lexer} from 'change_detection/parser/lexer'; import {Lexer} from 'change_detection/parser/lexer';
import {ClosureMap} from 'change_detection/parser/closure_map'; import {ClosureMap} from 'change_detection/parser/closure_map';
@ -36,7 +36,7 @@ export function main() {
new MockStep((parent, current, control) => { new MockStep((parent, current, control) => {
if (isPresent(current.element.getAttribute('viewroot'))) { if (isPresent(current.element.getAttribute('viewroot'))) {
current.isViewRoot = true; current.isViewRoot = true;
current.inheritedProtoView = new ProtoView(current.element, new ProtoWatchGroup()); current.inheritedProtoView = new ProtoView(current.element, new ProtoRecordRange());
} else if (isPresent(parent)) { } else if (isPresent(parent)) {
current.inheritedProtoView = parent.inheritedProtoView; current.inheritedProtoView = parent.inheritedProtoView;
} }
@ -81,7 +81,7 @@ export function main() {
function instantiateView(protoView) { function instantiateView(protoView) {
evalContext = new Context(); evalContext = new Context();
view = protoView.instantiate(evalContext, new Injector([]), null); view = protoView.instantiate(evalContext, new Injector([]), null);
changeDetector = new ChangeDetector(view.watchGroup); changeDetector = new ChangeDetector(view.recordRange);
} }
it('should not create an ElementBinder for elements that have no bindings', () => { it('should not create an ElementBinder for elements that have no bindings', () => {
@ -206,7 +206,7 @@ export function main() {
var results = pipeline.process(createElement('<div viewroot prop-binding directives></div>')); var results = pipeline.process(createElement('<div viewroot prop-binding directives></div>'));
var pv = results[0].inheritedProtoView; var pv = results[0].inheritedProtoView;
results[0].inheritedElementBinder.nestedProtoView = new ProtoView( results[0].inheritedElementBinder.nestedProtoView = new ProtoView(
createElement('<div></div>'), new ProtoWatchGroup()); createElement('<div></div>'), new ProtoRecordRange());
instantiateView(pv); instantiateView(pv);
evalContext.prop1 = 'a'; evalContext.prop1 = 'a';

View File

@ -4,7 +4,7 @@ import {ProtoElementInjector, ElementInjector} from 'core/compiler/element_injec
import {Reflector} from 'core/compiler/reflector'; import {Reflector} from 'core/compiler/reflector';
import {Component} from 'core/annotations/component'; import {Component} from 'core/annotations/component';
import {Decorator} from 'core/annotations/decorator'; import {Decorator} from 'core/annotations/decorator';
import {ProtoWatchGroup} from 'change_detection/watch_group'; import {ProtoRecordRange} from 'change_detection/record_range';
import {ChangeDetector} from 'change_detection/change_detector'; import {ChangeDetector} from 'change_detection/change_detector';
import {TemplateConfig} from 'core/annotations/template_config'; import {TemplateConfig} from 'core/annotations/template_config';
import {Parser} from 'change_detection/parser/parser'; import {Parser} from 'change_detection/parser/parser';
@ -35,7 +35,7 @@ export function main() {
} }
it('should collect the root node in the ProtoView element', () => { it('should collect the root node in the ProtoView element', () => {
var pv = new ProtoView(templateAwareCreateElement('<div id="1"></div>'), new ProtoWatchGroup()); var pv = new ProtoView(templateAwareCreateElement('<div id="1"></div>'), new ProtoRecordRange());
var view = pv.instantiate(null, null, null); var view = pv.instantiate(null, null, null);
expect(view.nodes.length).toBe(1); expect(view.nodes.length).toBe(1);
expect(view.nodes[0].getAttribute('id')).toEqual('1'); expect(view.nodes[0].getAttribute('id')).toEqual('1');
@ -44,7 +44,7 @@ export function main() {
describe('collect elements with property bindings', () => { describe('collect elements with property bindings', () => {
it('should collect property bindings on the root element if it has the ng-binding class', () => { it('should collect property bindings on the root element if it has the ng-binding class', () => {
var pv = new ProtoView(templateAwareCreateElement('<div [prop]="a" class="ng-binding"></div>'), new ProtoWatchGroup()); var pv = new ProtoView(templateAwareCreateElement('<div [prop]="a" class="ng-binding"></div>'), new ProtoRecordRange());
pv.bindElement(null); pv.bindElement(null);
pv.bindElementProperty('prop', parser.parseBinding('a').ast); pv.bindElementProperty('prop', parser.parseBinding('a').ast);
@ -55,7 +55,7 @@ export function main() {
it('should collect property bindings on child elements with ng-binding class', () => { it('should collect property bindings on child elements with ng-binding class', () => {
var pv = new ProtoView(templateAwareCreateElement('<div><span></span><span class="ng-binding"></span></div>'), var pv = new ProtoView(templateAwareCreateElement('<div><span></span><span class="ng-binding"></span></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
pv.bindElement(null); pv.bindElement(null);
pv.bindElementProperty('a', parser.parseBinding('b').ast); pv.bindElementProperty('a', parser.parseBinding('b').ast);
@ -69,7 +69,7 @@ export function main() {
describe('collect text nodes with bindings', () => { describe('collect text nodes with bindings', () => {
it('should collect text nodes under the root element', () => { it('should collect text nodes under the root element', () => {
var pv = new ProtoView(templateAwareCreateElement('<div class="ng-binding">{{}}<span></span>{{}}</div>'), new ProtoWatchGroup()); var pv = new ProtoView(templateAwareCreateElement('<div class="ng-binding">{{}}<span></span>{{}}</div>'), new ProtoRecordRange());
pv.bindElement(null); pv.bindElement(null);
pv.bindTextNode(0, parser.parseBinding('a').ast); pv.bindTextNode(0, parser.parseBinding('a').ast);
pv.bindTextNode(2, parser.parseBinding('b').ast); pv.bindTextNode(2, parser.parseBinding('b').ast);
@ -82,7 +82,7 @@ export function main() {
it('should collect text nodes with bindings on child elements with ng-binding class', () => { it('should collect text nodes with bindings on child elements with ng-binding class', () => {
var pv = new ProtoView(templateAwareCreateElement('<div><span> </span><span class="ng-binding">{{}}</span></div>'), var pv = new ProtoView(templateAwareCreateElement('<div><span> </span><span class="ng-binding">{{}}</span></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
pv.bindElement(null); pv.bindElement(null);
pv.bindTextNode(0, parser.parseBinding('b').ast); pv.bindTextNode(0, parser.parseBinding('b').ast);
@ -97,14 +97,14 @@ export function main() {
describe('inplace instantiation', () => { describe('inplace instantiation', () => {
it('should be supported.', () => { it('should be supported.', () => {
var template = createElement('<div></div>') var template = createElement('<div></div>')
var view = new ProtoView(template, new ProtoWatchGroup()) var view = new ProtoView(template, new ProtoRecordRange())
.instantiate(null, null, null, true); .instantiate(null, null, null, true);
expect(view.nodes[0]).toBe(template); expect(view.nodes[0]).toBe(template);
}); });
it('should be off by default.', () => { it('should be off by default.', () => {
var template = createElement('<div></div>') var template = createElement('<div></div>')
var view = new ProtoView(template, new ProtoWatchGroup()) var view = new ProtoView(template, new ProtoRecordRange())
.instantiate(null, null, null); .instantiate(null, null, null);
expect(view.nodes[0]).not.toBe(template); expect(view.nodes[0]).not.toBe(template);
}); });
@ -120,7 +120,7 @@ export function main() {
describe('create ElementInjectors', () => { describe('create ElementInjectors', () => {
it('should use the directives of the ProtoElementInjector', () => { it('should use the directives of the ProtoElementInjector', () => {
var pv = new ProtoView(createElement('<div class="ng-binding"></div>'), new ProtoWatchGroup()); var pv = new ProtoView(createElement('<div class="ng-binding"></div>'), new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 1, [SomeDirective])); pv.bindElement(new ProtoElementInjector(null, 1, [SomeDirective]));
var view = pv.instantiate(null, null, null); var view = pv.instantiate(null, null, null);
@ -130,7 +130,7 @@ export function main() {
it('should use the correct parent', () => { it('should use the correct parent', () => {
var pv = new ProtoView(createElement('<div class="ng-binding"><span class="ng-binding"></span></div>'), var pv = new ProtoView(createElement('<div class="ng-binding"><span class="ng-binding"></span></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
var protoParent = new ProtoElementInjector(null, 0, [SomeDirective]); var protoParent = new ProtoElementInjector(null, 0, [SomeDirective]);
pv.bindElement(protoParent); pv.bindElement(protoParent);
pv.bindElement(new ProtoElementInjector(protoParent, 1, [AnotherDirective])); pv.bindElement(new ProtoElementInjector(protoParent, 1, [AnotherDirective]));
@ -146,7 +146,7 @@ export function main() {
it('should collect a single root element injector', () => { it('should collect a single root element injector', () => {
var pv = new ProtoView(createElement('<div class="ng-binding"><span class="ng-binding"></span></div>'), var pv = new ProtoView(createElement('<div class="ng-binding"><span class="ng-binding"></span></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
var protoParent = new ProtoElementInjector(null, 0, [SomeDirective]); var protoParent = new ProtoElementInjector(null, 0, [SomeDirective]);
pv.bindElement(protoParent); pv.bindElement(protoParent);
pv.bindElement(new ProtoElementInjector(protoParent, 1, [AnotherDirective])); pv.bindElement(new ProtoElementInjector(protoParent, 1, [AnotherDirective]));
@ -158,7 +158,7 @@ export function main() {
it('should collect multiple root element injectors', () => { it('should collect multiple root element injectors', () => {
var pv = new ProtoView(createElement('<div><span class="ng-binding"></span><span class="ng-binding"></span></div>'), var pv = new ProtoView(createElement('<div><span class="ng-binding"></span><span class="ng-binding"></span></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 1, [SomeDirective])); pv.bindElement(new ProtoElementInjector(null, 1, [SomeDirective]));
pv.bindElement(new ProtoElementInjector(null, 2, [AnotherDirective])); pv.bindElement(new ProtoElementInjector(null, 2, [AnotherDirective]));
@ -174,7 +174,7 @@ export function main() {
var view, ctx; var view, ctx;
function createComponentWithSubPV(subProtoView) { function createComponentWithSubPV(subProtoView) {
var pv = new ProtoView(createElement('<cmp class="ng-binding"></cmp>'), new ProtoWatchGroup()); var pv = new ProtoView(createElement('<cmp class="ng-binding"></cmp>'), new ProtoRecordRange());
var binder = pv.bindElement(new ProtoElementInjector(null, 0, [SomeComponent], true)); var binder = pv.bindElement(new ProtoElementInjector(null, 0, [SomeComponent], true));
binder.componentDirective = someComponentDirective; binder.componentDirective = someComponentDirective;
binder.nestedProtoView = subProtoView; binder.nestedProtoView = subProtoView;
@ -187,7 +187,7 @@ export function main() {
} }
it('should create shadow dom', () => { it('should create shadow dom', () => {
var subpv = new ProtoView(createElement('<span>hello shadow dom</span>'), new ProtoWatchGroup()); var subpv = new ProtoView(createElement('<span>hello shadow dom</span>'), new ProtoRecordRange());
var pv = createComponentWithSubPV(subpv); var pv = createComponentWithSubPV(subpv);
var view = createNestedView(pv); var view = createNestedView(pv);
@ -196,7 +196,7 @@ export function main() {
}); });
it('should expose component services to the component', () => { it('should expose component services to the component', () => {
var subpv = new ProtoView(createElement('<span></span>'), new ProtoWatchGroup()); var subpv = new ProtoView(createElement('<span></span>'), new ProtoRecordRange());
var pv = createComponentWithSubPV(subpv); var pv = createComponentWithSubPV(subpv);
var view = createNestedView(pv); var view = createNestedView(pv);
@ -208,7 +208,7 @@ export function main() {
it('should expose component services and component instance to directives in the shadow Dom', it('should expose component services and component instance to directives in the shadow Dom',
() => { () => {
var subpv = new ProtoView( var subpv = new ProtoView(
createElement('<div dec class="ng-binding">hello shadow dom</div>'), new ProtoWatchGroup()); createElement('<div dec class="ng-binding">hello shadow dom</div>'), new ProtoRecordRange());
var subBinder = subpv.bindElement( var subBinder = subpv.bindElement(
new ProtoElementInjector(null, 0, [ServiceDependentDecorator])); new ProtoElementInjector(null, 0, [ServiceDependentDecorator]));
var pv = createComponentWithSubPV(subpv); var pv = createComponentWithSubPV(subpv);
@ -226,18 +226,18 @@ export function main() {
}); });
}); });
describe('react to watch group changes', () => { describe('react to record changes', () => {
var view, cd, ctx; var view, cd, ctx;
function createView(protoView) { function createView(protoView) {
ctx = new MyEvaluationContext(); ctx = new MyEvaluationContext();
view = protoView.instantiate(ctx, null, null); view = protoView.instantiate(ctx, null, null);
cd = new ChangeDetector(view.watchGroup); cd = new ChangeDetector(view.recordRange);
} }
it('should consume text node changes', () => { it('should consume text node changes', () => {
var pv = new ProtoView(createElement('<div class="ng-binding">{{}}</div>'), var pv = new ProtoView(createElement('<div class="ng-binding">{{}}</div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
pv.bindElement(null); pv.bindElement(null);
pv.bindTextNode(0, parser.parseBinding('foo').ast); pv.bindTextNode(0, parser.parseBinding('foo').ast);
createView(pv); createView(pv);
@ -249,7 +249,7 @@ export function main() {
it('should consume element binding changes', () => { it('should consume element binding changes', () => {
var pv = new ProtoView(createElement('<div class="ng-binding"></div>'), var pv = new ProtoView(createElement('<div class="ng-binding"></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
pv.bindElement(null); pv.bindElement(null);
pv.bindElementProperty('id', parser.parseBinding('foo').ast); pv.bindElementProperty('id', parser.parseBinding('foo').ast);
createView(pv); createView(pv);
@ -261,7 +261,7 @@ export function main() {
it('should consume directive watch expression change.', () => { it('should consume directive watch expression change.', () => {
var pv = new ProtoView(createElement('<div class="ng-binding"></div>'), var pv = new ProtoView(createElement('<div class="ng-binding"></div>'),
new ProtoWatchGroup()); new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 0, [SomeDirective])); pv.bindElement(new ProtoElementInjector(null, 0, [SomeDirective]));
pv.bindDirectiveProperty( 0, parser.parseBinding('foo').ast, 'prop', closureMap.setter('prop')); pv.bindDirectiveProperty( 0, parser.parseBinding('foo').ast, 'prop', closureMap.setter('prop'));
createView(pv); createView(pv);
@ -277,7 +277,7 @@ export function main() {
var el, pv; var el, pv;
beforeEach(() => { beforeEach(() => {
el = DOM.createElement('div'); el = DOM.createElement('div');
pv = new ProtoView(createElement('<div>hi</div>'), new ProtoWatchGroup()); pv = new ProtoView(createElement('<div>hi</div>'), new ProtoRecordRange());
}); });
it('should create the root component when instantiated', () => { it('should create the root component when instantiated', () => {