fix(ivy): don't throw when attempting to destroy a destroyed ComponentRef (#31022)

Currently in Ivy we throw when attempting to destroy a `ComponentRef` that has been destroyed, however in ViewEngine we didn't which can cause some tests to break. These changes remove the error to match ViewEngine.

These changes resolve FW-1379.

PR Close #31022
This commit is contained in:
crisbeto 2019-06-13 17:41:45 +02:00 committed by Andrew Kushnir
parent 0c3bb6a731
commit 8f5c396a7c
2 changed files with 28 additions and 12 deletions

View File

@ -18,7 +18,6 @@ import {ElementRef as viewEngine_ElementRef} from '../linker/element_ref';
import {NgModuleRef as viewEngine_NgModuleRef} from '../linker/ng_module_factory';
import {RendererFactory2} from '../render/api';
import {Sanitizer} from '../sanitization/security';
import {assertDefined} from '../util/assert';
import {VERSION} from '../version';
import {NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR} from '../view/provider';
@ -258,13 +257,16 @@ export class ComponentRef<T> extends viewEngine_ComponentRef<T> {
get injector(): Injector { return new NodeInjector(this._tNode, this._rootLView); }
destroy(): void {
ngDevMode && assertDefined(this.destroyCbs, 'NgModule already destroyed');
this.destroyCbs !.forEach(fn => fn());
if (this.destroyCbs) {
this.destroyCbs.forEach(fn => fn());
this.destroyCbs = null;
!this.hostView.destroyed && this.hostView.destroy();
}
}
onDestroy(callback: () => void): void {
ngDevMode && assertDefined(this.destroyCbs, 'NgModule already destroyed');
this.destroyCbs !.push(callback);
if (this.destroyCbs) {
this.destroyCbs.push(callback);
}
}
}

View File

@ -1035,6 +1035,20 @@ describe('ViewContainerRef', () => {
.toEqual(
'<p vcref=""></p><embedded-cmp-with-ngcontent>12<hr>34</embedded-cmp-with-ngcontent>');
});
it('should not throw when calling destroy() multiple times for a ComponentRef', () => {
@Component({template: ''})
class App {
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
fixture.componentRef.destroy();
expect(() => fixture.componentRef.destroy()).not.toThrow();
});
});
describe('insertion points and declaration points', () => {