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';
66import { ServiceIdentifier } from './types/service-identifier.type' ;
77import { ServiceMetadata } from './interfaces/service-metadata.interface.' ;
88import { 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 }
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