Skip to content

Commit d8c7c27

Browse files
committed
refactor(change_detector): extracted ChangeDetectorDefinition
1 parent fadabf7 commit d8c7c27

File tree

8 files changed

+1061
-1052
lines changed

8 files changed

+1061
-1052
lines changed

modules/angular2/change_detection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export {
2626
ProtoChangeDetector,
2727
ChangeDispatcher,
2828
ChangeDetector,
29-
ChangeDetection
29+
ChangeDetection,
30+
ChangeDetectorDefinition
3031
} from './src/change_detection/interfaces';
3132
export {
3233
CHECK_ONCE,

modules/angular2/src/change_detection/change_detection.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {NullPipeFactory} from './pipes/null_pipe';
99
import {BindingRecord} from './binding_record';
1010
import {DirectiveRecord} from './directive_record';
1111
import {DEFAULT} from './constants';
12-
import {ChangeDetection, ProtoChangeDetector} from './interfaces';
12+
import {ChangeDetection, ProtoChangeDetector, ChangeDetectorDefinition} from './interfaces';
1313
import {Injectable} from 'angular2/src/di/decorators';
1414
import {List} from 'angular2/src/facade/collection';
1515

@@ -59,11 +59,8 @@ export var defaultPipes = {
5959
export class DynamicChangeDetection extends ChangeDetection {
6060
constructor(public registry: PipeRegistry) { super(); }
6161

62-
createProtoChangeDetector(name: string, bindingRecords: List<BindingRecord>,
63-
variableBindings: List<string>, directiveRecords: List<DirectiveRecord>,
64-
changeControlStrategy: string = DEFAULT): ProtoChangeDetector {
65-
return new DynamicProtoChangeDetector(this.registry, bindingRecords, variableBindings,
66-
directiveRecords, changeControlStrategy);
62+
createProtoChangeDetector(definition:ChangeDetectorDefinition): ProtoChangeDetector {
63+
return new DynamicProtoChangeDetector(this.registry, definition);
6764
}
6865
}
6966

@@ -79,11 +76,8 @@ export class DynamicChangeDetection extends ChangeDetection {
7976
export class JitChangeDetection extends ChangeDetection {
8077
constructor(public registry: PipeRegistry) { super(); }
8178

82-
createProtoChangeDetector(name: string, bindingRecords: List<BindingRecord>,
83-
variableBindings: List<string>, directiveRecords: List<DirectiveRecord>,
84-
changeControlStrategy: string = DEFAULT): ProtoChangeDetector {
85-
return new JitProtoChangeDetector(this.registry, bindingRecords, variableBindings,
86-
directiveRecords, changeControlStrategy);
79+
createProtoChangeDetector(definition:ChangeDetectorDefinition): ProtoChangeDetector {
80+
return new JitProtoChangeDetector(this.registry, definition);
8781
}
8882
}
8983

modules/angular2/src/change_detection/interfaces.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {List} from 'angular2/src/facade/collection';
22
import {Locals} from './parser/locals';
3-
import {DEFAULT} from './constants';
43
import {BindingRecord} from './binding_record';
4+
import {DirectiveRecord} from './directive_record';
55

66
// HACK: workaround for Traceur behavior.
77
// It expects all transpiled modules to contain this marker.
@@ -38,9 +38,7 @@ export class ProtoChangeDetector {
3838
* @exportedAs angular2/change_detection
3939
*/
4040
export class ChangeDetection {
41-
createProtoChangeDetector(name: string, bindingRecords: List<any>, variableBindings: List<any>,
42-
directiveRecords: List<any>,
43-
changeControlStrategy: string = DEFAULT): ProtoChangeDetector {
41+
createProtoChangeDetector(definition: ChangeDetectorDefinition): ProtoChangeDetector {
4442
return null;
4543
}
4644
}
@@ -65,3 +63,9 @@ export class ChangeDetector {
6563
detectChanges() {}
6664
checkNoChanges() {}
6765
}
66+
67+
export class ChangeDetectorDefinition {
68+
constructor(public id: string, public strategy: string, public variableNames: List<string>,
69+
public bindingRecords: List<BindingRecord>,
70+
public directiveRecords: List<DirectiveRecord>) {}
71+
}

modules/angular2/src/change_detection/proto_change_detector.ts

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import {
2222
PrefixNot
2323
} from './parser/ast';
2424

25-
import {ChangeDispatcher, ChangeDetector, ProtoChangeDetector} from './interfaces';
25+
import {
26+
ChangeDispatcher,
27+
ChangeDetector,
28+
ProtoChangeDetector,
29+
ChangeDetectorDefinition
30+
} from './interfaces';
2631
import {ChangeDetectionUtil} from './change_detection_util';
2732
import {DynamicChangeDetector} from './dynamic_change_detector';
2833
import {ChangeDetectorJITGenerator} from './change_detection_jit_generator';
@@ -56,56 +61,45 @@ export var __esModule = true;
5661
export class DynamicProtoChangeDetector extends ProtoChangeDetector {
5762
_records: List<ProtoRecord>;
5863

59-
constructor(private _pipeRegistry: PipeRegistry, private _bindingRecords: List<any>,
60-
private _variableBindings: List<any>, private _directiveRecords: List<any>,
61-
private _changeControlStrategy: string) {
64+
constructor(private _pipeRegistry: PipeRegistry, private definition: ChangeDetectorDefinition) {
6265
super();
66+
this._records = this._createRecords(definition);
6367
}
6468

6569
instantiate(dispatcher: any) {
66-
this._createRecordsIfNecessary();
67-
return new DynamicChangeDetector(this._changeControlStrategy, dispatcher, this._pipeRegistry,
68-
this._records, this._directiveRecords);
70+
return new DynamicChangeDetector(this.definition.strategy, dispatcher, this._pipeRegistry,
71+
this._records, this.definition.directiveRecords);
6972
}
7073

71-
_createRecordsIfNecessary() {
72-
if (isBlank(this._records)) {
73-
var recordBuilder = new ProtoRecordBuilder();
74-
ListWrapper.forEach(this._bindingRecords,
75-
(b) => { recordBuilder.addAst(b, this._variableBindings); });
76-
this._records = coalesce(recordBuilder.records);
77-
}
74+
_createRecords(definition: ChangeDetectorDefinition) {
75+
var recordBuilder = new ProtoRecordBuilder();
76+
ListWrapper.forEach(definition.bindingRecords,
77+
(b) => { recordBuilder.addAst(b, definition.variableNames); });
78+
return coalesce(recordBuilder.records);
7879
}
7980
}
8081

8182
var _jitProtoChangeDetectorClassCounter: number = 0;
8283
export class JitProtoChangeDetector extends ProtoChangeDetector {
8384
_factory: Function;
8485

85-
constructor(private _pipeRegistry, private _bindingRecords: List<any>,
86-
private _variableBindings: List<any>, private _directiveRecords: List<any>,
87-
private _changeControlStrategy: string) {
86+
constructor(private _pipeRegistry, private definition: ChangeDetectorDefinition) {
8887
super();
89-
this._factory = null;
90-
}
91-
92-
instantiate(dispatcher: any) {
93-
this._createFactoryIfNecessary();
94-
return this._factory(dispatcher, this._pipeRegistry);
88+
this._factory = this._createFactory(definition);
9589
}
9690

97-
_createFactoryIfNecessary() {
98-
if (isBlank(this._factory)) {
99-
var recordBuilder = new ProtoRecordBuilder();
100-
ListWrapper.forEach(this._bindingRecords,
101-
(b) => { recordBuilder.addAst(b, this._variableBindings); });
102-
var c = _jitProtoChangeDetectorClassCounter++;
103-
var records = coalesce(recordBuilder.records);
104-
var typeName = `ChangeDetector${c}`;
105-
this._factory = new ChangeDetectorJITGenerator(typeName, this._changeControlStrategy, records,
106-
this._directiveRecords)
107-
.generate();
108-
}
91+
instantiate(dispatcher: any) { return this._factory(dispatcher, this._pipeRegistry); }
92+
93+
_createFactory(definition: ChangeDetectorDefinition) {
94+
var recordBuilder = new ProtoRecordBuilder();
95+
ListWrapper.forEach(definition.bindingRecords,
96+
(b) => { recordBuilder.addAst(b, definition.variableNames); });
97+
var c = _jitProtoChangeDetectorClassCounter++;
98+
var records = coalesce(recordBuilder.records);
99+
var typeName = `ChangeDetector${c}`;
100+
return new ChangeDetectorJITGenerator(typeName, definition.strategy, records,
101+
this.definition.directiveRecords)
102+
.generate();
109103
}
110104
}
111105

@@ -114,13 +108,13 @@ class ProtoRecordBuilder {
114108

115109
constructor() { this.records = []; }
116110

117-
addAst(b: BindingRecord, variableBindings: List < any >= null) {
111+
addAst(b: BindingRecord, variableNames: List < string >= null) {
118112
var last = ListWrapper.last(this.records);
119113
if (isPresent(last) && last.bindingRecord.directiveRecord == b.directiveRecord) {
120114
last.lastInDirective = false;
121115
}
122116

123-
var pr = _ConvertAstIntoProtoRecords.convert(b, this.records.length, variableBindings);
117+
var pr = _ConvertAstIntoProtoRecords.convert(b, this.records.length, variableNames);
124118
if (!ListWrapper.isEmpty(pr)) {
125119
var last = ListWrapper.last(pr);
126120
last.lastInBinding = true;
@@ -135,12 +129,12 @@ class _ConvertAstIntoProtoRecords {
135129
protoRecords: List<any>;
136130

137131
constructor(private bindingRecord: BindingRecord, private contextIndex: number,
138-
private expressionAsString: string, private variableBindings: List<any>) {
132+
private expressionAsString: string, private variableNames: List<any>) {
139133
this.protoRecords = [];
140134
}
141135

142-
static convert(b: BindingRecord, contextIndex: number, variableBindings: List<any>) {
143-
var c = new _ConvertAstIntoProtoRecords(b, contextIndex, b.ast.toString(), variableBindings);
136+
static convert(b: BindingRecord, contextIndex: number, variableNames: List<any>) {
137+
var c = new _ConvertAstIntoProtoRecords(b, contextIndex, b.ast.toString(), variableNames);
144138
b.ast.visit(c);
145139
return c.protoRecords;
146140
}
@@ -159,7 +153,7 @@ class _ConvertAstIntoProtoRecords {
159153

160154
visitAccessMember(ast: AccessMember) {
161155
var receiver = ast.receiver.visit(this);
162-
if (isPresent(this.variableBindings) && ListWrapper.contains(this.variableBindings, ast.name) &&
156+
if (isPresent(this.variableNames) && ListWrapper.contains(this.variableNames, ast.name) &&
163157
ast.receiver instanceof
164158
ImplicitReceiver) {
165159
return this._addRecord(RECORD_TYPE_LOCAL, ast.name, ast.name, [], null, receiver);
@@ -172,7 +166,7 @@ class _ConvertAstIntoProtoRecords {
172166
;
173167
var receiver = ast.receiver.visit(this);
174168
var args = this._visitAll(ast.args);
175-
if (isPresent(this.variableBindings) && ListWrapper.contains(this.variableBindings, ast.name)) {
169+
if (isPresent(this.variableNames) && ListWrapper.contains(this.variableNames, ast.name)) {
176170
var target = this._addRecord(RECORD_TYPE_LOCAL, ast.name, ast.name, [], null, receiver);
177171
return this._addRecord(RECORD_TYPE_INVOKE_CLOSURE, "closure", null, args, null, target);
178172
} else {

modules/angular2/src/core/compiler/proto_view_factory.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
44
import {isPresent, isBlank} from 'angular2/src/facade/lang';
55
import {reflector} from 'angular2/src/reflection/reflection';
66

7-
import {ChangeDetection, DirectiveIndex, BindingRecord, DirectiveRecord, ProtoChangeDetector} from 'angular2/change_detection';
7+
import {ChangeDetection, DirectiveIndex, BindingRecord, DirectiveRecord, ProtoChangeDetector, ChangeDetectorDefinition} from 'angular2/change_detection';
88
import {Component} from '../annotations_impl/annotations';
99

1010
import * as renderApi from 'angular2/src/render/api';
@@ -171,13 +171,8 @@ export class ProtoViewFactory {
171171
name = 'dummy';
172172
}
173173

174-
return this._changeDetection.createProtoChangeDetector(
175-
name,
176-
bindingRecords,
177-
variableNames,
178-
directiveRecords,
179-
changeDetection
180-
);
174+
var definition = new ChangeDetectorDefinition(name, changeDetection, variableNames, bindingRecords, directiveRecords);
175+
return this._changeDetection.createProtoChangeDetector(definition);
181176
}
182177

183178
_createElementBinders(protoView, elementBinders, sortedDirectives) {

0 commit comments

Comments
 (0)