File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { Constructable } from './types/constructable.type';
6
6
import { ServiceIdentifier } from './types/service-identifier.type' ;
7
7
import { ServiceMetadata } from './interfaces/service-metadata.interface.' ;
8
8
import { AsyncInitializedService } from './types/AsyncInitializedService' ;
9
+ import { MissingInitializedPromiseError } from './error/MissingInitializedPromiseError' ;
9
10
10
11
11
12
/**
@@ -495,8 +496,12 @@ export class ContainerInstance {
495
496
496
497
if ( type ) this . applyPropertyHandlers ( type , value ) ;
497
498
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
+
500
505
value . _initialized . then ( ( ) => resolve ( value ) ) ;
501
506
} ) ;
502
507
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments