Skip to content

Commit

Permalink
feat: support unregister
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 16, 2024
1 parent b5e0260 commit 4e4fbfe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export class Container {
return this;
}

unregister<Value>(token: InjectionToken<Value>): this {
this.registry.delete(token);
return this;
}

resolve<Values extends unknown[]>(...tokens: InjectionTokens<Values>): Values[number];
resolve<Value>(...tokens: InjectionToken<Value>[]): Value {
for (const token of tokens) {
Expand Down Expand Up @@ -130,7 +135,7 @@ export class Container {
private resolveScopedInstance<T>(registration: Registration<T>, instantiate: () => T): T {
const context = useInjectionContext();

if (!context || context.container != this) {
if (!context || context.container !== this) {
return withInjectionContext({
container: this,
resolution: {
Expand Down
7 changes: 5 additions & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class Registry {
this.map.clear();
}

delete<T>(token: InjectionToken<T>): void {
this.map.delete(token);
}

get<T>(token: InjectionToken<T>): Registration<T> | undefined {
return (
internals.get(token)
Expand Down Expand Up @@ -72,7 +76,7 @@ export class Registry {
);
}

set<T>(token: InjectionToken<T>, registration: Registration<T>): this {
set<T>(token: InjectionToken<T>, registration: Registration<T>): void {
assert(!internals.has(token), ErrorMessage.ReservedToken, token.name);
let registrations = this.map.get(token);
if (!registrations) {
Expand All @@ -84,7 +88,6 @@ export class Registry {
provider !== registration.provider)) {
registrations.push(registration);
}
return this;
}

values(): IterableIterator<Registration[]> {
Expand Down

0 comments on commit 4e4fbfe

Please sign in to comment.