refactor(pipes): use angular lifecycle hooks instead of PipeOnDestroy

BREAKING CHANGE:
Previously, pipes that wanted to be notified when they were destroyed
would implement the PipeOnDestroy interface and name the callback
`onDestroy`. This change removes the PipeOnDestroy interface and
instead uses Angular's lifecycle interface `OnDestroy`, with the
`ngOnDestroy` method.

Before:
```
import {Pipe, PipeOnDestroy} from 'angular2/angular2';
@Pipe({pure: false})
export class MyPipe implements PipeOnDestroy {
  onDestroy() {}
}
```

After:
import {Pipe, OnDestroy} from 'angular2/angular2';
@Pipe({pure: false})
export class MyPipe implements PipeOnDestroy {
  ngOnDestroy() {}
}
This commit is contained in:
Jeff Cross
2015-11-17 10:09:23 -08:00
committed by vsavkin
parent 604c8bbad5
commit fcc7ce225e
13 changed files with 79 additions and 82 deletions
@@ -96,13 +96,13 @@ export function main() {
}));
});
describe("onDestroy", () => {
describe("ngOnDestroy", () => {
it("should do nothing when no subscription",
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
() => { expect(() => pipe.ngOnDestroy()).not.toThrow(); });
it("should dispose of the existing subscription", inject([AsyncTestCompleter], (async) => {
pipe.transform(emitter);
pipe.onDestroy();
pipe.ngOnDestroy();
ObservableWrapper.callEmit(emitter, message);
@@ -182,9 +182,9 @@ export function main() {
}, timer)
}));
describe("onDestroy", () => {
describe("ngOnDestroy", () => {
it("should do nothing when no source",
() => { expect(() => pipe.onDestroy()).not.toThrow(); });
() => { expect(() => pipe.ngOnDestroy()).not.toThrow(); });
it("should dispose of the existing source", inject([AsyncTestCompleter], (async) => {
pipe.transform(completer.promise);
@@ -194,7 +194,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
pipe.onDestroy();
pipe.ngOnDestroy();
expect(pipe.transform(completer.promise)).toBe(null);
async.done();
}, timer);
@@ -10,20 +10,20 @@ import {
afterEach
} from 'angular2/testing_internal';
import {Injector, Inject, provide, Pipe, PipeTransform} from 'angular2/core';
import {Injector, Inject, provide, Pipe, PipeTransform, OnDestroy} from 'angular2/core';
import {ProtoPipes, Pipes} from 'angular2/src/core/pipes/pipes';
import {PipeProvider} from 'angular2/src/core/pipes/pipe_provider';
class PipeA implements PipeTransform {
class PipeA implements PipeTransform, OnDestroy {
transform(a, b) {}
onDestroy() {}
ngOnDestroy() {}
}
class PipeB implements PipeTransform {
class PipeB implements PipeTransform, OnDestroy {
dep;
constructor(@Inject("dep") dep: any) { this.dep = dep; }
transform(a, b) {}
onDestroy() {}
ngOnDestroy() {}
}
export function main() {
@@ -35,7 +35,6 @@ import {
DirectiveRecord,
DirectiveIndex,
PipeTransform,
PipeOnDestroy,
ChangeDetectionStrategy,
WrappedValue,
DynamicProtoChangeDetector,
@@ -48,6 +47,7 @@ import {
import {SelectedPipe, Pipes} from 'angular2/src/core/change_detection/pipes';
import {JitProtoChangeDetector} from 'angular2/src/core/change_detection/jit_proto_change_detector';
import {OnDestroy} from 'angular2/src/core/linker/interfaces';
import {getDefinition} from './change_detector_config';
import {createObservableModel} from './change_detector_spec_util';
@@ -1228,7 +1228,7 @@ export function main() {
expect(cd.hydrated()).toBe(true);
});
it('should destroy all active pipes implementing onDestroy during dehyration', () => {
it('should destroy all active pipes implementing ngOnDestroy during dehyration', () => {
var pipe = new PipeWithOnDestroy();
var registry = new FakePipes('pipe', () => pipe);
var cd = _createChangeDetector('name | pipe', new Person('bob'), registry).changeDetector;
@@ -1239,7 +1239,7 @@ export function main() {
expect(pipe.destroyCalled).toBe(true);
});
it('should not call onDestroy all pipes that do not implement onDestroy', () => {
it('should not call ngOnDestroy all pipes that do not implement ngOnDestroy', () => {
var pipe = new CountingPipe();
var registry = new FakePipes('pipe', () => pipe);
var cd = _createChangeDetector('name | pipe', new Person('bob'), registry).changeDetector;
@@ -1365,9 +1365,9 @@ class CountingPipe implements PipeTransform {
transform(value, args = null) { return `${value} state:${this.state ++}`; }
}
class PipeWithOnDestroy implements PipeTransform, PipeOnDestroy {
class PipeWithOnDestroy implements PipeTransform, OnDestroy {
destroyCalled: boolean = false;
onDestroy() { this.destroyCalled = true; }
ngOnDestroy() { this.destroyCalled = true; }
transform(value, args = null) { return null; }
}
@@ -54,7 +54,8 @@ import {
Inject,
Host,
SkipSelf,
SkipSelfMetadata
SkipSelfMetadata,
OnDestroy
} from 'angular2/core';
import {NgIf, NgFor} from 'angular2/common';
@@ -1999,8 +2000,8 @@ class SomeViewport {
}
@Pipe({name: 'double'})
class DoublePipe implements PipeTransform {
onDestroy() {}
class DoublePipe implements PipeTransform, OnDestroy {
ngOnDestroy() {}
transform(value, args = null) { return `${value}${value}`; }
}
+1 -2
View File
@@ -108,7 +108,7 @@ var NG_ALL = [
'ApplicationRef.tick()',
*/
'AsyncPipe',
'AsyncPipe.onDestroy()',
'AsyncPipe.ngOnDestroy()',
'AsyncPipe.transform()',
'Attribute',
'Attribute.attributeName',
@@ -1425,7 +1425,6 @@ var NG_ALL = [
'OnChanges:dart',
'OnDestroy:dart',
'OnInit:dart',
'PipeOnDestroy:dart',
'PipeTransform:dart',
'reflector',
'RenderBeginCmd:dart',