1
- import { BaseException , Type , isBlank , isPresent } from 'angular2/src/core/facade/lang' ;
1
+ import {
2
+ BaseException ,
3
+ Type ,
4
+ isBlank ,
5
+ isPresent ,
6
+ StringWrapper
7
+ } from 'angular2/src/core/facade/lang' ;
2
8
import { ListWrapper , MapWrapper , StringMapWrapper } from 'angular2/src/core/facade/collection' ;
3
9
4
10
import { AbstractChangeDetector } from './abstract_change_detector' ;
@@ -11,10 +17,9 @@ import {CodegenLogicUtil} from './codegen_logic_util';
11
17
import { codify } from './codegen_facade' ;
12
18
import { EventBinding } from './event_binding' ;
13
19
import { BindingTarget } from './binding_record' ;
14
- import { ChangeDetectorGenConfig } from './interfaces' ;
20
+ import { ChangeDetectorGenConfig , ChangeDetectorDefinition } from './interfaces' ;
15
21
import { ChangeDetectionStrategy } from './constants' ;
16
-
17
-
22
+ import { createPropertyRecords , createEventRecords } from './proto_change_detector' ;
18
23
19
24
/**
20
25
* The code generator takes a list of proto records and creates a function/class
@@ -25,39 +30,65 @@ import {ChangeDetectionStrategy} from './constants';
25
30
* `angular2.transform.template_compiler.change_detector_codegen` library. If you make updates
26
31
* here, please make equivalent changes there.
27
32
*/
28
- const ABSTRACT_CHANGE_DETECTOR = "AbstractChangeDetector" ;
29
- const UTIL = "ChangeDetectionUtil" ;
30
33
const IS_CHANGED_LOCAL = "isChanged" ;
31
34
const CHANGES_LOCAL = "changes" ;
32
35
33
36
export class ChangeDetectorJITGenerator {
34
- _logic : CodegenLogicUtil ;
35
- _names : CodegenNameUtil ;
36
- _typeName : string ;
37
-
38
- constructor ( private id : string , private changeDetectionStrategy : ChangeDetectionStrategy ,
39
- private records : ProtoRecord [ ] , private propertyBindingTargets : BindingTarget [ ] ,
40
- private eventBindings : EventBinding [ ] , private directiveRecords : any [ ] ,
41
- private genConfig : ChangeDetectorGenConfig ) {
42
- this . _names =
43
- new CodegenNameUtil ( this . records , this . eventBindings , this . directiveRecords , UTIL ) ;
44
- this . _logic = new CodegenLogicUtil ( this . _names , UTIL , changeDetectionStrategy ) ;
45
- this . _typeName = sanitizeName ( `ChangeDetector_${ this . id } ` ) ;
37
+ private _logic : CodegenLogicUtil ;
38
+ private _names : CodegenNameUtil ;
39
+ private id : string ;
40
+ private changeDetectionStrategy : ChangeDetectionStrategy ;
41
+ private records : ProtoRecord [ ] ;
42
+ private propertyBindingTargets : BindingTarget [ ] ;
43
+ private eventBindings : EventBinding [ ] ;
44
+ private directiveRecords : any [ ] ;
45
+ private genConfig : ChangeDetectorGenConfig ;
46
+ typeName : string ;
47
+
48
+ constructor ( definition : ChangeDetectorDefinition , private changeDetectionUtilVarName : string ,
49
+ private abstractChangeDetectorVarName : string ) {
50
+ var propertyBindingRecords = createPropertyRecords ( definition ) ;
51
+ var eventBindingRecords = createEventRecords ( definition ) ;
52
+ var propertyBindingTargets = definition . bindingRecords . map ( b => b . target ) ;
53
+ this . id = definition . id ;
54
+ this . changeDetectionStrategy = definition . strategy ;
55
+ this . genConfig = definition . genConfig ;
56
+
57
+ this . records = propertyBindingRecords ;
58
+ this . propertyBindingTargets = propertyBindingTargets ;
59
+ this . eventBindings = eventBindingRecords ;
60
+ this . directiveRecords = definition . directiveRecords ;
61
+ this . _names = new CodegenNameUtil ( this . records , this . eventBindings , this . directiveRecords ,
62
+ this . changeDetectionUtilVarName ) ;
63
+ this . _logic = new CodegenLogicUtil ( this . _names , this . changeDetectionUtilVarName ,
64
+ this . changeDetectionStrategy ) ;
65
+ this . typeName = sanitizeName ( `ChangeDetector_${ this . id } ` ) ;
46
66
}
47
67
48
68
generate ( ) : Function {
49
- var classDefinition = `
50
- var ${ this . _typeName } = function ${ this . _typeName } (dispatcher) {
51
- ${ ABSTRACT_CHANGE_DETECTOR } .call(
69
+ var factorySource = `
70
+ ${ this . generateSource ( ) }
71
+ return function(dispatcher) {
72
+ return new ${ this . typeName } (dispatcher);
73
+ }
74
+ ` ;
75
+ return new Function ( this . abstractChangeDetectorVarName , this . changeDetectionUtilVarName ,
76
+ factorySource ) ( AbstractChangeDetector , ChangeDetectionUtil ) ;
77
+ }
78
+
79
+ generateSource ( ) : string {
80
+ return `
81
+ var ${ this . typeName } = function ${ this . typeName } (dispatcher) {
82
+ ${ this . abstractChangeDetectorVarName } .call(
52
83
this, ${ JSON . stringify ( this . id ) } , dispatcher, ${ this . records . length } ,
53
- ${ this . _typeName } .gen_propertyBindingTargets, ${ this . _typeName } .gen_directiveIndices,
84
+ ${ this . typeName } .gen_propertyBindingTargets, ${ this . typeName } .gen_directiveIndices,
54
85
${ codify ( this . changeDetectionStrategy ) } );
55
86
this.dehydrateDirectives(false);
56
87
}
57
88
58
- ${ this . _typeName } .prototype = Object.create(${ ABSTRACT_CHANGE_DETECTOR } .prototype);
89
+ ${ this . typeName } .prototype = Object.create(${ this . abstractChangeDetectorVarName } .prototype);
59
90
60
- ${ this . _typeName } .prototype.detectChangesInRecordsInternal = function(throwOnChange) {
91
+ ${ this . typeName } .prototype.detectChangesInRecordsInternal = function(throwOnChange) {
61
92
${ this . _names . genInitLocals ( ) }
62
93
var ${ IS_CHANGED_LOCAL } = false;
63
94
var ${ CHANGES_LOCAL } = null;
@@ -80,31 +111,25 @@ export class ChangeDetectorJITGenerator {
80
111
${ this . _genPropertyBindingTargets ( ) }
81
112
82
113
${ this . _genDirectiveIndices ( ) }
83
-
84
- return function(dispatcher) {
85
- return new ${ this . _typeName } (dispatcher);
86
- }
87
114
` ;
88
- return new Function ( ABSTRACT_CHANGE_DETECTOR , UTIL , classDefinition ) ( AbstractChangeDetector ,
89
- ChangeDetectionUtil ) ;
90
115
}
91
116
92
117
_genPropertyBindingTargets ( ) : string {
93
118
var targets = this . _logic . genPropertyBindingTargets ( this . propertyBindingTargets ,
94
119
this . genConfig . genDebugInfo ) ;
95
- return `${ this . _typeName } .gen_propertyBindingTargets = ${ targets } ;` ;
120
+ return `${ this . typeName } .gen_propertyBindingTargets = ${ targets } ;` ;
96
121
}
97
122
98
123
_genDirectiveIndices ( ) : string {
99
124
var indices = this . _logic . genDirectiveIndices ( this . directiveRecords ) ;
100
- return `${ this . _typeName } .gen_directiveIndices = ${ indices } ;` ;
125
+ return `${ this . typeName } .gen_directiveIndices = ${ indices } ;` ;
101
126
}
102
127
103
128
_maybeGenHandleEventInternal ( ) : string {
104
129
if ( this . eventBindings . length > 0 ) {
105
130
var handlers = this . eventBindings . map ( eb => this . _genEventBinding ( eb ) ) . join ( "\n" ) ;
106
131
return `
107
- ${ this . _typeName } .prototype.handleEventInternal = function(eventName, elIndex, locals) {
132
+ ${ this . typeName } .prototype.handleEventInternal = function(eventName, elIndex, locals) {
108
133
var ${ this . _names . getPreventDefaultAccesor ( ) } = false;
109
134
${ this . _names . genInitEventLocals ( ) }
110
135
${ handlers }
@@ -156,7 +181,7 @@ export class ChangeDetectorJITGenerator {
156
181
}
157
182
var dehydrateFieldsCode = this . _names . genDehydrateFields ( ) ;
158
183
if ( ! destroyPipesCode && ! dehydrateFieldsCode ) return '' ;
159
- return `${ this . _typeName } .prototype.dehydrateDirectives = function(destroyPipes) {
184
+ return `${ this . typeName } .prototype.dehydrateDirectives = function(destroyPipes) {
160
185
${ destroyPipesCode }
161
186
${ dehydrateFieldsCode }
162
187
}` ;
@@ -166,7 +191,7 @@ export class ChangeDetectorJITGenerator {
166
191
var hydrateDirectivesCode = this . _logic . genHydrateDirectives ( this . directiveRecords ) ;
167
192
var hydrateDetectorsCode = this . _logic . genHydrateDetectors ( this . directiveRecords ) ;
168
193
if ( ! hydrateDirectivesCode && ! hydrateDetectorsCode ) return '' ;
169
- return `${ this . _typeName } .prototype.hydrateDirectives = function(directives) {
194
+ return `${ this . typeName } .prototype.hydrateDirectives = function(directives) {
170
195
${ hydrateDirectivesCode }
171
196
${ hydrateDetectorsCode }
172
197
}` ;
@@ -177,7 +202,7 @@ export class ChangeDetectorJITGenerator {
177
202
if ( notifications . length > 0 ) {
178
203
var directiveNotifications = notifications . join ( "\n" ) ;
179
204
return `
180
- ${ this . _typeName } .prototype.afterContentLifecycleCallbacksInternal = function() {
205
+ ${ this . typeName } .prototype.afterContentLifecycleCallbacksInternal = function() {
181
206
${ directiveNotifications }
182
207
}
183
208
` ;
@@ -191,7 +216,7 @@ export class ChangeDetectorJITGenerator {
191
216
if ( notifications . length > 0 ) {
192
217
var directiveNotifications = notifications . join ( "\n" ) ;
193
218
return `
194
- ${ this . _typeName } .prototype.afterViewLifecycleCallbacksInternal = function() {
219
+ ${ this . typeName } .prototype.afterViewLifecycleCallbacksInternal = function() {
195
220
${ directiveNotifications }
196
221
}
197
222
` ;
@@ -239,7 +264,7 @@ export class ChangeDetectorJITGenerator {
239
264
var pipeName = r . name ;
240
265
241
266
var init = `
242
- if (${ pipe } === ${ UTIL } .uninitialized) {
267
+ if (${ pipe } === ${ this . changeDetectionUtilVarName } .uninitialized) {
243
268
${ pipe } = ${ this . _names . getPipesAccessorName ( ) } .get('${ pipeName } ');
244
269
}
245
270
` ;
@@ -251,7 +276,7 @@ export class ChangeDetectorJITGenerator {
251
276
252
277
var check = `
253
278
if (${ oldValue } !== ${ newValue } ) {
254
- ${ newValue } = ${ UTIL } .unwrapValue(${ newValue } )
279
+ ${ newValue } = ${ this . changeDetectionUtilVarName } .unwrapValue(${ newValue } )
255
280
${ this . _genChangeMarker ( r ) }
256
281
${ this . _genUpdateDirectiveOrElement ( r ) }
257
282
${ this . _genAddToChanges ( r ) }
@@ -342,7 +367,7 @@ export class ChangeDetectorJITGenerator {
342
367
343
368
_genCheckNoChanges ( ) : string {
344
369
if ( this . genConfig . genCheckNoChanges ) {
345
- return `${ this . _typeName } .prototype.checkNoChanges = function() { this.runDetectChanges(true); }` ;
370
+ return `${ this . typeName } .prototype.checkNoChanges = function() { this.runDetectChanges(true); }` ;
346
371
} else {
347
372
return '' ;
348
373
}
0 commit comments