From d5031b117eab710fb01d2630eadd1b70d85947aa Mon Sep 17 00:00:00 2001 From: gleip Date: Thu, 12 Feb 2026 15:56:11 +0300 Subject: [PATCH] fix: Logic create singleton object --- src/Container.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Container.ts b/src/Container.ts index 6894b1e..38be050 100644 --- a/src/Container.ts +++ b/src/Container.ts @@ -26,17 +26,10 @@ type SingletonValue = { value: Constant; init?: boolean }; class Container { private readonly container = new Map(); private readonly singletons = new Map(); + private readonly initializedKeys = new Set(); private buildDependency(key: symbol) { - const deepDependency = this.get(key); - - if (this.isAdapterDependency(deepDependency.dependency)) { - return new deepDependency.dependency.value(...deepDependency.constructor); - } - - if (this.isConstantDependency(deepDependency.dependency)) { - return deepDependency.dependency.value; - } + return this.getInstance(key); } private inject(dependency: ContainerValue): { dependency: ContainerValue; constructor: Array } { @@ -179,11 +172,11 @@ class Container { public async initDependencies() { const initialized: InitializableService[] = []; for await (const [key, dependency] of this.container) { - if (this.isAdapterDependency(dependency) && dependency.options?.init && !this.singletons.has(key)) { + if (this.isAdapterDependency(dependency) && dependency.options?.init && !this.initializedKeys.has(key)) { const instance = this.getInstance(key); await instance?.init(); initialized.push(instance!); - this.singletons.set(key, { value: instance, init: dependency.options?.init }); + this.initializedKeys.add(key); } } return initialized;