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

Introduce waitForInitialization with timeout/result types for the browser SDK, emit an initialized event on successful identify, and add tests for success/timeout/failure.

  • Browser SDK (4.x):
    • Add waitForInitialization(options) API returning LDWaitForInitializationResult (complete/failed/timeout) with default 5s timeout; implemented via internal init promise and cancelableTimedPromise.
    • Resolve init on identifyResult: set internal initialized state; resolve as complete on success or failed with error; expose via makeClient.
  • Events/Core:
    • Add and emit "initialized" event: new in LDEmitter; emitted on successful identify in LDClientImpl.
  • Types/Compat:
    • Define LDWaitForInitialization* option/result types in LDClient.
    • Exclude waitForInitialization from /compat LDClient surface.
  • Tests:
    • Add tests for waitForInitialization success, timeout, and failure behaviors.

Written by Cursor Bugbot for commit 0d640bf. 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: 22431 bytes
Compressed size limit: 25000
Uncompressed size: 77397 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
if (res.status === 'completed') {
this._isInitialized = true;
this._initResolve?.({ status: 'complete' });
}
Copy link

Choose a reason for hiding this comment

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

Bug: Initialization failure returns timeout instead of failed status

The identifyResult method only calls _initResolve when res.status === 'completed'. If identify fails with 'error', 'timeout', or 'shed' status, _initResolve is never called. This means waitForInitialization will always return { status: 'timeout' } for failed identifications, even though LDWaitForInitializationFailed type with status: 'failed' exists. Users cannot distinguish between an actual timeout and a genuine initialization failure.

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.

I think this is fine... if there is an error on one of the identifies then we will timeout here. Identification errors can be handled in the identify().catch() in case developers would want to try something else on error that could ultimately yield a successful identify within the waitForInitialization timeout.

Copy link
Member

Choose a reason for hiding this comment

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

We do need to make sure that the waitForInitialization method returns immediately if we aren't actually attempting to initialize anymore though, versus actually timing out.

return Promise.resolve({
status: 'complete',
});
}
Copy link

Choose a reason for hiding this comment

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

Bug: No failure state tracking for post-identify waitForInitialization calls

The code tracks successful initialization with _isInitialized but has no equivalent tracking for failed initialization. If identify fails before waitForInitialization is called, the failure state is lost because _initResolve doesn't exist yet and there's no _initFailed or _initError field to record the failure. When waitForInitialization is called afterward, it sees _isInitialized as false, creates a new promise, but since identify already completed, _initResolve will never be called. The user experiences a timeout instead of receiving the immediate failed status.

Additional Locations (1)

Fix in Cursor Fix in Web

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.
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