8000 feat(core): Expose `Injector.destroy` on `Injector` created with … by JeanMeche · Pull Request #60054 · angular/angular · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(core): Expose Injector.destroy on Injector created with … #60054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion goldens/public-api/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,12 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
// @public @deprecated (undocumented)
export const defineInjectable: typeof ɵɵdefineInjectable;

// @public
export interface DestroyableInjector extends Injector {
// (undocumented)
destroy(): void;
}

// @public
export function destroyPlatform(): void;

Expand Down Expand Up @@ -949,7 +955,7 @@ export abstract class Injector {
providers: Array<Provider | StaticProvider>;
parent?: Injector;
name?: string;
}): Injector;
}): DestroyableInjector;
abstract get<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {
optional?: false;
}): T;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/di/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export {
} from './interface/defs';
export {forwardRef, resolveForwardRef, ForwardRefFn} from './forward_ref';
export {Injectable, InjectableDecorator, InjectableProvider} from './injectable';
export {Injector} from './injector';
export {Injector, DestroyableInjector} from './injector';
export {EnvironmentInjector} from './r3_injector';
export {
importProvidersFrom,
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/di/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export abstract class Injector {
providers: Array<Provider | StaticProvider>;
parent?: Injector;
name?: string;
}): Injector;
}): DestroyableInjector;

static create(
options:
Expand Down Expand Up @@ -146,3 +146,12 @@ export abstract class Injector {
*/
static __NG_ELEMENT_ID__ = InjectorMarkers.Injector;
}

/**
* An Injector that the owner can destroy and trigger the DestroyRef.destroy hooks.
*
* @publicApi
*/
export interface DestroyableInjector extends Injector {
destroy(): void;
}
2 changes: 2 additions & 0 deletions packages/core/src/di/r3_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ interface Record<T> {
/**
* An `Injector` that's part of the environment injector hierarchy, which exists outside of the
* component tree.
*
* @publicApi
*/
export abstract class EnvironmentInjector implements Injector {
/**
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/acceptance/di_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID,
ɵINJECTOR_SCOPE,
ɵInternalEnvironmentProviders as InternalEnvironmentProviders,
DestroyRef,
} from '@angular/core';
import {RuntimeError, RuntimeErrorCode} from '@angular/core/src/errors';
import {ViewRef as ViewRefInternal} from '@angular/core/src/render3/view_ref';
Expand Down Expand Up @@ -6529,4 +6530,26 @@ describe('di', () => {

expect(injector.get(token)).toBe('module');
});

it('should be able to destroy programmatically created injectors', () => {
@Injectable()
class Service {
ngOnDestroy() {}
}

const parentInjector = Injector.create({
providers: [Service],
parent: TestBed.inject(Injector),
});

const childInjector = Injector.create({providers: [Service], parent: parentInjector});

const instance = childInjector.get(Service);
const destroySpy = spyOn(instance, 'ngOnDestroy');

parentInjector.get(DestroyRef).onDestroy(() => childInjector.destroy());
parentInjector.destroy();

expect(destroySpy).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion packages/platform-browser/test/browser/tools/tools_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('profiler', () => {
deps: [],
},
],
}),
}) as Injector,
} as ComponentRef<any>);
});

Expand Down
0