refactor(ChangeDetector): misc minor updates

This commit is contained in:
Victor Berchet 2015-10-15 16:24:42 -07:00
parent c56efc0c5f
commit b91351469f
3 changed files with 38 additions and 34 deletions

View File

@ -3,14 +3,15 @@ import {ListWrapper, Map} from 'angular2/src/core/facade/collection';
import {RecordType, ProtoRecord} from './proto_record'; import {RecordType, ProtoRecord} from './proto_record';
/** /**
* Removes "duplicate" records. It assuming that record evaluation does not * Removes "duplicate" records. It assumes that record evaluation does not have side-effects.
* have side-effects.
* *
* Records that are not last in bindings are removed and all the indices * Records that are not last in bindings are removed and all the indices of the records that depend
* of the records that depend on them are updated. * on them are updated.
* *
* Records that are last in bindings CANNOT be removed, and instead are * Records that are last in bindings CANNOT be removed, and instead are replaced with very cheap
* replaced with very cheap SELF records. * SELF records.
*
* @internal
*/ */
export function coalesce(records: ProtoRecord[]): ProtoRecord[] { export function coalesce(records: ProtoRecord[]): ProtoRecord[] {
var res: ProtoRecord[] = []; var res: ProtoRecord[] = [];

View File

@ -7,7 +7,7 @@ import {EventBinding} from './event_binding';
import {BindingRecord, BindingTarget} from './binding_record'; import {BindingRecord, BindingTarget} from './binding_record';
import {DirectiveRecord, DirectiveIndex} from './directive_record'; import {DirectiveRecord, DirectiveIndex} from './directive_record';
import {Locals} from './parser/locals'; import {Locals} from './parser/locals';
import {ChangeDetectorGenConfig} from './interfaces'; import {ChangeDispatcher, ChangeDetectorGenConfig} from './interfaces';
import {ChangeDetectionUtil, SimpleChange} from './change_detection_util'; import {ChangeDetectionUtil, SimpleChange} from './change_detection_util';
import {ChangeDetectionStrategy, ChangeDetectorState} from './constants'; import {ChangeDetectionStrategy, ChangeDetectorState} from './constants';
import {ProtoRecord, RecordType} from './proto_record'; import {ProtoRecord, RecordType} from './proto_record';
@ -19,7 +19,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
prevContexts: any[]; prevContexts: any[];
directives: any = null; directives: any = null;
constructor(id: string, dispatcher: any, numberOfPropertyProtoRecords: number, constructor(id: string, dispatcher: ChangeDispatcher, numberOfPropertyProtoRecords: number,
propertyBindingTargets: BindingTarget[], directiveIndices: DirectiveIndex[], propertyBindingTargets: BindingTarget[], directiveIndices: DirectiveIndex[],
strategy: ChangeDetectionStrategy, private _records: ProtoRecord[], strategy: ChangeDetectionStrategy, private _records: ProtoRecord[],
private _eventBindings: EventBinding[], private _directiveRecords: DirectiveRecord[], private _eventBindings: EventBinding[], private _directiveRecords: DirectiveRecord[],
@ -195,7 +195,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_updateDirectiveOrElement(change, bindingRecord) { private _updateDirectiveOrElement(change, bindingRecord) {
if (isBlank(bindingRecord.directiveRecord)) { if (isBlank(bindingRecord.directiveRecord)) {
super.notifyDispatcher(change.currentValue); super.notifyDispatcher(change.currentValue);
} else { } else {
@ -209,7 +209,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_addChange(bindingRecord: BindingRecord, change, changes) { private _addChange(bindingRecord: BindingRecord, change, changes) {
if (bindingRecord.callOnChanges()) { if (bindingRecord.callOnChanges()) {
return super.addChange(changes, change.previousValue, change.currentValue); return super.addChange(changes, change.previousValue, change.currentValue);
} else { } else {
@ -218,13 +218,16 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_getDirectiveFor(directiveIndex) { return this.directives.getDirectiveFor(directiveIndex); } private _getDirectiveFor(directiveIndex) {
return this.directives.getDirectiveFor(directiveIndex);
}
/** @internal */ /** @internal */
_getDetectorFor(directiveIndex) { return this.directives.getDetectorFor(directiveIndex); } private _getDetectorFor(directiveIndex) { return this.directives.getDetectorFor(directiveIndex); }
/** @internal */ /** @internal */
_check(proto: ProtoRecord, throwOnChange: boolean, values: any[], locals: Locals): SimpleChange { private _check(proto: ProtoRecord, throwOnChange: boolean, values: any[],
locals: Locals): SimpleChange {
if (proto.isPipeRecord()) { if (proto.isPipeRecord()) {
return this._pipeCheck(proto, throwOnChange, values); return this._pipeCheck(proto, throwOnChange, values);
} else { } else {
@ -233,7 +236,8 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_referenceCheck(proto: ProtoRecord, throwOnChange: boolean, values: any[], locals: Locals) { private _referenceCheck(proto: ProtoRecord, throwOnChange: boolean, values: any[],
locals: Locals) {
if (this._pureFuncAndArgsDidNotChange(proto)) { if (this._pureFuncAndArgsDidNotChange(proto)) {
this._setChanged(proto, false); this._setChanged(proto, false);
return null; return null;
@ -272,7 +276,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_calculateCurrValue(proto: ProtoRecord, values: any[], locals: Locals) { private _calculateCurrValue(proto: ProtoRecord, values: any[], locals: Locals) {
switch (proto.mode) { switch (proto.mode) {
case RecordType.Self: case RecordType.Self:
return this._readContext(proto, values); return this._readContext(proto, values);
@ -340,7 +344,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_pipeCheck(proto: ProtoRecord, throwOnChange: boolean, values: any[]) { private _pipeCheck(proto: ProtoRecord, throwOnChange: boolean, values: any[]) {
var context = this._readContext(proto, values); var context = this._readContext(proto, values);
var selectedPipe = this._pipeFor(proto, context); var selectedPipe = this._pipeFor(proto, context);
if (!selectedPipe.pure || this._argsOrContextChanged(proto)) { if (!selectedPipe.pure || this._argsOrContextChanged(proto)) {
@ -379,7 +383,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_pipeFor(proto: ProtoRecord, context) { private _pipeFor(proto: ProtoRecord, context) {
var storedPipe = this._readPipe(proto); var storedPipe = this._readPipe(proto);
if (isPresent(storedPipe)) return storedPipe; if (isPresent(storedPipe)) return storedPipe;
@ -389,7 +393,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_readContext(proto: ProtoRecord, values: any[]) { private _readContext(proto: ProtoRecord, values: any[]) {
if (proto.contextIndex == -1) { if (proto.contextIndex == -1) {
return this._getDirectiveFor(proto.directiveIndex); return this._getDirectiveFor(proto.directiveIndex);
} }
@ -397,29 +401,29 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_readSelf(proto: ProtoRecord, values: any[]) { return values[proto.selfIndex]; } private _readSelf(proto: ProtoRecord, values: any[]) { return values[proto.selfIndex]; }
/** @internal */ /** @internal */
_writeSelf(proto: ProtoRecord, value, values: any[]) { values[proto.selfIndex] = value; } private _writeSelf(proto: ProtoRecord, value, values: any[]) { values[proto.selfIndex] = value; }
/** @internal */ /** @internal */
_readPipe(proto: ProtoRecord) { return this.localPipes[proto.selfIndex]; } private _readPipe(proto: ProtoRecord) { return this.localPipes[proto.selfIndex]; }
/** @internal */ /** @internal */
_writePipe(proto: ProtoRecord, value) { this.localPipes[proto.selfIndex] = value; } private _writePipe(proto: ProtoRecord, value) { this.localPipes[proto.selfIndex] = value; }
/** @internal */ /** @internal */
_setChanged(proto: ProtoRecord, value: boolean) { private _setChanged(proto: ProtoRecord, value: boolean) {
if (proto.argumentToPureFunction) this.changes[proto.selfIndex] = value; if (proto.argumentToPureFunction) this.changes[proto.selfIndex] = value;
} }
/** @internal */ /** @internal */
_pureFuncAndArgsDidNotChange(proto: ProtoRecord): boolean { private _pureFuncAndArgsDidNotChange(proto: ProtoRecord): boolean {
return proto.isPureFunction() && !this._argsChanged(proto); return proto.isPureFunction() && !this._argsChanged(proto);
} }
/** @internal */ /** @internal */
_argsChanged(proto: ProtoRecord): boolean { private _argsChanged(proto: ProtoRecord): boolean {
var args = proto.args; var args = proto.args;
for (var i = 0; i < args.length; ++i) { for (var i = 0; i < args.length; ++i) {
if (this.changes[args[i]]) { if (this.changes[args[i]]) {
@ -430,12 +434,12 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
} }
/** @internal */ /** @internal */
_argsOrContextChanged(proto: ProtoRecord): boolean { private _argsOrContextChanged(proto: ProtoRecord): boolean {
return this._argsChanged(proto) || this.changes[proto.contextIndex]; return this._argsChanged(proto) || this.changes[proto.contextIndex];
} }
/** @internal */ /** @internal */
_readArgs(proto: ProtoRecord, values: any[]) { private _readArgs(proto: ProtoRecord, values: any[]) {
var res = ListWrapper.createFixedSize(proto.args.length); var res = ListWrapper.createFixedSize(proto.args.length);
var args = proto.args; var args = proto.args;
for (var i = 0; i < args.length; ++i) { for (var i = 0; i < args.length; ++i) {

View File

@ -80,9 +80,7 @@ export function createEventRecords(definition: ChangeDetectorDefinition): EventB
} }
export class ProtoRecordBuilder { export class ProtoRecordBuilder {
records: ProtoRecord[]; records: ProtoRecord[] = [];
constructor() { this.records = []; }
add(b: BindingRecord, variableNames: string[], bindingIndex: number) { add(b: BindingRecord, variableNames: string[], bindingIndex: number) {
var oldLast = ListWrapper.last(this.records); var oldLast = ListWrapper.last(this.records);
@ -265,8 +263,7 @@ class _ConvertAstIntoProtoRecords implements AstVisitor {
return this._addRecord(RecordType.Chain, "chain", null, args, null, 0); return this._addRecord(RecordType.Chain, "chain", null, args, null, 0);
} }
/** @internal */ private _visitAll(asts: any[]) {
_visitAll(asts: any[]) {
var res = ListWrapper.createFixedSize(asts.length); var res = ListWrapper.createFixedSize(asts.length);
for (var i = 0; i < asts.length; ++i) { for (var i = 0; i < asts.length; ++i) {
res[i] = asts[i].visit(this); res[i] = asts[i].visit(this);
@ -274,8 +271,10 @@ class _ConvertAstIntoProtoRecords implements AstVisitor {
return res; return res;
} }
/** @internal */ /**
_addRecord(type, name, funcOrValue, args, fixedArgs, context) { * Adds a `ProtoRecord` and returns its selfIndex.
*/
private _addRecord(type, name, funcOrValue, args, fixedArgs, context): number {
var selfIndex = this._records.length + 1; var selfIndex = this._records.length + 1;
if (context instanceof DirectiveIndex) { if (context instanceof DirectiveIndex) {
this._records.push(new ProtoRecord(type, name, funcOrValue, args, fixedArgs, -1, context, this._records.push(new ProtoRecord(type, name, funcOrValue, args, fixedArgs, -1, context,