Skip to content

Conversation

@joker23
Copy link
Contributor

@joker23 joker23 commented Dec 12, 2025

Related issues

sdk-1653
sdk-1376
sdk-1663
sdk-1681

Describe the solution you've provided

  • Introduced ActiveContextTracker to manage the current active context and its serialized state.
  • Updated LDClientImpl to utilize the new context tracker for identifying and evaluating flags.
  • Added logic in BrowserClientImpl to read flags from bootstrap data during the initial identification process.
  • Added a new presetFlags function in client sdk common that allow flagstore to take in contexless data before initialization
  • Bootstrapped data will first preset the flag store so they can be evaluated before a full context is made
  • Added logic to suppress event creation if context is not validated (while the client only has preset data)

Additional context

  • eventually I would like to consolidate the context tracker with the tracking logic we have for waitForInitialization and have a general state tracker for client sdk common.
  • I am also open to suggestions on distinguishing between "no context" state and "pre initialized" state. Though, it seems like, at the moment, those 2 states are handled in the same way.

Supersedes #1024


Note

Enables evaluating bootstrapped flags before identify completes by presetting flags and tracking active context; suppresses events without a valid context.

  • Core (packages/shared/sdk-client):
    • Context Management: Introduce ActiveContextTracker and switch LDClientImpl to use it for pristine/checked contexts and identification promises.
    • Preset Flags & Evaluation: Add presetFlags in LDClientImpl and FlagManager to load flags without a context; allow evaluations before full initialization (log warnings) and suppress event emission when context is not valid.
    • Flag Updates: Update FlagUpdater to track the active Context object (not just key) and use it for change callbacks and upsert validation.
  • Browser (packages/sdk/browser):
    • Bootstrap Handling: On first identify, read bootstrap via readFlagsFromBootstrap and presetFlags so flags can be evaluated before identify completes; keep initialization result wiring and goal tracking.
  • Tests:
    • Add test to verify flags can be evaluated with bootstrap data before identify resolves.

Written by Cursor Bugbot for commit 2d45b52. This will update automatically on new commits. Configure here.

- Introduced `ActiveContextTracker` to manage the current active context and its serialized state.
- Updated `LDClientImpl` to utilize the new context tracker for identifying and evaluating flags.
- Added logic in `BrowserClientImpl` to read flags from bootstrap data during the initial identification process.

This change improves the SDK's ability to handle context and flag management, particularly during initialization.
@github-actions
Copy link
Contributor

@launchdarkly/browser size report
This is the brotli compressed size of the ESM build.
Compressed size: 169118 bytes
Compressed size limit: 200000
Uncompressed size: 789399 bytes

@github-actions
Copy link
Contributor

@launchdarkly/js-sdk-common size report
This is the brotli compressed size of the ESM build.
Compressed size: 25394 bytes
Compressed size limit: 26000
Uncompressed size: 124693 bytes

@github-actions
Copy link
Contributor

github-actions bot commented Dec 12, 2025

@launchdarkly/js-client-sdk-common size report
This is the brotli compressed size of the ESM build.
Compressed size: 18009 bytes
Compressed size limit: 20000
Uncompressed size: 92221 bytes

@github-actions
Copy link
Contributor

github-actions bot commented Dec 12, 2025

@launchdarkly/js-client-sdk size report
This is the brotli compressed size of the ESM build.
Compressed size: 22654 bytes
Compressed size limit: 25000
Uncompressed size: 78251 bytes

@joker23 joker23 marked this pull request as ready for review December 12, 2025 22:59
@joker23 joker23 requested a review from a team as a code owner December 12, 2025 22:59
this._eventFactoryDefault.unknownFlagEvent(flagKey, defVal, evalContext),
);

this.emitter.emit('error', this._activeContextTracker.getPristineContext(), error);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Error events emitted with undefined context during pre-initialization evaluation

When evaluating flags before identification completes (using preset bootstrap data), error events for "flag not found" or "wrong type" are emitted via emitter.emit('error', this._activeContextTracker.getPristineContext(), error) where getPristineContext() returns undefined. Previously, _variationInternal returned early when there was no context, so these error events were never emitted in the pre-initialization state. Error listeners that expect a defined context as the first argument may now receive undefined, potentially causing issues in downstream handlers.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as the error is probably due to non-existing context

@joker23 joker23 force-pushed the skz/sdk-1663/active-context-tracking branch 2 times, most recently from 53906ff to 48334f3 Compare December 15, 2025 15:56
@joker23 joker23 force-pushed the skz/sdk-1663/active-context-tracking branch from 48334f3 to 1c9cb11 Compare December 15, 2025 17:29
@joker23 joker23 force-pushed the skz/sdk-1663/active-context-tracking branch from 1c9cb11 to 2d45b52 Compare December 15, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants