feat(ChangeDetector): Add support for chained properties

This commit is contained in:
Victor Berchet
2014-10-27 17:57:36 +01:00
parent 63494a74bf
commit c90a7114d3
4 changed files with 70 additions and 28 deletions

View File

@ -44,19 +44,19 @@ export class Record {
@FIELD('final watchGroup:WatchGroup')
@FIELD('final protoRecord:ProtoRecord')
/// order list of all records. Including head/tail markers
@FIELD('_next:Record')
@FIELD('_prev:Record')
@FIELD('next:Record')
@FIELD('prev:Record')
/// next record to dirty check
@FIELD('_checkNext:Record')
@FIELD('_checkPrev:Record')
@FIELD('checkNext:Record')
@FIELD('checkPrev:Record')
// next notifier
@FIELD('_notifierNext:Record')
@FIELD('notifierNext:Record')
@FIELD('_mode:int')
@FIELD('_context')
@FIELD('_getter')
@FIELD('_arguments')
@FIELD('mode:int')
@FIELD('context')
@FIELD('getter')
@FIELD('previousValue')
@FIELD('currentValue')
constructor(watchGroup/*:wg.WatchGroup*/, protoRecord:ProtoRecord) {
this.protoRecord = protoRecord;
this.watchGroup = watchGroup;
@ -71,7 +71,8 @@ export class Record {
this.getter = null;
this.arguments = null;
this.previousValue = null;
this.currentValue = null;
// `this` means that the record is fresh
this.currentValue = this;
}
check():boolean {
@ -119,10 +120,9 @@ export class Record {
}
}
// todo(vicb): compute this info only once in ctor ? (add a bit in mode not to grow the mem req)
if (this.protoRecord.dispatchMemento === null) {
// forward propagate to the next record
this.next.setContext(this.currentValue);
} else {
// notify through dispatcher
this.watchGroup.dispatcher.onRecordChange(this, this.protoRecord.dispatchMemento);
@ -132,8 +132,6 @@ export class Record {
}
setContext(context) {
// use `this` as a marker for a fresh record
this.currentValue = this;
this.mode = MODE_STATE_PROPERTY;
this.context = context;
var factory = new FieldGetterFactory();

View File

@ -1,5 +1,6 @@
import {ProtoRecord, Record} from './record';
import {FIELD} from 'facade/lang';
import {ListWrapper} from 'facade/collection';
export class ProtoWatchGroup {
@FIELD('final headRecord:ProtoRecord')
@ -19,16 +20,25 @@ export class ProtoWatchGroup {
*/
watch(expression:string,
memento,
shallow /*= false*/) // TODO(vicb): comment out when opt-params are supported
shallow = false)
{
var protoRecord = new ProtoRecord(this, expression, memento);
var parts = expression.split('.');
var protoRecords = ListWrapper.createFixedSize(parts.length);
if (this.headRecord === null) {
this.headRecord = this.tailRecord = protoRecord;
} else {
this.tailRecord.next = protoRecord;
protoRecord.prev = this.tailRecord;
this.tailRecord = protoRecord;
for (var i = parts.length - 1; i >= 0; i--) {
protoRecords[i] = new ProtoRecord(this, parts[i], memento);
memento = null;
}
for (var i = 0; i < parts.length; i++) {
var protoRecord = protoRecords[i];
if (this.headRecord === null) {
this.headRecord = this.tailRecord = protoRecord;
} else {
this.tailRecord.next = protoRecord;
protoRecord.prev = this.tailRecord;
this.tailRecord = protoRecord;
}
}
}