Skip to content

Commit d84993f

Browse files
author
Tim Blasi
committed
refactor(change_detect): Move (de)hydrate logic into dedicated methods
Call new `(de)hydrateDirectives` methods from `(de)hydrate`. Add a null implementation in `AbstractChangeDetector` and only override if necessary for the specific change detector. Update to angular#3248
1 parent a9efc48 commit d84993f

4 files changed

Lines changed: 98 additions & 35 deletions

File tree

modules/angular2/src/change_detection/abstract_change_detector.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ export class AbstractChangeDetector implements ChangeDetector {
6060

6161
hydrate(context: any, locals: Locals, directives: any, pipes: any): void {}
6262

63+
hydrateDirectives(directives: any): void {}
64+
6365
dehydrate(): void {}
6466

67+
dehydrateDirectives(destroyPipes: boolean): void {}
68+
6569
callOnAllChangesDone(): void {}
6670

6771
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
@@ -95,4 +99,4 @@ export class AbstractChangeDetector implements ChangeDetector {
9599
null;
96100
throw new ChangeDetectionError(proto, exception, stack, context);
97101
}
98-
}
102+
}

modules/angular2/src/change_detection/change_detection_jit_generator.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,31 @@ var ALREADY_CHECKED_ACCESSOR = "this.alreadyChecked";
3333

