Skip to content

Commit

Permalink
refactor: update alias token
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Oct 13, 2023
1 parent 76d7dbd commit 80d81bb
Showing 1 changed file with 18 additions and 35 deletions.
53 changes: 18 additions & 35 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,6 @@ export class Injector {
return injector;
}

/**
* If the token is an alias, find the finally token
*
* the cycles check is done when register alias provider
*/
resolveToken<T extends Token>(token: T): [T, T[]] {
const deps = [token] as T[];
let [aliasCreator] = this.getCreator(token);
while (aliasCreator && isAliasCreator(aliasCreator)) {
token = aliasCreator.useAlias as T;
deps.push(token);
[aliasCreator] = this.getCreator(token);
}

return [token, deps];
}

get<T extends ConstructorOf<any>>(token: T, args?: ConstructorParameters<T>, opts?: InstanceOpts): TokenResult<T>;
get<T extends Token>(token: T, opts?: InstanceOpts): TokenResult<T>;
get<T extends Token, K extends ConstructorOf<any> = ConstructorOf<any>>(
Expand All @@ -121,8 +104,6 @@ export class Injector {
args = undefined;
}

[token] = this.resolveToken(token);

let creator: InstanceCreator | null = null;
let injector: Injector = this;

Expand Down Expand Up @@ -280,8 +261,6 @@ export class Injector {
}

disposeOne(token: Token, key = 'dispose') {
[token] = this.resolveToken(token);

const [creator] = this.getCreator(token);
if (!creator || creator.status === CreatorStatus.init) {
return;
Expand Down Expand Up @@ -353,19 +332,10 @@ export class Injector {
const creator = Helper.parseCreatorFromProvider(provider);

this.creatorMap.set(token, creator);
if (isAliasCreator(creator)) {
// Make sure there are no cycles
const paths = [token, creator.useAlias];
let [aliasCreator] = this.getCreator(creator.useAlias);

while (aliasCreator && isAliasCreator(aliasCreator)) {
if (paths.includes(aliasCreator.useAlias)) {
throw InjectorError.aliasCircularError(paths, aliasCreator.useAlias);
}
paths.push(aliasCreator.useAlias);
[aliasCreator] = this.getCreator(aliasCreator.useAlias);
}
} else if (isClassCreator(creator)) {
// use effect to Make sure there are no cycles
void this.getCreator(token);

if (isClassCreator(creator)) {
const domain = creator.opts.domain;
if (domain) {
const domains = Array.isArray(domain) ? domain : [domain];
Expand Down Expand Up @@ -403,7 +373,20 @@ export class Injector {
}

private getCreator(token: Token): [InstanceCreator | null, Injector] {
const creator = this.creatorMap.get(token);
let creator = this.creatorMap.get(token);

const paths = [token];

while (creator && isAliasCreator(creator)) {
token = creator.useAlias;

if (paths.includes(token)) {
throw InjectorError.aliasCircularError(paths, token);
}
paths.push(token);
creator = this.creatorMap.get(token);
}

if (creator) {
return [creator, this];
}
Expand Down

0 comments on commit 80d81bb

Please sign in to comment.