fix(core): call ngOnDestroy on all services that have it (#23755)
Previously, ngOnDestroy was only called on services which were statically determined to have ngOnDestroy methods. In some cases, such as with services instantiated via factory functions, it's not statically known that the service has an ngOnDestroy method. This commit changes the runtime to look for ngOnDestroy when instantiating all DI tokens, and to call the method if it's present. Fixes #22466 Fixes #22240 Fixes #14818 PR Close #23755
This commit is contained in:

committed by
Igor Minar

parent
77ff72f93b
commit
fc034270ce
@ -150,6 +150,15 @@ function _createProviderInstance(ngModule: NgModuleData, providerDef: NgModulePr
|
||||
injectable = providerDef.value;
|
||||
break;
|
||||
}
|
||||
|
||||
// The read of `ngOnDestroy` here is slightly expensive as it's megamorphic, so it should be
|
||||
// avoided if possible. The sequence of checks here determines whether ngOnDestroy needs to be
|
||||
// checked. It might not if the `injectable` isn't an object or if NodeFlags.OnDestroy is already
|
||||
// set (ngOnDestroy was detected statically).
|
||||
if (injectable !== UNDEFINED_VALUE && injectable != null && typeof injectable === 'object' &&
|
||||
!(providerDef.flags & NodeFlags.OnDestroy) && typeof injectable.ngOnDestroy === 'function') {
|
||||
providerDef.flags |= NodeFlags.OnDestroy;
|
||||
}
|
||||
return injectable === undefined ? UNDEFINED_VALUE : injectable;
|
||||
}
|
||||
|
||||
@ -199,12 +208,17 @@ function _callFactory(ngModule: NgModuleData, factory: any, deps: DepDef[]): any
|
||||
|
||||
export function callNgModuleLifecycle(ngModule: NgModuleData, lifecycles: NodeFlags) {
|
||||
const def = ngModule._def;
|
||||
const destroyed = new Set<any>();
|
||||
for (let i = 0; i < def.providers.length; i++) {
|
||||
const provDef = def.providers[i];
|
||||
if (provDef.flags & NodeFlags.OnDestroy) {
|
||||
const instance = ngModule._providers[i];
|
||||
if (instance && instance !== UNDEFINED_VALUE) {
|
||||
instance.ngOnDestroy();
|
||||
const onDestroy: Function|undefined = instance.ngOnDestroy;
|
||||
if (typeof onDestroy === 'function' && !destroyed.has(instance)) {
|
||||
onDestroy.apply(instance);
|
||||
destroyed.add(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user