Skip to content

Conversation

@joker23
Copy link
Contributor

@joker23 joker23 commented Dec 3, 2025

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

  • feat: adding waitForInitialize to browser 4.x
  • test: adding unit tests for waitForInitilization

Describe the solution you've provided

Added waitForInitialization function to browser 4.x implementation.

Additional context

  • I kept the method signiture the same as 3.x but am open to updating it to align with what we have in the server sdks f({timeout: 5}) instead of f(5)
  • I "simplified" the way we are tracking for the initialization state to a boolean. It looks like our new identification would not be throwing which minimizes the reject conditions... I can add those back in case we get an error status returned? At that point we might want to just wait for the timeout to throw.
  • I put in a default 5 seconds timeout on the wait
  • I also added in an initialized event that a successful identify will emit, I figured that would be a way for developers to know if LDClient initialized even if the initialization goes past timeout.

Note

Introduces waitForInitialization to the browser client with a configurable timeout and explicit success/failed/timeout results, and emits an initialized event on successful identify, with comprehensive tests.

  • Browser SDK:
    • Initialization API: Add waitForInitialization(options) to LDClient with default 5s timeout, returning complete | failed | timeout results; supports multiple calls and immediate return if already resolved.
    • Timeout handling: Use cancelableTimedPromise and internal _promiseWithTimeout to race initialization vs timeout.
    • Identify integration: identifyResult now resolves the initialization promise as complete or failed and starts goal tracking.
    • Events: Add "initialized" event to emitter and emit it on successful identify.
    • Types: Define LDWaitForInitializationOptions/Result types and expose method in public client; wire through makeClient.
  • Compat:
    • Exclude waitForInitialization from the compat LDClient interface.
  • Tests:
    • Add unit tests for waitForInitialization covering success, timeout, and failure (before/after calling).

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

@joker23 joker23 requested a review from a team as a code owner December 3, 2025 22:18
@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2025

@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

github-actions bot commented Dec 3, 2025

@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 3, 2025

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

@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2025

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

@joker23 joker23 requested a review from kinyoklion December 5, 2025 17:20
@joker23 joker23 force-pushed the skz/sdk-1360/browser-4.x-waitforinit branch from d324724 to acf1ac7 Compare December 5, 2025 17:24
joker23 and others added 3 commits December 10, 2025 13:22
unit tests broke because we introduced retries on initial polls which timed out of initial wait tests (since those tests were using fake timers). This change would simply advance the fake timer so all retries would have been triggered.
} else if (res.status === 'error') {
this._initializeResult = { status: 'failed', error: res.error };
this._initResolve?.(this._initializeResult);
}
Copy link

Choose a reason for hiding this comment

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

Bug: Stale initialization state persists across multiple identify calls

The _initializeResult state is set when an identify call completes but is never reset when a new identify starts. If a user calls identify successfully, then calls identify again for a different context, and then calls waitForInitialization, the method will immediately return the stale result from the first identify rather than waiting for the in-progress second identify to complete. The initialization state (_initializeResult, _initializedPromise, and _initResolve) needs to be reset at the start of each identifyResult call.

Additional Locations (1)

Fix in Cursor Fix in Web

@joker23 joker23 force-pushed the skz/sdk-1360/browser-4.x-waitforinit branch from 71a20ce to 262c6fe Compare December 12, 2025 22:11
@joker23 joker23 merged commit 156532a into main Dec 12, 2025
37 checks passed
@joker23 joker23 deleted the skz/sdk-1360/browser-4.x-waitforinit branch December 12, 2025 22:29
@github-actions github-actions bot mentioned this pull request Dec 12, 2025
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.

3 participants