@@ -26,16 +26,23 @@ import { BrowserIdentifyOptions as LDIdentifyOptions } from './BrowserIdentifyOp
2626import { registerStateDetection } from './BrowserStateDetector' ;
2727import GoalManager from './goals/GoalManager' ;
2828import { Goal , isClick } from './goals/Goals' ;
29- import { LDClient } from './LDClient' ;
29+ import {
30+ LDClient ,
31+ LDWaitForInitializationComplete ,
32+ LDWaitForInitializationFailed ,
33+ LDWaitForInitializationOptions ,
34+ LDWaitForInitializationResult ,
35+ LDWaitForInitializationTimeout ,
36+ } from './LDClient' ;
3037import { LDPlugin } from './LDPlugin' ;
3138import validateBrowserOptions , { BrowserOptions , filterToBaseOptionsWithDefaults } from './options' ;
3239import BrowserPlatform from './platform/BrowserPlatform' ;
3340
3441class BrowserClientImpl extends LDClientImpl {
3542 private readonly _goalManager ?: GoalManager ;
3643 private readonly _plugins ?: LDPlugin [ ] ;
37- private _initializedPromise ?: Promise < void > ;
38- private _initResolve ?: ( ) => void ;
44+ private _initializedPromise ?: Promise < LDWaitForInitializationResult > ;
45+ private _initResolve ?: ( result : LDWaitForInitializationResult ) => void ;
3946 private _isInitialized : boolean = false ;
4047
4148 constructor (
@@ -219,13 +226,17 @@ class BrowserClientImpl extends LDClientImpl {
219226 const res = await super . identifyResult ( context , identifyOptionsWithUpdatedDefaults ) ;
220227 if ( res . status === 'completed' ) {
221228 this . _isInitialized = true ;
222- this . _initResolve ?.( ) ;
229+ this . _initResolve ?.( { status : 'complete' } ) ;
223230 }
224231 this . _goalManager ?. startTracking ( ) ;
225232 return res ;
226233 }
227234
228- waitForInitialization ( timeout : number = 5 ) : Promise < void > {
235+ waitForInitialization (
236+ options ?: LDWaitForInitializationOptions ,
237+ ) : Promise < LDWaitForInitializationResult > {
238+ const timeout = options ?. timeout ?? 5 ;
239+
229240 // An initialization promise is only created if someone is going to use that promise.
230241 // If we always created an initialization promise, and there was no call waitForInitialization
231242 // by the time the promise was rejected, then that would result in an unhandled promise
@@ -238,7 +249,9 @@ class BrowserClientImpl extends LDClientImpl {
238249 }
239250
240251 if ( this . _isInitialized ) {
241- return Promise . resolve ( ) ;
252+ return Promise . resolve ( {
253+ status : 'complete' ,
254+ } ) ;
242255 }
243256
244257 if ( ! this . _initializedPromise ) {
@@ -258,16 +271,25 @@ class BrowserClientImpl extends LDClientImpl {
258271 * @param logger A logger to log when the timeout expires.
259272 * @returns
260273 */
261- private _promiseWithTimeout ( basePromise : Promise < void > , timeout : number ) : Promise < void > {
274+ private _promiseWithTimeout (
275+ basePromise : Promise < LDWaitForInitializationResult > ,
276+ timeout : number ,
277+ ) : Promise < LDWaitForInitializationResult > {
262278 const cancelableTimeout = cancelableTimedPromise ( timeout , 'waitForInitialization' ) ;
263279 return Promise . race ( [
264- basePromise . then ( ( ) => cancelableTimeout . cancel ( ) ) ,
265- cancelableTimeout . promise ,
280+ basePromise . then ( ( res : LDWaitForInitializationResult ) => {
281+ cancelableTimeout . cancel ( ) ;
282+ return res ;
283+ } ) ,
284+ cancelableTimeout . promise
285+ // If the promise resolves without error, then the initialization completed successfully.
286+ // NOTE: this should never return as the resolution would only be triggered by the basePromise
287+ // being resolved.
288+ . then ( ( ) => ( { status : 'complete' } ) as LDWaitForInitializationComplete )
289+ . catch ( ( ) => ( { status : 'timeout' } ) as LDWaitForInitializationTimeout ) ,
266290 ] ) . catch ( ( reason ) => {
267- if ( reason instanceof LDTimeoutError ) {
268- this . logger ?. error ( reason . message ) ;
269- }
270- throw reason ;
291+ this . logger ?. error ( reason . message ) ;
292+ return { status : 'failed' , error : reason as Error } as LDWaitForInitializationFailed ;
271293 } ) ;
272294 }
273295
@@ -337,7 +359,8 @@ export function makeClient(
337359 close : ( ) => impl . close ( ) ,
338360 allFlags : ( ) => impl . allFlags ( ) ,
339361 addHook : ( hook : Hook ) => impl . addHook ( hook ) ,
340- waitForInitialization : ( timeout : number ) => impl . waitForInitialization ( timeout ) ,
362+ waitForInitialization : ( LDWaitForInitializationOptions ) =>
363+ impl . waitForInitialization ( LDWaitForInitializationOptions ) ,
341364 logger : impl . logger ,
342365 } ;
343366
0 commit comments