refactor(change_detector): extracted ChangeDetectorDefinition

This commit is contained in:
vsavkin
2015-05-13 13:01:31 -07:00
parent fadabf79e3
commit d8c7c274e4
8 changed files with 1060 additions and 1051 deletions

View File

@ -9,7 +9,7 @@ import {NullPipeFactory} from './pipes/null_pipe';
import {BindingRecord} from './binding_record';
import {DirectiveRecord} from './directive_record';
import {DEFAULT} from './constants';
import {ChangeDetection, ProtoChangeDetector} from './interfaces';
import {ChangeDetection, ProtoChangeDetector, ChangeDetectorDefinition} from './interfaces';
import {Injectable} from 'angular2/src/di/decorators';
import {List} from 'angular2/src/facade/collection';
@ -59,11 +59,8 @@ export var defaultPipes = {
export class DynamicChangeDetection extends ChangeDetection {
constructor(public registry: PipeRegistry) { super(); }
createProtoChangeDetector(name: string, bindingRecords: List<BindingRecord>,
variableBindings: List<string>, directiveRecords: List<DirectiveRecord>,
changeControlStrategy: string = DEFAULT): ProtoChangeDetector {
return new DynamicProtoChangeDetector(this.registry, bindingRecords, variableBindings,
directiveRecords, changeControlStrategy);
createProtoChangeDetector(definition:ChangeDetectorDefinition): ProtoChangeDetector {
return new DynamicProtoChangeDetector(this.registry, definition);
}
}
@ -79,11 +76,8 @@ export class DynamicChangeDetection extends ChangeDetection {
export class JitChangeDetection extends ChangeDetection {
constructor(public registry: PipeRegistry) { super(); }
createProtoChangeDetector(name: string, bindingRecords: List<BindingRecord>,
variableBindings: List<string>, directiveRecords: List<DirectiveRecord>,
changeControlStrategy: string = DEFAULT): ProtoChangeDetector {
return new JitProtoChangeDetector(this.registry, bindingRecords, variableBindings,
directiveRecords, changeControlStrategy);
createProtoChangeDetector(definition:ChangeDetectorDefinition): ProtoChangeDetector {
return new JitProtoChangeDetector(this.registry, definition);
}
}

View File

@ -1,7 +1,7 @@
import {List} from 'angular2/src/facade/collection';
import {Locals} from './parser/locals';
import {DEFAULT} from './constants';
import {BindingRecord} from './binding_record';
import {DirectiveRecord} from './directive_record';
// HACK: workaround for Traceur behavior.
// It expects all transpiled modules to contain this marker.
@ -38,9 +38,7 @@ export class ProtoChangeDetector {
* @exportedAs angular2/change_detection
*/
export class ChangeDetection {
createProtoChangeDetector(name: string, bindingRecords: List<any>, variableBindings: List<any>,
directiveRecords: List<any>,
changeControlStrategy: string = DEFAULT): ProtoChangeDetector {
createProtoChangeDetector(definition: ChangeDetectorDefinition): ProtoChangeDetector {
return null;
}
}
@ -65,3 +63,9 @@ export class ChangeDetector {
detectChanges() {}
checkNoChanges() {}
}
export class ChangeDetectorDefinition {
constructor(public id: string, public strategy: string, public variableNames: List<string>,
public bindingRecords: List<BindingRecord>,
public directiveRecords: List<DirectiveRecord>) {}
}

View File

@ -22,7 +22,12 @@ import {
PrefixNot
} from './parser/ast';
import {ChangeDispatcher, ChangeDetector, ProtoChangeDetector} from './interfaces';
import {
ChangeDispatcher,
ChangeDetector,
ProtoChangeDetector,
ChangeDetectorDefinition
} from './interfaces';
import {ChangeDetectionUtil} from './change_detection_util';
import {DynamicChangeDetector} from './dynamic_change_detector';
import {ChangeDetectorJITGenerator} from './change_detection_jit_generator';
@ -56,25 +61,21 @@ export var __esModule = true;
export class DynamicProtoChangeDetector extends ProtoChangeDetector {
_records: List<ProtoRecord>;
constructor(private _pipeRegistry: PipeRegistry, private _bindingRecords: List<any>,
private _variableBindings: List<any>, private _directiveRecords: List<any>,
private _changeControlStrategy: string) {
constructor(private _pipeRegistry: PipeRegistry, private definition: ChangeDetectorDefinition) {
super();
this._records = this._createRecords(definition);
}
instantiate(dispatcher: any) {
this._createRecordsIfNecessary();
return new DynamicChangeDetector(this._changeControlStrategy, dispatcher, this._pipeRegistry,
this._records, this._directiveRecords);
return new DynamicChangeDetector(this.definition.strategy, dispatcher, this._pipeRegistry,
this._records, this.definition.directiveRecords);
}
_createRecordsIfNecessary() {
if (isBlank(this._records)) {
var recordBuilder = new ProtoRecordBuilder();
ListWrapper.forEach(this._bindingRecords,
(b) => { recordBuilder.addAst(b, this._variableBindings); });
this._records = coalesce(recordBuilder.records);
}
_createRecords(definition: ChangeDetectorDefinition) {
var recordBuilder = new ProtoRecordBuilder();
ListWrapper.forEach(definition.bindingRecords,
(b) => { recordBuilder.addAst(b, definition.variableNames); });
return coalesce(recordBuilder.records);
}
}
@ -82,30 +83,23 @@ var _jitProtoChangeDetectorClassCounter: number = 0;
export class JitProtoChangeDetector extends ProtoChangeDetector {
_factory: Function;
constructor(private _pipeRegistry, private _bindingRecords: List<any>,
private _variableBindings: List<any>, private _directiveRecords: List<any>,
private _changeControlStrategy: string) {
constructor(private _pipeRegistry, private definition: ChangeDetectorDefinition) {
super();
this._factory = null;
this._factory = this._createFactory(definition);
}
instantiate(dispatcher: any) {
this._createFactoryIfNecessary();
return this._factory(dispatcher, this._pipeRegistry);
}
instantiate(dispatcher: any) { return this._factory(dispatcher, this._pipeRegistry); }
_createFactoryIfNecessary() {
if (isBlank(this._factory)) {
var recordBuilder = new ProtoRecordBuilder();
ListWrapper.forEach(this._bindingRecords,
(b) => { recordBuilder.addAst(b, this._variableBindings); });
var c = _jitProtoChangeDetectorClassCounter++;
var records = coalesce(recordBuilder.records);
var typeName = `ChangeDetector${c}`;
this._factory = new ChangeDetectorJITGenerator(typeName, this._changeControlStrategy, records,
this._directiveRecords)
.generate();
}
_createFactory(definition: ChangeDetectorDefinition) {
var recordBuilder = new ProtoRecordBuilder();
ListWrapper.forEach(definition.bindingRecords,
(b) => { recordBuilder.addAst(b, definition.variableNames); });
var c = _jitProtoChangeDetectorClassCounter++;
var records = coalesce(recordBuilder.records);
var typeName = `ChangeDetector${c}`;
return new ChangeDetectorJITGenerator(typeName, definition.strategy, records,
this.definition.directiveRecords)
.generate();
}
}
@ -114,13 +108,13 @@ class ProtoRecordBuilder {
constructor() { this.records = []; }
addAst(b: BindingRecord, variableBindings: List < any >= null) {
addAst(b: BindingRecord, variableNames: List < string >= null) {
var last = ListWrapper.last(this.records);
if (isPresent(last) && last.bindingRecord.directiveRecord == b.directiveRecord) {
last.lastInDirective = false;
}
var pr = _ConvertAstIntoProtoRecords.convert(b, this.records.length, variableBindings);
var pr = _ConvertAstIntoProtoRecords.convert(b, this.records.length, variableNames);
if (!ListWrapper.isEmpty(pr)) {
var last = ListWrapper.last(pr);
last.lastInBinding = true;
@ -135,12 +129,12 @@ class _ConvertAstIntoProtoRecords {
protoRecords: List<any>;
constructor(private bindingRecord: BindingRecord, private contextIndex: number,
private expressionAsString: string, private variableBindings: List<any>) {
private expressionAsString: string, private variableNames: List<any>) {
this.protoRecords = [];
}
static convert(b: BindingRecord, contextIndex: number, variableBindings: List<any>) {
var c = new _ConvertAstIntoProtoRecords(b, contextIndex, b.ast.toString(), variableBindings);
static convert(b: BindingRecord, contextIndex: number, variableNames: List<any>) {
var c = new _ConvertAstIntoProtoRecords(b, contextIndex, b.ast.toString(), variableNames);
b.ast.visit(c);
return c.protoRecords;
}
@ -159,7 +153,7 @@ class _ConvertAstIntoProtoRecords {
visitAccessMember(ast: AccessMember) {
var receiver = ast.receiver.visit(this);
if (isPresent(this.variableBindings) && ListWrapper.contains(this.variableBindings, ast.name) &&
if (isPresent(this.variableNames) && ListWrapper.contains(this.variableNames, ast.name) &&
ast.receiver instanceof
ImplicitReceiver) {
return this._addRecord(RECORD_TYPE_LOCAL, ast.name, ast.name, [], null, receiver);
@ -172,7 +166,7 @@ class _ConvertAstIntoProtoRecords {
;
var receiver = ast.receiver.visit(this);
var args = this._visitAll(ast.args);
if (isPresent(this.variableBindings) && ListWrapper.contains(this.variableBindings, ast.name)) {
if (isPresent(this.variableNames) && ListWrapper.contains(this.variableNames, ast.name)) {
var target = this._addRecord(RECORD_TYPE_LOCAL, ast.name, ast.name, [], null, receiver);
return this._addRecord(RECORD_TYPE_INVOKE_CLOSURE, "closure", null, args, null, target);
} else {

View File

@ -4,7 +4,7 @@ import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {reflector} from 'angular2/src/reflection/reflection';
import {ChangeDetection, DirectiveIndex, BindingRecord, DirectiveRecord, ProtoChangeDetector} from 'angular2/change_detection';
import {ChangeDetection, DirectiveIndex, BindingRecord, DirectiveRecord, ProtoChangeDetector, ChangeDetectorDefinition} from 'angular2/change_detection';
import {Component} from '../annotations_impl/annotations';
import * as renderApi from 'angular2/src/render/api';
@ -171,13 +171,8 @@ export class ProtoViewFactory {
name = 'dummy';
}
return this._changeDetection.createProtoChangeDetector(
name,
bindingRecords,
variableNames,
directiveRecords,
changeDetection
);
var definition = new ChangeDetectorDefinition(name, changeDetection, variableNames, bindingRecords, directiveRecords);
return this._changeDetection.createProtoChangeDetector(definition);
}
_createElementBinders(protoView, elementBinders, sortedDirectives) {