File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import {ObjectType} from "./types/ObjectType";
6
6
import { ServiceIdentifier } from "./types/ServiceIdentifier" ;
7
7
import { ServiceMetadata } from "./types/ServiceMetadata" ;
8
8
import { AsyncInitializedService } from "./types/AsyncInitializedService" ;
9
+ import { MissingInitializedPromiseError } from "./error/MissingInitializedPromiseError" ;
9
10
10
11
/**
11
12
* TypeDI can have multiple containers.
@@ -499,10 +500,13 @@ export class ContainerInstance {
499
500
if ( type )
500
501
this . applyPropertyHandlers ( type , value ) ;
501
502
502
- if ( value instanceof AsyncInitializedService ) {
503
- return new Promise ( ( resolve ) => {
504
- value . _initialized . then ( ( ) => resolve ( value ) ) ;
505
- } ) ;
503
+ if ( value instanceof AsyncInitializedService || service . asyncInitialization ) {
504
+ return new Promise ( ( resolve ) => {
505
+ if ( ! ( value . _initialized instanceof Promise ) && service . asyncInitialization ) {
506
+ throw new MissingInitializedPromiseError ( service . value ) ;
507
+ }
508
+ value . _initialized . then ( ( ) => resolve ( value ) ) ;
509
+ } ) ;
506
510
}
507
511
return Promise . resolve ( value ) ;
508
512
}
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