Skip to content
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

Feature - Unregister a token #190

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
43 changes: 43 additions & 0 deletions src/__tests__/global-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,49 @@ test("clears cached instances from container.resolve() calls", () => {
expect(instance3).toBeInstanceOf(Foo);
});

// --- unregister() ---

test("unregister all instances", () => {
class Foo {}
const instance1 = new Foo();
globalContainer.registerInstance("Test", instance1);

expect(globalContainer.resolve("Test")).toBeInstanceOf(Foo);

globalContainer.unregisterAll();

expect(() => {
globalContainer.resolve("Test");
}).toThrow();
});

test("unregister a single instance", () => {
class Foo {}

const instance1 = new Foo();
const instance2 = new Foo();

globalContainer.registerInstance("Test1", instance1);
globalContainer.registerInstance("Test2", instance2);

expect(globalContainer.resolve("Test1")).toBeInstanceOf(Foo);
expect(globalContainer.resolve("Test2")).toBeInstanceOf(Foo);

globalContainer.unregister("Test1");

expect(globalContainer.resolve("Test2")).toBeInstanceOf(Foo);

expect(() => {
globalContainer.resolve("Test1");
}).toThrow();
});

test("fails to delete unregistered dependency by name", () => {
expect(() => {
globalContainer.unregister("NotRegistered");
}).toThrow();
});

// --- @injectable ---

test("@injectable resolves when not using DI", () => {
Expand Down
20 changes: 20 additions & 0 deletions src/dependency-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ class InternalDependencyContainer implements DependencyContainer {
this.interceptors.postResolution.clear();
}

public unregisterAll(): void {
this._registry.clear();
this.interceptors.preResolution.clear();
this.interceptors.postResolution.clear();
}

public unregister<T>(token: InjectionToken<T>): void {
const registration = this.getRegistration(token);

if (!registration) {
throw new Error(
`Attempted to delete unregistered dependency token: "${token.toString()}"`
);
}

this._registry.delete(token);
this.interceptors.preResolution.delete(token);
this.interceptors.postResolution.delete(token);
}

public clearInstances(): void {
for (const [token, registrations] of this._registry.entries()) {
this._registry.setAll(
Expand Down
4 changes: 4 additions & 0 deletions src/registry-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export default abstract class RegistryBase<T> {
this._registryMap.clear();
}

public delete(key: InjectionToken<any>): void {
this._registryMap.delete(key);
}

private ensure(key: InjectionToken<any>): void {
if (!this._registryMap.has(key)) {
this._registryMap.set(key, []);
Expand Down
3 changes: 3 additions & 0 deletions src/types/dependency-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export default interface DependencyContainer {
*/
reset(): void;

unregisterAll(): void;
unregister<T>(token: InjectionToken<T>): void;

clearInstances(): void;
createChildContainer(): DependencyContainer;

Expand Down