@@ -217,6 +217,47 @@ export abstract class OpenFeatureCommonAPI<
217217 contextOrUndefined ?: EvaluationContext ,
218218 ) : this;
219219
220+ protected initializeProviderForDomain ( wrapper : ProviderWrapper < P , AnyProviderStatus > , domain ?: string ) : Promise < void > | void {
221+ if ( typeof wrapper . provider . initialize !== 'function' ) {
222+ return ;
223+ }
224+
225+ return wrapper . provider
226+ . initialize ( domain ? ( this . _domainScopedContext . get ( domain ) ?? this . _context ) : this . _context )
227+ . then ( ( ) => {
228+ wrapper . status = this . _statusEnumType . READY ;
229+ // fetch the most recent event emitters, some may have been added during init
230+ this . getAssociatedEventEmitters ( domain ) . forEach ( ( emitter ) => {
231+ emitter ?. emit ( AllProviderEvents . Ready , { clientName : domain , domain, providerName : wrapper . provider . metadata . name } ) ;
232+ } ) ;
233+ this . _apiEmitter ?. emit ( AllProviderEvents . Ready , { clientName : domain , domain, providerName : wrapper . provider . metadata . name } ) ;
234+ } )
235+ . catch ( ( error ) => {
236+ // if this is a fatal error, transition to FATAL status
237+ if ( ( error as OpenFeatureError ) ?. code === ErrorCode . PROVIDER_FATAL ) {
238+ wrapper . status = this . _statusEnumType . FATAL ;
239+ } else {
240+ wrapper . status = this . _statusEnumType . ERROR ;
241+ }
242+ this . getAssociatedEventEmitters ( domain ) . forEach ( ( emitter ) => {
243+ emitter ?. emit ( AllProviderEvents . Error , {
244+ clientName : domain ,
245+ domain,
246+ providerName : wrapper . provider . metadata . name ,
247+ message : error ?. message ,
248+ } ) ;
249+ } ) ;
250+ this . _apiEmitter ?. emit ( AllProviderEvents . Error , {
251+ clientName : domain ,
252+ domain,
253+ providerName : wrapper . provider . metadata . name ,
254+ message : error ?. message ,
255+ } ) ;
256+ // rethrow after emitting error events, so that public methods can control error handling
257+ throw error ;
258+ } ) ;
259+ }
260+
220261 protected setAwaitableProvider ( domainOrProvider ?: string | P , providerOrUndefined ?: P ) : Promise < void > | void {
221262 const domain = stringOrUndefined ( domainOrProvider ) ;
222263 const provider = objectOrUndefined < P > ( domainOrProvider ) ?? objectOrUndefined < P > ( providerOrUndefined ) ;
@@ -250,43 +291,12 @@ export abstract class OpenFeatureCommonAPI<
250291 this . _statusEnumType ,
251292 ) ;
252293
253- // initialize the provider if it implements "initialize" and it's not already registered
254- if ( typeof provider . initialize === 'function' && ! this . allProviders . includes ( provider ) ) {
255- initializationPromise = provider
256- . initialize ?.( domain ? ( this . _domainScopedContext . get ( domain ) ?? this . _context ) : this . _context )
257- ?. then ( ( ) => {
258- wrappedProvider . status = this . _statusEnumType . READY ;
259- // fetch the most recent event emitters, some may have been added during init
260- this . getAssociatedEventEmitters ( domain ) . forEach ( ( emitter ) => {
261- emitter ?. emit ( AllProviderEvents . Ready , { clientName : domain , domain, providerName } ) ;
262- } ) ;
263- this . _apiEmitter ?. emit ( AllProviderEvents . Ready , { clientName : domain , domain, providerName } ) ;
264- } )
265- ?. catch ( ( error ) => {
266- // if this is a fatal error, transition to FATAL status
267- if ( ( error as OpenFeatureError ) ?. code === ErrorCode . PROVIDER_FATAL ) {
268- wrappedProvider . status = this . _statusEnumType . FATAL ;
269- } else {
270- wrappedProvider . status = this . _statusEnumType . ERROR ;
271- }
272- this . getAssociatedEventEmitters ( domain ) . forEach ( ( emitter ) => {
273- emitter ?. emit ( AllProviderEvents . Error , {
274- clientName : domain ,
275- domain,
276- providerName,
277- message : error ?. message ,
278- } ) ;
279- } ) ;
280- this . _apiEmitter ?. emit ( AllProviderEvents . Error , {
281- clientName : domain ,
282- domain,
283- providerName,
284- message : error ?. message ,
285- } ) ;
286- // rethrow after emitting error events, so that public methods can control error handling
287- throw error ;
288- } ) ;
289- } else {
294+ // initialize the provider if it's not already registered and it implements "initialize"
295+ if ( ! this . allProviders . includes ( provider ) ) {
296+ initializationPromise = this . initializeProviderForDomain ( wrappedProvider , domain ) ;
297+ }
298+
299+ if ( ! initializationPromise ) {
290300 wrappedProvider . status = this . _statusEnumType . READY ;
291301 emitters . forEach ( ( emitter ) => {
292302 emitter ?. emit ( AllProviderEvents . Ready , { clientName : domain , domain, providerName } ) ;
0 commit comments