Skip to content

Commit

Permalink
fix(core): call ngOnDestroy for tree-shakeable providers (#28943)
Browse files Browse the repository at this point in the history
Prior to this change, any provider that was independently resolved using
its InjectableDef would not be considered when destroying the module it
was requested from. This commit provides a fix for this issue by storing
the resolved provider in the module's list of provider definitions.

Fixes #28927

PR Close #28943
  • Loading branch information
JoostK authored and IgorMinar committed Apr 5, 2019
1 parent f8cdda6 commit 30b0442
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/view/ng_module.ts
Expand Up @@ -109,7 +109,7 @@ export function resolveNgModuleDep(
} else if (
(injectableDef = getInjectableDef(depDef.token)) && targetsModule(data, injectableDef)) {
const index = data._providers.length;
data._def.providersByKey[depDef.tokenKey] = {
data._def.providers[index] = data._def.providersByKey[depDef.tokenKey] = {
flags: NodeFlags.TypeFactoryProvider | NodeFlags.LazyProvider,
value: injectableDef.factory,
deps: [], index,
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/view/ng_module_spec.ts
Expand Up @@ -200,6 +200,28 @@ describe('NgModuleRef_ injector', () => {
expect(Service.destroyed).toBe(1);
});

it('calls ngOnDestroy on scoped providers', () => {
class Module {}

class Service {
static destroyed = 0;

ngOnDestroy(): void { Service.destroyed++; }

static ngInjectableDef: InjectableDef<Service> = defineInjectable({
factory: () => new Service(),
providedIn: 'root',
});
}

const ref = createNgModuleRef(Module, Injector.NULL, [], makeFactoryProviders([], [Module]));

expect(ref.injector.get(Service)).toBeDefined();
expect(Service.destroyed).toBe(0);
ref.destroy();
expect(Service.destroyed).toBe(1);
});

it('only calls ngOnDestroy once per instance', () => {
class Module {}

Expand Down

0 comments on commit 30b0442

Please sign in to comment.