Skip to content

Commit

Permalink
refactor: simplify registry
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Nov 19, 2024
1 parent 6d890fc commit caec65e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function createContainer({
},

isRegistered(token) {
return registry.has(token);
return !!registry.get(token);
},

resetRegistry() {
Expand Down
36 changes: 2 additions & 34 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,7 @@ export class Registry {
}

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

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

getAll<T>(token: Token<T>): Registration<T>[] | undefined {
Expand All @@ -68,35 +57,14 @@ export class Registry {
);
}

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

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

set<T>(token: Token<T>, registration: Registration<T>): void {
assert(!internals.has(token), `cannot register reserved token ${token.name}`);
let registrations = this._map.get(token);
if (!registrations) {
registrations = [];
this._map.set(token, registrations);
}
const existingIndex = registrations.findIndex(({provider}) => provider === registration.provider);
if (existingIndex != -1) {
registrations[existingIndex] = registration;
}
else {
registrations.push(registration);
}
registrations.push(registration);
}
}

Expand Down

0 comments on commit caec65e

Please sign in to comment.