Skip to content

Commit d623b7b

Browse files
committed
Track whether initialization has run in wrapper
1 parent 54a163e commit d623b7b

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

packages/shared/src/open-feature.ts

Lines changed: 28 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
}
@@ -218,10 +238,13 @@ export abstract class OpenFeatureCommonAPI<
218238
): this;
219239

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

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

0 commit comments

Comments
 (0)