feat(core): support ngOnDestroy on providers of a directive.

Such providers also become eager as they will be instantiated
anyways on destruction.
This commit is contained in:
Tobias Bosch
2016-08-02 01:37:42 -07:00
parent ff3b71f7b3
commit c161ed415d
10 changed files with 87 additions and 42 deletions
@@ -1026,6 +1026,20 @@ export function main() {
'pipeWithOnDestroy.ngOnDestroy'
]);
}));
it('should call ngOnDestroy on an injectable class', fakeAsync(() => {
var ctx = createCompFixture(
'<div testDirective="dir"></div>', TestComponent,
tcb.overrideProviders(TestDirective, [InjectableWithLifecycle]));
ctx.debugElement.children[0].injector.get(InjectableWithLifecycle);
ctx.detectChanges(false);
ctx.destroy();
expect(directiveLog.filter(['ngOnDestroy'])).toEqual([
'dir.ngOnDestroy', 'injectable.ngOnDestroy'
]);
}));
});
});
@@ -1386,6 +1400,14 @@ class TestDirective implements OnInit, DoCheck, OnChanges, AfterContentInit, Aft
}
}
@Injectable()
class InjectableWithLifecycle {
name = 'injectable';
constructor(public log: DirectiveLog) {}
ngOnDestroy() { this.log.add(this.name, 'ngOnDestroy'); }
}
@Directive({selector: '[orderCheck0]'})
class OrderCheckDirective0 {
private _name: string;
@@ -384,6 +384,20 @@ export function main() {
expect(created).toBe(true);
}));
it('should instantiate providers with a lifecycle hook eagerly', fakeAsync(() => {
var created = false;
class SomeInjectable {
constructor() { created = true; }
ngOnDestroy() {}
}
var el = createComp(
'<div simpleDirective></div>',
tcb.overrideProviders(SimpleDirective, [SomeInjectable]));
expect(created).toBe(true);
}));
it('should instantiate view providers lazily', fakeAsync(() => {
var created = false;
var el = createComp(