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' ;
28import { ListWrapper , MapWrapper , StringMapWrapper } from 'angular2/src/core/facade/collection' ;
39
410import { AbstractChangeDetector } from './abstract_change_detector' ;
@@ -11,10 +17,9 @@ import {CodegenLogicUtil} from './codegen_logic_util';
1117import { codify } from './codegen_facade' ;
1218import { EventBinding } from './event_binding' ;
1319import { BindingTarget } from './binding_record' ;
14- import { ChangeDetectorGenConfig } from './interfaces' ;
20+ import { ChangeDetectorGenConfig , ChangeDetectorDefinition } from './interfaces' ;
1521import { ChangeDetectionStrategy } from './constants' ;
16-
17-
22+ import { createPropertyRecords , createEventRecords } from './proto_change_detector' ;
1823
1924/**
2025 * The code generator takes a list of proto records and creates a function/class
@@ -25,39 +30,65 @@ import {ChangeDetectionStrategy} from './constants';
2530 * `angular2.transform.template_compiler.change_detector_codegen` library. If you make updates
2631 * here, please make equivalent changes there.
2732*/
28- const ABSTRACT_CHANGE_DETECTOR = "AbstractChangeDetector" ;
29- const UTIL = "ChangeDetectionUtil" ;
3033const IS_CHANGED_LOCAL = "isChanged" ;
3134const CHANGES_LOCAL = "changes" ;
3235
3336export 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 } ` ) ;
4666 }
4767
4868 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(
5283 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,
5485 ${ codify ( this . changeDetectionStrategy ) } );
5586 this.dehydrateDirectives(false);
5687 }
5788
58- ${ this . _typeName } .prototype = Object.create(${ ABSTRACT_CHANGE_DETECTOR } .prototype);
89+ ${ this . typeName } .prototype = Object.create(${ this . abstractChangeDetectorVarName } .prototype);
5990
60- ${ this . _typeName } .prototype.detectChangesInRecordsInternal = function(throwOnChange) {
91+ ${ this . typeName } .prototype.detectChangesInRecordsInternal = function(throwOnChange) {
6192 ${ this . _names . genInitLocals ( ) }
6293 var ${ IS_CHANGED_LOCAL } = false;
6394 var ${ CHANGES_LOCAL } = null;
@@ -80,31 +111,25 @@ export class ChangeDetectorJITGenerator {
80111 ${ this . _genPropertyBindingTargets ( ) }
81112
82113 ${ this . _genDirectiveIndices ( ) }
83-
84- return function(dispatcher) {
85- return new ${ this . _typeName } (dispatcher);
86- }
87114 ` ;
88- return new Function ( ABSTRACT_CHANGE_DETECTOR , UTIL , classDefinition ) ( AbstractChangeDetector ,
89- ChangeDetectionUtil ) ;
90115 }
91116
92117 _genPropertyBindingTargets ( ) : string {
93118 var targets = this . _logic . genPropertyBindingTargets ( this . propertyBindingTargets ,
94119 this . genConfig . genDebugInfo ) ;
95- return `${ this . _typeName } .gen_propertyBindingTargets = ${ targets } ;` ;
120+ return `${ this . typeName } .gen_propertyBindingTargets = ${ targets } ;` ;
96121 }
97122
98123 _genDirectiveIndices ( ) : string {
99124 var indices = this . _logic . genDirectiveIndices ( this . directiveRecords ) ;
100- return `${ this . _typeName } .gen_directiveIndices = ${ indices } ;` ;
125+ return `${ this . typeName } .gen_directiveIndices = ${ indices } ;` ;
101126 }
102127
103128 _maybeGenHandleEventInternal ( ) : string {
104129 if ( this . eventBindings . length > 0 ) {
105130 var handlers = this . eventBindings . map ( eb => this . _genEventBinding ( eb ) ) . join ( "\n" ) ;
106131 return `
107- ${ this . _typeName } .prototype.handleEventInternal = function(eventName, elIndex, locals) {
132+ ${ this . typeName } .prototype.handleEventInternal = function(eventName, elIndex, locals) {
108133 var ${ this . _names . getPreventDefaultAccesor ( ) } = false;
109134 ${ this . _names . genInitEventLocals ( ) }
110135 ${ handlers }
@@ -156,7 +181,7 @@ export class ChangeDetectorJITGenerator {
156181 }
157182 var dehydrateFieldsCode = this . _names . genDehydrateFields ( ) ;
158183 if ( ! destroyPipesCode && ! dehydrateFieldsCode ) return '' ;
159- return `${ this . _typeName } .prototype.dehydrateDirectives = function(destroyPipes) {
184+ return `${ this . typeName } .prototype.dehydrateDirectives = function(destroyPipes) {
160185 ${ destroyPipesCode }
161186 ${ dehydrateFieldsCode }
162187 }` ;
@@ -166,7 +191,7 @@ export class ChangeDetectorJITGenerator {
166191 var hydrateDirectivesCode = this . _logic . genHydrateDirectives ( this . directiveRecords ) ;
167192 var hydrateDetectorsCode = this . _logic . genHydrateDetectors ( this . directiveRecords ) ;
168193 if ( ! hydrateDirectivesCode && ! hydrateDetectorsCode ) return '' ;
169- return `${ this . _typeName } .prototype.hydrateDirectives = function(directives) {
194+ return `${ this . typeName } .prototype.hydrateDirectives = function(directives) {
170195 ${ hydrateDirectivesCode }
171196 ${ hydrateDetectorsCode }
172197 }` ;
@@ -177,7 +202,7 @@ export class ChangeDetectorJITGenerator {
177202 if ( notifications . length > 0 ) {
178203 var directiveNotifications = notifications . join ( "\n" ) ;
179204 return `
180- ${ this . _typeName } .prototype.afterContentLifecycleCallbacksInternal = function() {
205+ ${ this . typeName } .prototype.afterContentLifecycleCallbacksInternal = function() {
181206 ${ directiveNotifications }
182207 }
183208 ` ;
@@ -191,7 +216,7 @@ export class ChangeDetectorJITGenerator {
191216 if ( notifications . length > 0 ) {
192217 var directiveNotifications = notifications . join ( "\n" ) ;
193218 return `
194- ${ this . _typeName } .prototype.afterViewLifecycleCallbacksInternal = function() {
219+ ${ this . typeName } .prototype.afterViewLifecycleCallbacksInternal = function() {
195220 ${ directiveNotifications }
196221 }
197222 ` ;
@@ -239,7 +264,7 @@ export class ChangeDetectorJITGenerator {
239264 var pipeName = r . name ;
240265
241266 var init = `
242- if (${ pipe } === ${ UTIL } .uninitialized) {
267+ if (${ pipe } === ${ this . changeDetectionUtilVarName } .uninitialized) {
243268 ${ pipe } = ${ this . _names . getPipesAccessorName ( ) } .get('${ pipeName } ');
244269 }
245270 ` ;
@@ -251,7 +276,7 @@ export class ChangeDetectorJITGenerator {
251276
252277 var check = `
253278 if (${ oldValue } !== ${ newValue } ) {
254- ${ newValue } = ${ UTIL } .unwrapValue(${ newValue } )
279+ ${ newValue } = ${ this . changeDetectionUtilVarName } .unwrapValue(${ newValue } )
255280 ${ this . _genChangeMarker ( r ) }
256281 ${ this . _genUpdateDirectiveOrElement ( r ) }
257282 ${ this . _genAddToChanges ( r ) }
@@ -342,7 +367,7 @@ export class ChangeDetectorJITGenerator {
342367
343368 _genCheckNoChanges ( ) : string {
344369 if ( this . genConfig . genCheckNoChanges ) {
345- return `${ this . _typeName } .prototype.checkNoChanges = function() { this.runDetectChanges(true); }` ;
370+ return `${ this . typeName } .prototype.checkNoChanges = function() { this.runDetectChanges(true); }` ;
346371 } else {
347372 return '' ;
348373 }
0 commit comments