Skip to content

Commit

Permalink
refactor: optimize resolve token
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Oct 13, 2023
1 parent 5a80b41 commit 1bdacde
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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];
}
Expand Down

0 comments on commit 1bdacde

Please sign in to comment.