Skip to content

Commit c348345

Browse files
Julien HellerNoNameProvided
authored andcommitted
Added check for Service option
1 parent 84391ef commit c348345

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/container-instance.class.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Constructable } from './types/constructable.type';
66
import { ServiceIdentifier } from './types/service-identifier.type';
77
import { ServiceMetadata } from './interfaces/service-metadata.interface.';
88
import { AsyncInitializedService } from './types/AsyncInitializedService';
9+
import { MissingInitializedPromiseError } from './error/MissingInitializedPromiseError';
910

1011

1112
/**
@@ -495,8 +496,12 @@ export class ContainerInstance {
495496

496497
if (type) this.applyPropertyHandlers(type, value);
497498

498-
if (value instanceof AsyncInitializedService) {
499-
return new Promise(resolve => {
499+
if (value instanceof AsyncInitializedService || service.asyncInitialization) {
500+
return new Promise((resolve) => {
501+
if (!(value._initialized instanceof Promise) && service.asyncInitialization) {
502+
throw new MissingInitializedPromiseError(service.value);
503+
}
504+
500505
value._initialized.then(() => resolve(value));
501506
});
502507
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Thrown when DI cannot inject value into property decorated by @Inject decorator.
3+
*/
4+
export class MissingInitializedPromiseError extends Error {
5+
name = "MissingInitializedPromiseError";
6+
7+
constructor(value: any) {
8+
super(
9+
(value._initialized
10+
? `asyncInitialization: true was used, but ${value.name}#_initialized is not a Promise. `
11+
: `asyncInitialization: true was used, but ${value.name}#_initialized is undefined. `) +
12+
`You will need to either extend the abstract AsyncInitializedService class, or assign ` +
13+
`${value.name}#_initialized to a Promise in your class' constructor that resolves when all required ` +
14+
`initialization is complete.`
15+
);
16+
Object.setPrototypeOf(this, MissingInitializedPromiseError.prototype);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)