diff --git a/src/container-instance.class.ts b/src/container-instance.class.ts index 6dbf848a..b0005f25 100644 --- a/src/container-instance.class.ts +++ b/src/container-instance.class.ts @@ -147,8 +147,8 @@ export class ContainerInstance { */ getAsync(identifier: ServiceIdentifier): Promise { const globalContainer = Container.of(undefined); - let service = globalContainer.findService(identifier); - let scopedService = this.findService(identifier); + const service = globalContainer.findService(identifier); + const scopedService = this.findService(identifier); if (service && service.global === true) return this.getServiceValueAsync(identifier, service); @@ -472,7 +472,7 @@ export class ContainerInstance { if (service.factory instanceof Array) { // use special [Type, "create"] syntax to allow factory services // in this case Type instance will be obtained from Container and its method "create" will be called - value = ((await this.getAsync(service.factory[0])) as any)[service.factory[1]](...params); + value = (await this.getAsync(service.factory[0]))[service.factory[1]](...params); } else { // regular factory function value = service.factory(...params, this); @@ -488,6 +488,7 @@ export class ContainerInstance { // need to be injected, and user can use provided container to get instances he needs params.push(this); + // eslint-disable-next-line prefer-spread value = new (type.bind.apply(type, params))(); } diff --git a/src/error/MissingInitializedPromiseError.ts b/src/error/MissingInitializedPromiseError.ts index 21cc2f4d..7bd95428 100644 --- a/src/error/MissingInitializedPromiseError.ts +++ b/src/error/MissingInitializedPromiseError.ts @@ -2,17 +2,18 @@ * Thrown when user improperly uses the asyncInitialization Service option. */ export class MissingInitializedPromiseError extends Error { - name = "MissingInitializedPromiseError"; + name = 'MissingInitializedPromiseError'; - constructor(value: any) { - super( - (value.initialized - ? `asyncInitialization: true was used, but ${value.name}#initialized is not a Promise. ` - : `asyncInitialization: true was used, but ${value.name}#initialized is undefined. `) + - `You will need to either extend the abstract AsyncInitializedService class, or assign ` + - `${value.name}#initialized to a Promise in your class' constructor that resolves when all required ` + - `initialization is complete.` - ); - Object.setPrototypeOf(this, MissingInitializedPromiseError.prototype); - } + // TODO: User proper type + constructor(value: { name: string; initialized: boolean }) { + super( + (value.initialized + ? `asyncInitialization: true was used, but ${value.name}#initialized is not a Promise. ` + : `asyncInitialization: true was used, but ${value.name}#initialized is undefined. `) + + `You will need to either extend the abstract AsyncInitializedService class, or assign ` + + `${value.name}#initialized to a Promise in your class' constructor that resolves when all required ` + + `initialization is complete.` + ); + Object.setPrototypeOf(this, MissingInitializedPromiseError.prototype); + } } diff --git a/src/types/AsyncInitializedService.ts b/src/types/AsyncInitializedService.ts index 980aaff6..d4ad4b51 100644 --- a/src/types/AsyncInitializedService.ts +++ b/src/types/AsyncInitializedService.ts @@ -2,11 +2,11 @@ * Extend when declaring a service with asyncInitialization: true flag. */ export abstract class AsyncInitializedService { - public initialized: Promise; + public initialized: Promise; - constructor() { - this.initialized = this.initialize(); - } + constructor() { + this.initialized = this.initialize(); + } - protected abstract initialize(): Promise; + protected abstract initialize(): Promise; }