@@ -15,8 +15,11 @@ import 'package:angular2/src/change_detection/proto_record.dart';
1515class Codegen {
1616 final StringBuffer _buf = new StringBuffer ();
1717
18- void generate (String name, ChangeDetectorDefinition def) {
19- new _CodegenState (name, def)._writeToBuf (_buf);
18+ /// Generates a change detector class with name `changeDetectorTypeName`
19+ /// which is used to detect changes in Objects of type `typeName` .
20+ void generate (String typeName, String changeDetectorTypeName,
21+ ChangeDetectorDefinition def) {
22+ new _CodegenState (typeName, changeDetectorTypeName, def)._writeToBuf (_buf);
2023 }
2124
2225 String get imports {
@@ -33,7 +36,8 @@ class Codegen {
3336
3437/// The state needed to generate a change detector for a single `Component` .
3538class _CodegenState {
36- final String _typeName;
39+ final String _contextTypeName;
40+ final String _changeDetectorTypeName;
3741 final String _changeDetectionMode;
3842 final List <ProtoRecord > _records;
3943 final List <DirectiveRecord > _directiveRecords;
@@ -42,30 +46,33 @@ class _CodegenState {
4246 final List <String > _fieldNames;
4347 final List <String > _pipeNames;
4448
45- _CodegenState ._(this ._typeName, String changeDetectionStrategy, this ._records,
46- this ._directiveRecords, List <String > localNames)
49+ _CodegenState ._(this ._contextTypeName, this ._changeDetectorTypeName,
50+ String changeDetectionStrategy, this ._records, this ._directiveRecords,
51+ List <String > localNames)
4752 : this ._localNames = localNames,
4853 _changeNames = _getChangeNames (localNames),
4954 _fieldNames = _getFieldNames (localNames),
5055 _pipeNames = _getPipeNames (localNames),
5156 _changeDetectionMode = ChangeDetectionUtil
5257 .changeDetectionMode (changeDetectionStrategy);
5358
54- factory _CodegenState (String typeName, ChangeDetectorDefinition def) {
59+ factory _CodegenState (String typeName, String changeDetectorTypeName,
60+ ChangeDetectorDefinition def) {
5561 var protoRecords = new ProtoRecordBuilder ();
5662 def.bindingRecords
5763 .forEach ((rec) => protoRecords.add (rec, def.variableNames));
5864 var records = coalesce (protoRecords.records);
59- return new _CodegenState ._(typeName, def.strategy, records ,
60- def.directiveRecords, _getLocalNames (records));
65+ return new _CodegenState ._(typeName, changeDetectorTypeName, def.strategy,
66+ records, def.directiveRecords, _getLocalNames (records));
6167 }
6268
6369 /// Generates sanitized names for use as local variables.
6470 static List <String > _getLocalNames (List <ProtoRecord > records) {
71+ var whitespacePattern = new RegExp (r'\W' );
6572 var localNames = new List <String >(records.length + 1 );
6673 localNames[0 ] = 'context' ;
6774 for (var i = 0 ; i < records.length; ++ i) {
68- var sanitizedName = records[i].name.replaceAll (new RegExp ( r'\W' ) , '' );
75+ var sanitizedName = records[i].name.replaceAll (whitespacePattern , '' );
6976 localNames[i + 1 ] = '$sanitizedName $i ' ;
7077 }
7178 return localNames;
@@ -85,17 +92,21 @@ class _CodegenState {
8592
8693 void _writeToBuf (StringBuffer buf) {
8794 buf.write ('''
88- class $_typeName extends $_BASE_CLASS {
95+ class $_changeDetectorTypeName extends $_BASE_CLASS {
8996 final dynamic $_DISPATCHER_ACCESSOR ;
9097 final $_GEN_PREFIX .PipeRegistry $_PIPE_REGISTRY_ACCESSOR ;
9198 final $_GEN_PREFIX .List<$_GEN_PREFIX .ProtoRecord> $_PROTOS_ACCESSOR ;
9299 final $_GEN_PREFIX .List<$_GEN_PREFIX .DirectiveRecord>
93100 $_DIRECTIVES_ACCESSOR ;
94101 dynamic $_LOCALS_ACCESSOR = null;
95- ${_allFields ().map (
96- (f ) => 'dynamic $f = $_UTIL .uninitialized();' ).join ('' )}
97-
98- $_typeName (
102+ ${_allFields ().map ((f ) {
103+ if (f == _CONTEXT_ACCESSOR ) {
104+ return '$_contextTypeName $f = null;' ;
105+ }
106+ return 'dynamic $f = $_UTIL .uninitialized();' ;
107+ }).join ('' )}
108+
109+ $_changeDetectorTypeName (
99110 this.$_DISPATCHER_ACCESSOR ,
100111 this.$_PIPE_REGISTRY_ACCESSOR ,
101112 this.$_PROTOS_ACCESSOR ,
@@ -116,7 +127,7 @@ class _CodegenState {
116127 ${_getCallOnAllChangesDoneBody ()}
117128 }
118129
119- void hydrate(context, locals, directives) {
130+ void hydrate($ _contextTypeName context, locals, directives) {
120131 $_MODE_ACCESSOR = '$_changeDetectionMode ';
121132 $_CONTEXT_ACCESSOR = context;
122133 $_LOCALS_ACCESSOR = locals;
@@ -126,19 +137,22 @@ class _CodegenState {
126137
127138 void dehydrate() {
128139 ${_genPipeOnDestroy ()}
129- ${_allFields ().map ((f ) => '$f = $_UTIL .uninitialized();' ).join ('' )}
140+ ${_allFields ().map ((f ) {
141+ return f == _CONTEXT_ACCESSOR
142+ ? '$f = null;'
143+ : '$f = $_UTIL .uninitialized();' ;
144+ }).join ('' )}
130145 $_LOCALS_ACCESSOR = null;
131146 }
132147
133- hydrated() => !$_IDENTICAL_CHECK_FN (
134- $_CONTEXT_ACCESSOR , $_UTIL .uninitialized());
148+ hydrated() => $_CONTEXT_ACCESSOR == null;
135149
136150 static $_GEN_PREFIX .ProtoChangeDetector
137151 $PROTO_CHANGE_DETECTOR_FACTORY_METHOD (
138152 $_GEN_PREFIX .PipeRegistry registry,
139153 $_GEN_PREFIX .ChangeDetectorDefinition def) {
140154 return new $_GEN_PREFIX .PregenProtoChangeDetector(
141- (a, b, c, d) => new $_typeName (a, b, c, d),
155+ (a, b, c, d) => new $_changeDetectorTypeName (a, b, c, d),
142156 registry, def);
143157 }
144158 }
0 commit comments