@@ -38,7 +38,7 @@ export class Provider<T extends Name> {
38
38
string ,
39
39
Deferred < NameServiceMapping [ T ] >
40
40
> = new Map ( ) ;
41
- private onInitCallbacks : Set < OnInitCallBack < T > > = new Set ( ) ;
41
+ private onInitCallbacks : Map < string , Set < OnInitCallBack < T > > > = new Map ( ) ;
42
42
43
43
constructor (
44
44
private readonly name : T ,
@@ -49,7 +49,7 @@ export class Provider<T extends Name> {
49
49
* @param identifier A provider can provide mulitple instances of a service
50
50
* if this.component.multipleInstances is true.
51
51
*/
52
- get ( identifier : string = DEFAULT_ENTRY_NAME ) : Promise < NameServiceMapping [ T ] > {
52
+ get ( identifier ? : string ) : Promise < NameServiceMapping [ T ] > {
53
53
// if multipleInstances is not supported, use the default name
54
54
const normalizedIdentifier = this . normalizeInstanceIdentifier ( identifier ) ;
55
55
@@ -99,11 +99,11 @@ export class Provider<T extends Name> {
99
99
identifier ?: string ;
100
100
optional ?: boolean ;
101
101
} ) : NameServiceMapping [ T ] | null {
102
- const identifier = options ?. identifier ?? DEFAULT_ENTRY_NAME ;
103
- const optional = options ?. optional ?? false ;
104
-
105
102
// if multipleInstances is not supported, use the default name
106
- const normalizedIdentifier = this . normalizeInstanceIdentifier ( identifier ) ;
103
+ const normalizedIdentifier = this . normalizeInstanceIdentifier (
104
+ options ?. identifier
105
+ ) ;
106
+ const optional = options ?. optional ?? false ;
107
107
108
108
if (
109
109
this . isInitialized ( normalizedIdentifier ) ||
@@ -219,9 +219,9 @@ export class Provider<T extends Name> {
219
219
}
220
220
221
221
initialize ( opts : InitializeOptions = { } ) : NameServiceMapping [ T ] {
222
- const { instanceIdentifier = DEFAULT_ENTRY_NAME , options = { } } = opts ;
222
+ const { options = { } } = opts ;
223
223
const normalizedIdentifier = this . normalizeInstanceIdentifier (
224
- instanceIdentifier
224
+ opts . instanceIdentifier
225
225
) ;
226
226
if ( this . isInitialized ( normalizedIdentifier ) ) {
227
227
throw Error (
@@ -261,13 +261,24 @@ export class Provider<T extends Name> {
261
261
* @param callback - a function that will be invoked after the provider has been initialized by calling provider.initialize().
262
262
* The function is invoked SYNCHRONOUSLY, so it should not execute any longrunning tasks in order to not block the program.
263
263
*
264
+ * @param identifier An optional instance identifier
264
265
* @returns a function to unregister the callback
265
266
*/
266
- onInit ( callback : OnInitCallBack < T > ) : ( ) => void {
267
- this . onInitCallbacks . add ( callback ) ;
267
+ onInit ( callback : OnInitCallBack < T > , identifier ?: string ) : ( ) => void {
268
+ const normalizedIdentifier = this . normalizeInstanceIdentifier ( identifier ) ;
269
+ const existingCallbacks =
270
+ this . onInitCallbacks . get ( normalizedIdentifier ) ??
271
+ new Set < OnInitCallBack < T > > ( ) ;
272
+ existingCallbacks . add ( callback ) ;
273
+ this . onInitCallbacks . set ( normalizedIdentifier , existingCallbacks ) ;
274
+
275
+ const existingInstance = this . instances . has ( normalizedIdentifier ) ;
276
+ if ( existingInstance ) {
277
+ callback ( existingInstance , normalizedIdentifier ) ;
278
+ }
268
279
269
280
return ( ) => {
270
- this . onInitCallbacks . delete ( callback ) ;
281
+ existingCallbacks . delete ( callback ) ;
271
282
} ;
272
283
}
273
284
@@ -279,7 +290,11 @@ export class Provider<T extends Name> {
279
290
instance : NameServiceMapping [ T ] ,
280
291
identifier : string
281
292
) : void {
282
- for ( const callback of this . onInitCallbacks ) {
293
+ const callbacks = this . onInitCallbacks . get ( identifier ) ;
294
+ if ( ! callbacks ) {
295
+ return ;
296
+ }
297
+ for ( const callback of callbacks ) {
283
298
try {
284
299
callback ( instance , identifier ) ;
285
300
} catch {
@@ -324,7 +339,9 @@ export class Provider<T extends Name> {
324
339
return instance || null ;
325
340
}
326
341
327
- private normalizeInstanceIdentifier ( identifier : string ) : string {
342
+ private normalizeInstanceIdentifier (
343
+ identifier : string = DEFAULT_ENTRY_NAME
344
+ ) : string {
328
345
if ( this . component ) {
329
346
return this . component . multipleInstances ? identifier : DEFAULT_ENTRY_NAME ;
330
347
} else {
0 commit comments