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 {FIELD, int, isPresent} from 'facade/lang';
export * from './record';
export * from './watch_group'
export * from './record_range'
export class ChangeDetector {
@FIELD('final _rootWatchGroup:WatchGroup')
constructor(watchGroup:WatchGroup) {
this._rootWatchGroup = watchGroup;
@FIELD('final _rootRecordRange:RecordRange')
constructor(recordRange:RecordRange) {
this._rootRecordRange = recordRange;
}
detectChanges():int {
var count:int = 0;
for (var record = this._rootWatchGroup.findFirstEnabledRecord();
for (var record = this._rootRecordRange.findFirstEnabledRecord();
isPresent(record);
record = record.nextEnabled) {
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 {ListWrapper, MapWrapper} from 'facade/collection';
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.
*/
export class ProtoRecord {
@FIELD('final watchGroup:wg.ProtoWatchGroup')
@FIELD('final recordRange:ProtoRecordRange')
@FIELD('final context:Object')
@FIELD('final funcOrValue:Object')
@FIELD('final arity:int')
@ -26,13 +26,13 @@ export class ProtoRecord {
@FIELD('next:ProtoRecord')
@FIELD('prev:ProtoRecord')
@FIELD('recordInConstruction:Record')
constructor(watchGroup:ProtoWatchGroup,
constructor(recordRange:ProtoRecordRange,
recordType:string,
funcOrValue,
arity:int,
dest) {
this.watchGroup = watchGroup;
this.recordRange = recordRange;
this.recordType = recordType;
this.funcOrValue = funcOrValue;
this.arity = arity;
@ -61,7 +61,7 @@ export class ProtoRecord {
* - Keep this object as lean as possible. (Lean in number of fields)
*/
export class Record {
@FIELD('final watchGroup:WatchGroup')
@FIELD('final recordRange:RecordRange')
@FIELD('final protoRecord:ProtoRecord')
@FIELD('next:Record')
@FIELD('prev:Record')
@ -86,8 +86,8 @@ export class Record {
// Otherwise it is the context used by WatchGroupDispatcher.
@FIELD('dest')
constructor(watchGroup:WatchGroup, protoRecord:ProtoRecord, formatters:Map) {
this.watchGroup = watchGroup;
constructor(recordRange:RecordRange, protoRecord:ProtoRecord, formatters:Map) {
this.recordRange = recordRange;
this.protoRecord = protoRecord;
this.next = null;
@ -140,8 +140,8 @@ export class Record {
}
}
static createMarker(wg:WatchGroup) {
var r = new Record(wg, null, null);
static createMarker(rr:RecordRange) {
var r = new Record(rr, null, null);
r.disabled = true;
return r;
}
@ -166,7 +166,7 @@ export class Record {
this.dest.updateContext(this.currentValue);
}
} 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);
case MODE_STATE_INVOKE_PURE_FUNCTION:
this.watchGroup.disableRecord(this);
this.recordRange.disableRecord(this);
return FunctionWrapper.apply(this.funcOrValue, this.args);
case MODE_STATE_CONST:
this.watchGroup.disableRecord(this);
this.recordRange.disableRecord(this);
return this.funcOrValue;
case MODE_STATE_MARKER:
@ -206,18 +206,18 @@ export class Record {
updateArg(value, position:int) {
this.args[position] = value;
this.watchGroup.enableRecord(this);
this.recordRange.enableRecord(this);
}
updateContext(value) {
this.context = value;
if (! this.isMarkerRecord) {
this.watchGroup.enableRecord(this);
if (!this.isMarkerRecord) {
this.recordRange.enableRecord(this);
}
}
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,
LiteralArray, LiteralMap, KeyedAccess, Chain, Assignment} from './parser/ast';
export class ProtoWatchGroup {
export class ProtoRecordRange {
@FIELD('headRecord:ProtoRecord')
@FIELD('tailRecord:ProtoRecord')
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 memento an opaque object which will be passed to WatchGroupDispatcher on
* detecting a change.
* @param shallow Should collections be shallow watched
*/
watch(ast:AST,
addRecordsFromAST(ast:AST,
memento,
shallow = false)
{
@ -45,20 +46,20 @@ export class ProtoWatchGroup {
// TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher.
// but @Implements is not ready yet.
instantiate(dispatcher, formatters:Map):WatchGroup {
var watchGroup:WatchGroup = new WatchGroup(this, dispatcher);
instantiate(dispatcher, formatters:Map):RecordRange {
var recordRange:RecordRange = new RecordRange(this, dispatcher);
if (this.headRecord !== null) {
this._createRecords(watchGroup, formatters);
this._createRecords(recordRange, formatters);
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) {
var record = new Record(watchGroup, proto, formatters);
var record = new Record(recordRange, proto, formatters);
proto.recordInConstruction = record;
watchGroup.addRecord(record);
recordRange.addRecord(record);
}
}
@ -72,16 +73,16 @@ export class ProtoWatchGroup {
}
}
export class WatchGroup {
@FIELD('final protoWatchGroup:ProtoWatchGroup')
export class RecordRange {
@FIELD('final protoRecordRange:ProtoRecordRange')
@FIELD('final dispatcher:WatchGroupDispatcher')
@FIELD('final headRecord:Record')
@FIELD('final tailRecord:Record')
@FIELD('final disabled:boolean')
// TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher.
// but @Implements is not ready yet.
constructor(protoWatchGroup:ProtoWatchGroup, dispatcher) {
this.protoWatchGroup = protoWatchGroup;
constructor(protoRecordRange:ProtoRecordRange, dispatcher) {
this.protoRecordRange = protoRecordRange;
this.dispatcher = dispatcher;
this.disabled = false;
@ -89,64 +90,53 @@ export class WatchGroup {
this.headRecord = Record.createMarker(this);
this.tailRecord = Record.createMarker(this);
this.headRecord.next = this.tailRecord;
this.tailRecord.prev = this.headRecord;
_glue(this.headRecord, this.tailRecord);
}
/// addRecord assumes that all records are enabled
/// addRecord assumes that the record is newly created, so it is enabled.
addRecord(record:Record) {
var lastRecord = this.tailRecord.prev;
lastRecord.next = record;
lastRecord.nextEnabled = record;
record.prev = lastRecord;
record.prevEnabled = lastRecord;
record.next = this.tailRecord;
this.tailRecord.prev = record;
_glue(lastRecord, record);
_glueEnabled(lastRecord, record);
_glue(record, this.tailRecord);
}
addChild(child:WatchGroup) {
addRange(child:RecordRange) {
var lastRecord = this.tailRecord.prev;
var lastEnabledRecord = this.findLastEnabledRecord();
var firstEnabledChildRecord = child.findFirstEnabledRecord();
lastRecord.next = child.headRecord;
child.headRecord.prev = lastRecord;
child.tailRecord.next = this.tailRecord;
this.tailRecord.prev = child.tailRecord;
_glue(lastRecord, child.headRecord);
_glue(child.tailRecord, this.tailRecord);
if (isPresent(lastEnabledRecord) && isPresent(firstEnabledChildRecord)) {
lastEnabledRecord.nextEnabled = firstEnabledChildRecord;
firstEnabledChildRecord.prevEnabled = lastEnabledRecord;
_glueEnabled(lastEnabledRecord, firstEnabledChildRecord);
}
}
removeChild(child:WatchGroup) {
removeRange(child:RecordRange) {
var firstEnabledChildRecord = child.findFirstEnabledRecord();
var lastEnabledChildRecord = child.findLastEnabledRecord();
var next = child.tailRecord.next;
var prev = child.headRecord.prev;
next.prev = prev;
prev.next = next;
_glue(prev, next);
var nextEnabled = lastEnabledChildRecord.nextEnabled;
var prevEnabled = firstEnabledChildRecord.prevEnabled;
if (isPresent(nextEnabled)) nextEnabled.prev = prevEnabled;
if (isPresent(prevEnabled)) prevEnabled.next = nextEnabled;
if (isPresent(nextEnabled)) nextEnabled.prevEnabled = prevEnabled;
if (isPresent(prevEnabled)) prevEnabled.nextEnabled = nextEnabled;
}
findFirstEnabledRecord() {
return this._nextEnabled(this.headRecord);
return this._nextEnabledInCurrentRange(this.headRecord);
}
findLastEnabledRecord() {
return this._prevEnabled(this.tailRecord);
return this._prevEnabledInCurrentRange(this.tailRecord);
}
disableRecord(record:Record) {
@ -162,8 +152,8 @@ export class WatchGroup {
enableRecord(record:Record) {
if (!record.disabled) return;
var prevEnabled = this._prevEnabled(record);
var nextEnabled = this._nextEnabled(record);
var prevEnabled = this._prevEnabledInCurrentRange(record);
var nextEnabled = this._nextEnabledInCurrentRange(record);
record.prevEnabled = prevEnabled;
record.nextEnabled = nextEnabled;
@ -174,7 +164,7 @@ export class WatchGroup {
record.disabled = false;
}
disableGroup(child:WatchGroup) {
disableRange(child:RecordRange) {
var firstEnabledChildRecord = child.findFirstEnabledRecord();
var lastEnabledChildRecord = child.findLastEnabledRecord();
@ -187,31 +177,32 @@ export class WatchGroup {
child.disabled = true;
}
enableGroup(child:WatchGroup) {
var prevEnabledRecord = this._prevEnabled(child.headRecord);
var nextEnabledRecord = this._nextEnabled(child.tailRecord);
enableRange(child:RecordRange) {
var prevEnabledRecord = this._prevEnabledInCurrentRange(child.headRecord);
var nextEnabledRecord = this._nextEnabledInCurrentRange(child.tailRecord);
var firstEnabledChildRecord = child.findFirstEnabledRecord();
var lastEnabledChildRecord = child.findLastEnabledRecord();
if (isPresent(firstEnabledChildRecord) && isPresent(prevEnabledRecord)){
firstEnabledChildRecord.prevEnabled = prevEnabledRecord;
prevEnabledRecord.nextEnabled = firstEnabledChildRecord;
_glueEnabled(prevEnabledRecord, firstEnabledChildRecord);
}
if (isPresent(lastEnabledChildRecord) && isPresent(nextEnabledRecord)){
lastEnabledChildRecord.nextEnabled = nextEnabledRecord;
nextEnabledRecord.prevEnabled = lastEnabledChildRecord;
_glueEnabled(lastEnabledChildRecord, nextEnabledRecord);
}
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;
while (isPresent(record) && record !== this.tailRecord && record.disabled) {
if (record.isMarkerRecord && record.watchGroup.disabled) {
record = record.watchGroup.tailRecord.next;
if (record.isMarkerRecord && record.recordRange.disabled) {
record = record.recordRange.tailRecord.next;
} else {
record = record.next;
}
@ -219,11 +210,14 @@ export class WatchGroup {
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;
while (isPresent(record) && record !== this.headRecord && record.disabled) {
if (record.isMarkerRecord && record.watchGroup.disabled) {
record = record.watchGroup.headRecord.prev;
if (record.isMarkerRecord && record.recordRange.disabled) {
record = record.recordRange.headRecord.prev;
} else {
record = record.prev;
}
@ -233,10 +227,10 @@ export class WatchGroup {
/**
* Sets the context (the object) on which the change detection expressions will
* dereference themselves on. Since the WatchGroup can be reused the context
* can be re-set many times during the lifetime of the WatchGroup.
* dereference themselves on. Since the RecordRange can be reused the context
* 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) {
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 {
// The record holds the previous value at the time of the call
onRecordChange(record:Record, context) {}
@ -264,11 +268,11 @@ class Destination {
@IMPLEMENTS(AstVisitor)
class ProtoRecordCreator {
@FIELD('final protoWatchGroup:ProtoWatchGroup')
@FIELD('final protoRecordRange:ProtoRecordRange')
@FIELD('headRecord:ProtoRecord')
@FIELD('tailRecord:ProtoRecord')
constructor(protoWatchGroup) {
this.protoWatchGroup = protoWatchGroup;
constructor(protoRecordRange) {
this.protoRecordRange = protoRecordRange;
this.headRecord = null;
this.tailRecord = null;
}
@ -365,7 +369,7 @@ class ProtoRecordCreator {
}
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) {