3434
export class ChangeDetectorJITGenerator {
3535
_names: CodegenNameUtil;
36+
_typeName: string;
3637

3738
constructor(public id: string, public changeDetectionStrategy: string,
3839
public records: List<ProtoRecord>, public directiveRecords: List<any>,
3940
private generateCheckNoChanges: boolean) {
4041
this._names = new CodegenNameUtil(this.records, this.directiveRecords, 'this._', UTIL);
42+
this._typeName = sanitizeName(`ChangeDetector_${this.id}`);
4143
}
4244

4345
generate(): Function {
44-
var typeName = sanitizeName(`ChangeDetector_${this.id}`);
4546
var classDefinition = `
46-
var ${typeName} = function ${typeName}(dispatcher, protos, directiveRecords) {
47+
var ${this._typeName} = function ${this._typeName}(dispatcher, protos, directiveRecords) {
4748
${ABSTRACT_CHANGE_DETECTOR}.call(this, ${JSON.stringify(this.id)}, dispatcher);
4849
${PROTOS_ACCESSOR} = protos;
4950
${DIRECTIVES_ACCESSOR} = directiveRecords;
5051
${LOCALS_ACCESSOR} = null;
5152
${CURRENT_PROTO} = null;
5253
${PIPES_ACCESSOR} = null;
5354
${ALREADY_CHECKED_ACCESSOR} = false;
54-
${this._names.genDehydrateFields()}
55+
this.dehydrateDirectives(false);
5556
}
5657
57-
${typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype);
58+
${this._typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype);
5859
59-
${typeName}.prototype.detectChangesInRecords = function(throwOnChange) {
60+
${this._typeName}.prototype.detectChangesInRecords = function(throwOnChange) {
6061
if (!this.hydrated()) {
6162
${UTIL}.throwDehydrated();
6263
}
@@ -67,7 +68,7 @@ export class ChangeDetectorJITGenerator {
6768
}
6869
}
6970
70-
${typeName}.prototype.__detectChangesInRecords = function(throwOnChange) {
71+
${this._typeName}.prototype.__detectChangesInRecords = function(throwOnChange) {
7172
${CURRENT_PROTO} = null;
7273
7374
${this._names.genInitLocals()}
@@ -81,35 +82,37 @@ export class ChangeDetectorJITGenerator {
8182
${ALREADY_CHECKED_ACCESSOR} = true;
8283
}
8384
84-
${this._genCheckNoChanges(typeName)}
85+
${this._genCheckNoChanges()}
8586
86-
${typeName}.prototype.callOnAllChangesDone = function() {
87+
${this._typeName}.prototype.callOnAllChangesDone = function() {
8788
${this._genCallOnAllChangesDoneBody()}
8889
}
8990
90-
${typeName}.prototype.hydrate = function(context, locals, directives, pipes) {
91+
${this._typeName}.prototype.hydrate = function(context, locals, directives, pipes) {
9192
${MODE_ACCESSOR} = "${ChangeDetectionUtil.changeDetectionMode(this.changeDetectionStrategy)}";
9293
${this._names.getContextName()} = context;
9394
${LOCALS_ACCESSOR} = locals;
94-
${this._genHydrateDirectives()}
95-
${this._genHydrateDetectors()}
95+
this.hydrateDirectives(directives);
9696
${PIPES_ACCESSOR} = pipes;
9797
${ALREADY_CHECKED_ACCESSOR} = false;
9898
}
9999
100-
${typeName}.prototype.dehydrate = function() {
101-
${this._names.genPipeOnDestroy()}
102-
${this._names.genDehydrateFields()}
100+
${this._maybeGenHydrateDirectives()}
101+
102+
${this._typeName}.prototype.dehydrate = function() {
103+
this.dehydrateDirectives(true);
103104
${LOCALS_ACCESSOR} = null;
104105
${PIPES_ACCESSOR} = null;
105106
}
106107
107-
${typeName}.prototype.hydrated = function() {
108+
${this._maybeGenDehydrateDirectives()}
109+
110+
${this._typeName}.prototype.hydrated = function() {
108111
return ${this._names.getContextName()} !== null;
109112
}
110113
111114
return function(dispatcher) {
112-
return new ${typeName}(dispatcher, protos, directiveRecords);
115+
return new ${this._typeName}(dispatcher, protos, directiveRecords);
113116
}
114117
`;
115118

@@ -118,6 +121,29 @@ export class ChangeDetectorJITGenerator {
118121
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
119122
}
120123

124+
_maybeGenDehydrateDirectives(): string {
125+
var destroyPipesCode = this._names.genPipeOnDestroy();
126+
if (destroyPipesCode) {
127+
destroyPipesCode = `if (destroyPipes) { ${destroyPipesCode} }`;
128+
}
129+
var dehydrateFieldsCode = this._names.genDehydrateFields();
130+
if (!destroyPipesCode && !dehydrateFieldsCode) return '';
131+
return `${this._typeName}.prototype.dehydrateDirectives = function(destroyPipes) {
132+
${destroyPipesCode}
133+
${dehydrateFieldsCode}
134+
}`;
135+
}
136+
137+
_maybeGenHydrateDirectives(): string {
138+
var hydrateDirectivesCode = this._genHydrateDirectives();
139+
var hydrateDetectorsCode = this._genHydrateDetectors();
140+
if (!hydrateDirectivesCode && !hydrateDetectorsCode) return '';
141+
return `${this._typeName}.prototype.hydrateDirectives = function(directives) {
142+
${hydrateDirectivesCode}
143+
${hydrateDetectorsCode}
144+
}`;
145+
}
146+
121147
_genHydrateDirectives(): string {
122148
var directiveFieldNames = this._names.getAllDirectiveNames();
123149
var lines = ListWrapper.createFixedSize(directiveFieldNames.length);
@@ -345,9 +371,9 @@ export class ChangeDetectorJITGenerator {
345371
}
346372
}
347373

348-
_genCheckNoChanges(typeName: string): string {
374+
_genCheckNoChanges(): string {
349375
if (this.generateCheckNoChanges) {
350-
return `${typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
376+
return `${this._typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
351377
} else {
352378
return '';
353379
}

modules/angular2/src/transform/template_compiler/change_detector_codegen.dart

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ class _CodegenState {
115115
this.$_PROTOS_ACCESSOR,
116116
this.$_DIRECTIVES_ACCESSOR)
117117
: super(${_encodeValue(_changeDetectorDefId)}, $_DISPATCHER_ACCESSOR) {
118-
${_names.genDehydrateFields()}
118+
dehydrateDirectives(false);
119119
}
120120
121121
void detectChangesInRecords(throwOnChange) {
122122
if (!hydrated()) {
123123
$_UTIL.throwDehydrated();
124124
}
125125
try {
126-
this.__detectChangesInRecords(throwOnChange);
126+
__detectChangesInRecords(throwOnChange);
127127
} catch (e, s) {
128-
this.throwError($_CURRENT_PROTO, e, s);
128+
throwError($_CURRENT_PROTO, e, s);
129129
}
130130
}
131131
@@ -151,19 +151,21 @@ class _CodegenState {
151151
$_MODE_ACCESSOR = '$_changeDetectionMode';
152152
${_names.getContextName()} = context;
153153
$_LOCALS_ACCESSOR = locals;
154-
${_genHydrateDirectives()}
155-
${_genHydrateDetectors()}
154+
hydrateDirectives(directives);
156155
$_ALREADY_CHECKED_ACCESSOR = false;
157156
$_PIPES_ACCESSOR = pipes;
158157
}
159158
159+
${_maybeGenHydrateDirectives()}
160+
160161
void dehydrate() {
161-
${_names.genPipeOnDestroy()}
162-
${_names.genDehydrateFields()}
162+
dehydrateDirectives(true);
163163
$_LOCALS_ACCESSOR = null;
164164
$_PIPES_ACCESSOR = null;
165165
}
166166
167+
${_maybeGenDehydrateDirectives()}
168+
167169
hydrated() => ${_names.getContextName()} != null;
168170
169171
static $_GEN_PREFIX.ProtoChangeDetector
@@ -184,6 +186,34 @@ class _CodegenState {
184186
''');
185187
}
186188

189+
String _maybeGenDehydrateDirectives() {
190+
var destroyPipesParamName = 'destroyPipes';
191+
var destroyPipesCode = _names.genPipeOnDestroy();
192+
if (destroyPipesCode.isNotEmpty) {
193+
destroyPipesCode = 'if (${destroyPipesParamName}) { '
194+
'${destroyPipesCode}'
195+
'}';
196+
}
197+
var dehydrateFieldsCode = _names.genDehydrateFields();
198+
if (destroyPipesCode.isEmpty && dehydrateFieldsCode.isEmpty) return '';
199+
return 'void dehydrateDirectives(${destroyPipesParamName}) {'
200+
'${destroyPipesCode}'
201+
'${dehydrateFieldsCode}'
202+
'}';
203+
}
204+
205+
String _maybeGenHydrateDirectives() {
206+
var hydrateDirectivesCode = _genHydrateDirectives();
207+
var hydrateDetectorsCode = _genHydrateDetectors();
208+
if (hydrateDirectivesCode.isEmpty && hydrateDetectorsCode.isEmpty) {
209+
return '';
210+
}
211+
return 'void hydrateDirectives(directives) { '
212+
'$hydrateDirectivesCode'
213+
'$hydrateDetectorsCode'
214+
'}';
215+
}
216+
187217
String _genHydrateDirectives() {
188218
var buf = new StringBuffer();
189219
var directiveFieldNames = _names.getAllDirectiveNames();
@@ -412,7 +442,7 @@ class _CodegenState {
412442

413443
String _genCheckNoChanges() {
414444
if (this._generateCheckNoChanges) {
415-
return 'void checkNoChanges() { this.runDetectChanges(true); }';
445+
return 'void checkNoChanges() { runDetectChanges(true); }';
416446
} else {
417447
return '';
418448
}

modules/angular2/test/transform/integration/two_annotations_files/expected/bar.ng_deps.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ class _MyComponent_ChangeDetector0 extends _gen.AbstractChangeDetector {
3636
_MyComponent_ChangeDetector0(
3737
dynamic dispatcher, this._protos, this._directiveRecords)
3838
: super("MyComponent_comp_0", dispatcher) {
39-
_context = null;
40-
_myNum0 = _interpolate1 = _gen.ChangeDetectionUtil.uninitialized;
39+
dehydrateDirectives(false);
4140
}
4241

4342
void detectChangesInRecords(throwOnChange) {
4443
if (!hydrated()) {
4544
_gen.ChangeDetectionUtil.throwDehydrated();
4645
}
4746
try {
48-
this.__detectChangesInRecords(throwOnChange);
47+
__detectChangesInRecords(throwOnChange);
4948
} catch (e, s) {
50-
this.throwError(currentProto, e, s);
49+
throwError(currentProto, e, s);
5150
}
5251
}
5352

@@ -92,7 +91,7 @@ class _MyComponent_ChangeDetector0 extends _gen.AbstractChangeDetector {
9291
}
9392

9493
void checkNoChanges() {
95-
this.runDetectChanges(true);
94+
runDetectChanges(true);
9695
}
9796

9897
void callOnAllChangesDone() {
@@ -103,18 +102,22 @@ class _MyComponent_ChangeDetector0 extends _gen.AbstractChangeDetector {
103102
mode = 'ALWAYS_CHECK';
104103
_context = context;
105104
_locals = locals;
106-
105+
hydrateDirectives(directives);
107106
_alreadyChecked = false;
108107
_pipes = pipes;
109108
}
110109

111110
void dehydrate() {
112-
_context = null;
113-
_myNum0 = _interpolate1 = _gen.ChangeDetectionUtil.uninitialized;
111+
dehydrateDirectives(true);
114112
_locals = null;
115113
_pipes = null;
116114
}
117115

116+
void dehydrateDirectives(destroyPipes) {
117+
_context = null;
118+
_myNum0 = _interpolate1 = _gen.ChangeDetectionUtil.uninitialized;
119+
}
120+
118121
hydrated() => _context != null;
119122

120123
static _gen.ProtoChangeDetector newProtoChangeDetector(

0 commit comments

Comments
 (0)