Skip to content

Commit

Permalink
fix: definition for getAsync and get (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Dec 4, 2020
1 parent 49bc5c8 commit d40de78
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
12 changes: 8 additions & 4 deletions packages/core/src/context/applicationContext.ts
Expand Up @@ -179,7 +179,9 @@ export class BaseApplicationContext
return false;
}

get<T>(identifier: ObjectIdentifier, args?: any): T {
get<T>(identifier: { new (): T }, args?: any): T;
get<T>(identifier: ObjectIdentifier, args?: any): T;
get(identifier: any, args?: any): any {
// 因为在这里拿不到类名, NotFoundError 类的错误信息在 ManagedResolverFactory.ts createAsync 方法中增加错误类名
identifier = parsePrefix(identifier);

Expand All @@ -197,15 +199,17 @@ export class BaseApplicationContext
throw new Error(`${identifier} must use getAsync`);
}

return this.parent.get<T>(identifier, args);
return this.parent.get(identifier, args);
}
if (!definition) {
throw new NotFoundError(identifier);
}
return this.getManagedResolverFactory().create({ definition, args });
}

async getAsync<T>(identifier: ObjectIdentifier, args?: any): Promise<T> {
async getAsync<T>(identifier: { new (): T }, args?: any): Promise<T>;
async getAsync<T>(identifier: ObjectIdentifier, args?: any): Promise<T>;
async getAsync(identifier: any, args?: any): Promise<any> {
// 因为在这里拿不到类名, NotFoundError 类的错误信息在 ManagedResolverFactory.ts createAsync 方法中增加错误类名
identifier = parsePrefix(identifier);

Expand All @@ -215,7 +219,7 @@ export class BaseApplicationContext

const definition = this.registry.getDefinition(identifier);
if (!definition && this.parent) {
return this.parent.getAsync<T>(identifier, args);
return this.parent.getAsync(identifier, args);
}

if (!definition) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/interface.ts
Expand Up @@ -34,7 +34,9 @@ export interface IMessageSource {
export interface IObjectFactory {
registry: IObjectDefinitionRegistry;
isAsync(identifier: ObjectIdentifier): boolean;
get<T>(identifier: new () => T, args?: any): T;
get<T>(identifier: ObjectIdentifier, args?: any): T;
getAsync<T>(identifier: new () => T, args?: any): Promise<T>;
getAsync<T>(identifier: ObjectIdentifier, args?: any): Promise<T>;
}
/**
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/fixtures/fun_sample.ts
Expand Up @@ -45,13 +45,13 @@ export class AliSingleton {
}

export async function singletonFactory(context: IApplicationContext) {
const inst = await context.getAsync('aliSingleton');
return (inst as AliSingleton).getInstance();
const inst = await context.getAsync(AliSingleton);
return inst.getInstance();
}

export async function singletonFactory2(context: IApplicationContext) {
return async () => {
const inst = await context.getAsync('aliSingleton');
return (inst as AliSingleton).getInstance();
const inst = await context.getAsync<AliSingleton>('aliSingleton');
return inst.getInstance();
};
}
1 change: 0 additions & 1 deletion packages/midway/src/index.ts
Expand Up @@ -13,7 +13,6 @@ export const VERSION = require('../package.json').version;
*/
export const RELEASE = 'WANDA';


/**
* @deprecated Please use IWebMiddleware instead
*/
Expand Down

0 comments on commit d40de78

Please sign in to comment.