Skip to content

Commit 081b53e

Browse files
committed
Track whether initialization has run in wrapper
1 parent a8bc118 commit 081b53e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

packages/shared/src/open-feature.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type AnyProviderStatus = ClientProviderStatus | ServerProviderStatus;
2727
*/
2828
export class ProviderWrapper<P extends CommonProvider<AnyProviderStatus>, S extends AnyProviderStatus> {
2929
private _pendingContextChanges = 0;
30+
private _initializing = false;
31+
private _initialized = false;
3032

3133
constructor(
3234
private _provider: P,
@@ -49,6 +51,8 @@ export class ProviderWrapper<P extends CommonProvider<AnyProviderStatus>, S exte
4951
this._status = _statusEnumType.ERROR as S;
5052
}
5153
});
54+
55+
this._initialized = !(typeof _provider.initialize === 'function');
5256
}
5357

5458
get provider(): P {
@@ -67,6 +71,22 @@ export class ProviderWrapper<P extends CommonProvider<AnyProviderStatus>, S exte
6771
this._status = status;
6872
}
6973

74+
get initializing() {
75+
return this._initializing;
76+
}
77+
78+
set initializing(initializing: boolean) {
79+
this._initializing = initializing;
80+
}
81+
82+
get initialized() {
83+
return this._initialized;
84+
}
85+
86+
set initialized(initialized: boolean) {
87+
this._initialized = initialized;
88+
}
89+
7090
get allContextChangesSettled() {
7191
return this._pendingContextChanges === 0;
7292
}
@@ -219,10 +239,13 @@ export abstract class OpenFeatureCommonAPI<
219239

220240
protected initializeProviderForDomain(wrapper: ProviderWrapper<P, AnyProviderStatus>, domain?: string): Promise<void> | void {
221241
if (typeof wrapper.provider.initialize !== 'function'
222-
|| this.allProviders.includes(wrapper.provider)) {
242+
|| this.allProviders.includes(wrapper.provider)
243+
|| wrapper.initializing
244+
|| wrapper.initialized) {
223245
return;
224246
}
225247

248+
wrapper.initializing = true;
226249
return wrapper.provider
227250
.initialize(domain ? (this._domainScopedContext.get(domain) ?? this._context) : this._context)
228251
.then(() => {
@@ -256,6 +279,10 @@ export abstract class OpenFeatureCommonAPI<
256279
});
257280
// rethrow after emitting error events, so that public methods can control error handling
258281
throw error;
282+
})
283+
.finally(() => {
284+
wrapper.initialized = true;
285+
wrapper.initializing = false;
259286
});
260287
}
261288

@@ -291,6 +318,7 @@ export abstract class OpenFeatureCommonAPI<
291318
this._statusEnumType,
292319
);
293320

321+
// TODO: Don't initialize (nor set READY) if user requests delayed initialization until first context change
294322
// initialize the provider if it implements "initialize" and it's not already registered
295323
const initializationPromise = this.initializeProviderForDomain(wrappedProvider, domain);
296324
if (!initializationPromise) {

0 commit comments

Comments
 (0)