@@ -38,7 +38,7 @@ export class Provider<T extends Name> {
3838 string ,
3939 Deferred < NameServiceMapping [ T ] >
4040 > = new Map ( ) ;
41- private onInitCallbacks : Set < OnInitCallBack < T > > = new Set ( ) ;
41+ private onInitCallbacks : Map < string , Set < OnInitCallBack < T > > > = new Map ( ) ;
4242
4343 constructor (
4444 private readonly name : T ,
@@ -49,7 +49,7 @@ export class Provider<T extends Name> {
4949 * @param identifier A provider can provide mulitple instances of a service
5050 * if this.component.multipleInstances is true.
5151 */
52- get ( identifier : string = DEFAULT_ENTRY_NAME ) : Promise < NameServiceMapping [ T ] > {
52+ get ( identifier ? : string ) : Promise < NameServiceMapping [ T ] > {
5353 // if multipleInstances is not supported, use the default name
5454 const normalizedIdentifier = this . normalizeInstanceIdentifier ( identifier ) ;
5555
@@ -99,11 +99,11 @@ export class Provider<T extends Name> {
9999 identifier ?: string ;
100100 optional ?: boolean ;
101101 } ) : NameServiceMapping [ T ] | null {
102- const identifier = options ?. identifier ?? DEFAULT_ENTRY_NAME ;
103- const optional = options ?. optional ?? false ;
104-
105102 // 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 ;
107107
108108 if (
109109 this . isInitialized ( normalizedIdentifier ) ||
@@ -219,9 +219,9 @@ export class Provider<T extends Name> {
219219 }
220220
221221 initialize ( opts : InitializeOptions = { } ) : NameServiceMapping [ T ] {
222- const { instanceIdentifier = DEFAULT_ENTRY_NAME , options = { } } = opts ;
222+ const { options = { } } = opts ;
223223 const normalizedIdentifier = this . normalizeInstanceIdentifier (
224- instanceIdentifier
224+ opts . instanceIdentifier
225225 ) ;
226226 if ( this . isInitialized ( normalizedIdentifier ) ) {
227227 throw Error (
@@ -261,13 +261,24 @@ export class Provider<T extends Name> {
261261 * @param callback - a function that will be invoked after the provider has been initialized by calling provider.initialize().
262262 * The function is invoked SYNCHRONOUSLY, so it should not execute any longrunning tasks in order to not block the program.
263263 *
264+ * @param identifier An optional instance identifier
264265 * @returns a function to unregister the callback
265266 */
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+ }
268279
269280 return ( ) => {
270- this . onInitCallbacks . delete ( callback ) ;
281+ existingCallbacks . delete ( callback ) ;
271282 } ;
272283 }
273284
@@ -279,7 +290,11 @@ export class Provider<T extends Name> {
279290 instance : NameServiceMapping [ T ] ,
280291 identifier : string
281292 ) : 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 ) {
283298 try {
284299 callback ( instance , identifier ) ;
285300 } catch {
@@ -324,7 +339,9 @@ export class Provider<T extends Name> {
324339 return instance || null ;
325340 }
326341
327- private normalizeInstanceIdentifier ( identifier : string ) : string {
342+ private normalizeInstanceIdentifier (
343+ identifier : string = DEFAULT_ENTRY_NAME
344+ ) : string {
328345 if ( this . component ) {
329346 return this . component . multipleInstances ? identifier : DEFAULT_ENTRY_NAME ;
330347 } else {
0 commit comments