Skip to content

Commit

Permalink
refactor: simplify registry
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 20, 2024
1 parent 7642ca6 commit 56e1137
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export class Container {
}

clearCache(): void {
for (const registrations of this.registry.values()) {
for (const registrations of this.registry.map.values()) {
registrations.forEach(({instance, ...registration}, i) => {
registrations[i] = registration;
});
}
}

resetRegistry(): void {
this.registry.clear();
this.registry.map.clear();
this.registry.set(Container, {
provider: {useValue: this},
});
Expand Down Expand Up @@ -101,7 +101,7 @@ export class Container {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {ErrorMessage} from "./errors";
export {inject, injectAll} from "./inject";
export type {InstanceRef} from "./instance";
export type {ClassProvider, FactoryProvider, Provider, ValueProvider} from "./provider";
export type {Registration, RegistrationOptions, Registry} from "./registry";
export type {Registration, RegistrationMap, RegistrationOptions, Registry} from "./registry";
export {Build, Value} from "./registry";
export {Scope} from "./scope";
export type {Constructor, Token, TokenList} from "./token";
Expand Down
51 changes: 23 additions & 28 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {NullProvider, type Provider, UndefinedProvider} from "./provider";
import {Scope} from "./scope";
import {type Token, Type} from "./token";

export type RegistrationMap = Omit<
Map<Token, Registration[]>,
keyof Registry
>;

export interface Registration<T = any> {
options?: RegistrationOptions;
instance?: InstanceRef<T>;
Expand All @@ -16,74 +21,68 @@ export interface RegistrationOptions {
}

export class Registry {
private map = new Map<Token, Registration[]>();
private _map = new Map<Token, Registration[]>();

readonly map: RegistrationMap = this._map;

private parent?: Registry;
readonly parent?: Registry;

constructor(parent?: Registry) {
this.parent = parent;
}

clear(): void {
this.map.clear();
}

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

get<T>(token: Token<T>): Registration<T> | undefined {
return (
internals.get(token)
|| this.getRecursive(token)
|| this._get(token)
);
}

private getRecursive<T>(token: Token<T>): Registration<T> | undefined {
const registrations = this.map.get(token);
private _get<T>(token: Token<T>): Registration<T> | undefined {
const registrations = this._map.get(token);
return (
registrations?.at(-1)
|| this.parent?.getRecursive(token)
|| this.parent?._get(token)
);
}

getAll<T>(token: Token<T>): Registration<T>[] | undefined {
const internal = internals.get(token);
return (
(internal && [internal])
|| this.getAllRecursive(token)
|| this._getAll(token)
);
}

private getAllRecursive<T>(token: Token<T>): Registration<T>[] | undefined {
const registrations = this.map.get(token);
private _getAll<T>(token: Token<T>): Registration<T>[] | undefined {
const registrations = this._map.get(token);
return (
registrations
|| this.parent?.getAllRecursive(token)
|| this.parent?._getAll(token)
);
}

has(token: Token): boolean {
return (
internals.has(token)
|| this.hasRecursive(token)
|| this._has(token)
);
}

private hasRecursive(token: Token): boolean {
const registrations = this.map.get(token);
private _has(token: Token): boolean {
const registrations = this._map.get(token);
return Boolean(
registrations?.length
|| this.parent?.hasRecursive(token),
|| this.parent?._has(token),
);
}

set<T>(token: Token<T>, registration: Registration<T>): void {
assert(!internals.has(token), ErrorMessage.ReservedToken, token.name);
let registrations = this.map.get(token);
let registrations = this._map.get(token);
if (!registrations) {
registrations = [];
this.map.set(token, registrations);
this._map.set(token, registrations);
}
const existing = registrations.find(
({provider}) => provider === registration.provider,
Expand All @@ -98,10 +97,6 @@ export class Registry {
registrations.push(registration);
}
}

values(): IterableIterator<Registration[]> {
return this.map.values();
}
}

export function Build<Value>(factory: (...args: []) => Value): Type<Value> {
Expand Down

0 comments on commit 56e1137

Please sign in to comment.