From 1bdacdefa48a9728d85eefc02f01c025b6b56f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Fri, 13 Oct 2023 11:36:16 +0800 Subject: [PATCH] refactor: optimize resolve token --- src/injector.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/injector.ts b/src/injector.ts index 6b384e9..f1cfa99 100644 --- a/src/injector.ts +++ b/src/injector.ts @@ -316,7 +316,7 @@ export class Injector { for (const provider of providers) { const originToken = Helper.parseTokenFromProvider(provider); const token = Helper.hasTag(provider) ? this.exchangeToken(originToken, provider.tag) : originToken; - const current = opts.deep ? this.getCreator(token)[0] : this.creatorMap.get(token); + const current = opts.deep ? this.getCreator(token)[0] : this.resolveToken(token)[1]; const shouldBeSet = [ // use provider's override attribute. @@ -333,7 +333,7 @@ export class Injector { this.creatorMap.set(token, creator); // use effect to Make sure there are no cycles - void this.getCreator(token); + void this.resolveToken(token); if (isClassCreator(creator)) { const domain = creator.opts.domain; @@ -372,7 +372,7 @@ export class Injector { } } - private getCreator(token: Token): [InstanceCreator | null, Injector] { + private resolveToken(token: Token): [Token, InstanceCreator | undefined] { let creator = this.creatorMap.get(token); const paths = [token]; @@ -387,6 +387,12 @@ export class Injector { creator = this.creatorMap.get(token); } + return [token, creator]; + } + + private getCreator(token: Token): [InstanceCreator | null, Injector] { + const creator: InstanceCreator | undefined = this.resolveToken(token)[1]; + if (creator) { return [creator, this]; }