Skip to content

Commit 0840683

Browse files
authored
Fix false browser initialization diagnostics (#747)
## Summary - track whether evaluated flag state is available in `FlagsClient` - mark state available before notifying `flagsUpdated` subscribers - avoid attaching `CLIENT_NOT_INITIALIZED` to valid evaluations delivered while client initialization is completing - add regression coverage for the initialization event ordering ## Verification - `cd packages/browser-sdk && ./node_modules/.bin/vitest run test/usage.test.ts` - `cd packages/browser-sdk && ../../node_modules/.bin/tsc --project tsconfig.build.json --noEmit` - `oxlint` and `oxfmt` on changed files ## Review - completed a simplification/refactor pass - completed a final diff review; no actionable findings
1 parent 78b16cb commit 0840683

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@reflag/browser-sdk": patch
3+
---
4+
5+
Avoid reporting `CLIENT_NOT_INITIALIZED` when a flag is evaluated from the initial flag state while client initialization is completing.

‎packages/browser-sdk/src/client.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ function shouldShowToolbar(opts: InitOptions) {
501501
*/
502502
export class ReflagClient {
503503
private state: State = "idle";
504-
private initializationFinished = false;
505504
private contextUpdateLoading = false;
506505
private readonly publishableKey: string;
507506
private context: ReflagContext;
@@ -740,7 +739,6 @@ export class ReflagClient {
740739
"ms" +
741740
(this.config.offline ? " (offline mode)" : ""),
742741
);
743-
this.initializationFinished = true;
744742
this.setState("initialized");
745743
}
746744

@@ -1422,9 +1420,7 @@ export class ReflagClient {
14221420
getFlag(flagKey: string): Flag {
14231421
const f = this.getFlags()[flagKey];
14241422
const evaluatedBeforeInitialization =
1425-
!this.initializationFinished &&
1426-
!this.config.offline &&
1427-
!this.config.bootstrapped;
1423+
!this.flagsClient.hasFlagState() && !this.config.offline;
14281424

14291425
// eslint-disable-next-line @typescript-eslint/no-this-alias
14301426
const self = this;

‎packages/browser-sdk/src/flag/flags.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export class FlagsClient {
298298
private initialized = false;
299299
private bootstrapped = false;
300300
private initializationComplete = false;
301+
private flagStateAvailable = false;
301302

302303
private rateLimiter: RateLimiter;
303304
private readonly logger: Logger;
@@ -425,6 +426,10 @@ export class FlagsClient {
425426
return this.fetchedFlags;
426427
}
427428

429+
hasFlagState(): boolean {
430+
return this.flagStateAvailable;
431+
}
432+
428433
requestOptInFlags() {
429434
this.optInFlagsRequested = true;
430435

@@ -545,6 +550,8 @@ export class FlagsClient {
545550
triggerEvent = true,
546551
flagStateVersion?: number,
547552
) {
553+
// Mark the state available before notifying consumers from updateFlags().
554+
this.flagStateAvailable = true;
548555
// Create a new fetched flags object making sure to clone the flags
549556
this.fetchedFlags = { ...fetchedFlags };
550557
this.fetchedFlagStateVersion = flagStateVersion;

‎packages/browser-sdk/test/usage.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,35 @@ describe(`sends "check" events `, () => {
485485
);
486486
});
487487

488+
it("does not add initialization diagnostics once flag state is available", async () => {
489+
const sendCheckEventSpy = vi.spyOn(
490+
FlagsClient.prototype,
491+
"sendCheckEvent",
492+
);
493+
const client = new ReflagClient({ publishableKey: KEY });
494+
const evaluatedDuringInitialization = vi.fn();
495+
496+
client.on("flagsUpdated", () => {
497+
expect(client.getState()).toBe("initializing");
498+
expect(client.getFlag("flagA").isEnabled).toBe(true);
499+
evaluatedDuringInitialization();
500+
});
501+
502+
await client.initialize();
503+
504+
expect(evaluatedDuringInitialization).toHaveBeenCalledOnce();
505+
expect(sendCheckEventSpy).toHaveBeenCalledWith(
506+
expect.objectContaining({
507+
action: "check-is-enabled",
508+
key: "flagA",
509+
value: true,
510+
version: 1,
511+
evaluationErrors: flagsResult.flagA.evaluationErrors,
512+
}),
513+
expect.any(Function),
514+
);
515+
});
516+
488517
it("does not add initialization diagnostics to bootstrapped evaluations", () => {
489518
const sendCheckEventSpy = vi.spyOn(
490519
FlagsClient.prototype,

0 commit comments

Comments
 (0)