@@ -7,7 +7,7 @@ import {Parser} from 'angular2/src/change_detection/parser/parser';
77import { Lexer } from 'angular2/src/change_detection/parser/lexer' ;
88
99import { ChangeDispatcher , DynamicChangeDetector , ChangeDetectionError , ContextWithVariableBindings ,
10- PipeRegistry , NO_CHANGE , CHECK_ALWAYS , CHECK_ONCE , CHECKED , DETACHED } from 'angular2/change_detection' ;
10+ PipeRegistry , Pipe , NO_CHANGE , CHECK_ALWAYS , CHECK_ONCE , CHECKED , DETACHED } from 'angular2/change_detection' ;
1111
1212import { ChangeDetectionUtil } from 'angular2/src/change_detection/change_detection_util' ;
1313
@@ -33,7 +33,7 @@ export function main() {
3333 pcd . addAst ( ast ( exp ) , memo , memo ) ;
3434 var dispatcher = new TestDispatcher ( ) ;
3535 var cd = pcd . instantiate ( dispatcher ) ;
36- cd . setContext ( context ) ;
36+ cd . hydrate ( context ) ;
3737
3838 return { "changeDetector" : cd , "dispatcher" : dispatcher } ;
3939 }
@@ -183,7 +183,7 @@ export function main() {
183183
184184 var dispatcher = new TestDispatcher ( ) ;
185185 var cd = pcd . instantiate ( dispatcher ) ;
186- cd . setContext ( new TestData ( "value" ) ) ;
186+ cd . hydrate ( new TestData ( "value" ) ) ;
187187
188188 cd . detectChanges ( ) ;
189189
@@ -264,7 +264,7 @@ export function main() {
264264 dispatcher . logValue ( 'InvokeC' ) ;
265265 return 'c'
266266 } ;
267- cd . setContext ( tr ) ;
267+ cd . hydrate ( tr ) ;
268268
269269 cd . detectChanges ( ) ;
270270
@@ -280,7 +280,7 @@ export function main() {
280280
281281 var dispatcher = new TestDispatcher ( ) ;
282282 var cd = pcd . instantiate ( dispatcher ) ;
283- cd . setContext ( new TestData ( 'value' ) ) ;
283+ cd . hydrate ( new TestData ( 'value' ) ) ;
284284
285285 expect ( ( ) => {
286286 cd . checkNoChanges ( ) ;
@@ -295,7 +295,7 @@ export function main() {
295295 pcd . addAst ( ast ( 'invalidProp' , 'someComponent' ) , "a" , 1 ) ;
296296
297297 var cd = pcd . instantiate ( new TestDispatcher ( ) ) ;
298- cd . setContext ( null ) ;
298+ cd . hydrate ( null ) ;
299299
300300 try {
301301 cd . detectChanges ( ) ;
@@ -442,6 +442,35 @@ export function main() {
442442 } ) ;
443443 } ) ;
444444
445+ describe ( "hydration" , ( ) => {
446+ it ( "should be able to rehydrate a change detector" , ( ) => {
447+ var c = createChangeDetector ( "memo" , "name" ) ;
448+ var cd = c [ "changeDetector" ] ;
449+
450+ cd . hydrate ( "some context" ) ;
451+ expect ( cd . hydrated ( ) ) . toBe ( true ) ;
452+
453+ cd . dehydrate ( ) ;
454+ expect ( cd . hydrated ( ) ) . toBe ( false ) ;
455+
456+ cd . hydrate ( "other context" ) ;
457+ expect ( cd . hydrated ( ) ) . toBe ( true ) ;
458+ } ) ;
459+
460+ it ( "should destroy all active pipes during dehyration" , ( ) => {
461+ var pipe = new OncePipe ( ) ;
462+ var registry = new FakePipeRegistry ( 'pipe' , ( ) => pipe ) ;
463+ var c = createChangeDetector ( "memo" , "name | pipe" , new Person ( 'bob' ) , registry ) ;
464+ var cd = c [ "changeDetector" ] ;
465+
466+ cd . detectChanges ( ) ;
467+
468+ cd . dehydrate ( ) ;
469+
470+ expect ( pipe . destroyCalled ) . toBe ( true ) ;
471+ } ) ;
472+ } ) ;
473+
445474 describe ( "pipes" , ( ) => {
446475 it ( "should support pipes" , ( ) => {
447476 var registry = new FakePipeRegistry ( 'pipe' , ( ) => new CountingPipe ( ) ) ;
@@ -477,6 +506,21 @@ export function main() {
477506
478507 expect ( registry . numberOfLookups ) . toEqual ( 2 ) ;
479508 } ) ;
509+
510+ it ( "should invoke onDestroy on a pipe before switching to another one" , ( ) => {
511+ var pipe = new OncePipe ( ) ;
512+ var registry = new FakePipeRegistry ( 'pipe' , ( ) => pipe ) ;
513+ var ctx = new Person ( "Megatron" ) ;
514+
515+ var c = createChangeDetector ( "memo" , "name | pipe" , ctx , registry ) ;
516+ var cd = c [ "changeDetector" ] ;
517+
518+ cd . detectChanges ( ) ;
519+ ctx . name = "Optimus Prime" ;
520+ cd . detectChanges ( ) ;
521+
522+ expect ( pipe . destroyCalled ) . toEqual ( true ) ;
523+ } ) ;
480524 } ) ;
481525
482526 it ( "should do nothing when returns NO_CHANGE" , ( ) => {
@@ -502,10 +546,11 @@ export function main() {
502546 } ) ;
503547}
504548
505- class CountingPipe {
549+ class CountingPipe extends Pipe {
506550 state :number ;
507551
508552 constructor ( ) {
553+ super ( ) ;
509554 this . state = 0 ;
510555 }
511556
@@ -518,23 +563,31 @@ class CountingPipe {
518563 }
519564}
520565
521- class OncePipe {
566+ class OncePipe extends Pipe {
522567 called :boolean ;
568+ destroyCalled :boolean ;
569+
523570 constructor ( ) {
571+ super ( ) ;
524572 this . called = false ; ;
573+ this . destroyCalled = false ;
525574 }
526575
527576 supports ( newValue ) {
528577 return ! this . called ;
529578 }
530579
580+ onDestroy ( ) {
581+ this . destroyCalled = true ;
582+ }
583+
531584 transform ( value ) {
532585 this . called = true ;
533586 return value ;
534587 }
535588}
536589
537- class IdentityPipe {
590+ class IdentityPipe extends Pipe {
538591 state :any ;
539592
540593 supports ( newValue ) {
0 commit comments