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 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ constructor injection.
- [Interception](#interception)
- [Child Containers](#child-containers)
- [Clearing Instances](#clearing-instances)
- [Unregister](#unregister)
- [Circular dependencies](#circular-dependencies)
- [The `delay` helper function](#the-delay-helper-function)
- [Interfaces and circular dependencies](#interfaces-and-circular-dependencies)
Expand Down Expand Up @@ -595,6 +596,29 @@ test("something", () => {
});
```

Unlike with `container.reset()`, the registrations themselves are not cleared.

### Unregister
Unregister allows you to clear a previously created and registered instance.

```typescript
class Foo {}
class Bar {}

const myFoo = new Foo();
container.registerInstance("MY_FOO", myFoo);

const myBar = new Bar();
container.registerInstance("MY_BAR", myBar);

container.unregister("MY_BAR");

const myFoo2 = container.resolve("MY_FOO"); // resolved instance
const myBar2 = container.resolve("MY_BAR"); // throws error
```

Unlike `container.reset()`, unregister allows the removal of a specific token without affecting the others.

# Circular dependencies

Sometimes you need to inject services that have cyclic dependencies between them. As an example:
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/global-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,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 @@ -394,6 +394,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 {
this.ensureNotDisposed();

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 @@ -95,6 +95,9 @@ export default interface DependencyContainer extends Disposable {
*/
reset(): void;

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

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

Expand Down