From e44fc4e11a8419d0c9e247430c6c20703382f83c Mon Sep 17 00:00:00 2001 From: Xuanbo Cheng Date: Wed, 20 Nov 2024 13:03:10 +0000 Subject: [PATCH] refactor: simplify registry (#14) --- src/container.ts | 2 +- src/registry.ts | 36 ++---------------------------------- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/container.ts b/src/container.ts index 2f6f30e..1022bf6 100644 --- a/src/container.ts +++ b/src/container.ts @@ -185,7 +185,7 @@ export function createContainer({ }, isRegistered(token) { - return registry.has(token); + return !!registry.get(token); }, resetRegistry() { diff --git a/src/registry.ts b/src/registry.ts index 4c00404..f0d21fc 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -38,18 +38,7 @@ export class Registry { } get(token: Token): Registration | undefined { - return ( - internals.get(token) - || this._get(token) - ); - } - - private _get(token: Token): Registration | undefined { - const registrations = this._map.get(token); - return ( - registrations?.at(-1) - || this.parent?._get(token) - ); + return this.getAll(token)?.at(-1); } getAll(token: Token): Registration[] | undefined { @@ -68,21 +57,6 @@ 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(token: Token, registration: Registration): void { assert(!internals.has(token), `cannot register reserved token ${token.name}`); let registrations = this._map.get(token); @@ -90,13 +64,7 @@ export class Registry { 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); } }