@@ -7,7 +7,7 @@ import { IBasicClient, SplitIO } from '../types';
77import { validateAndTrackApiKey } from '../utils/inputValidation/apiKey' ;
88import { createLoggerAPI } from '../logger/sdkLogger' ;
99import { NEW_FACTORY , RETRIEVE_MANAGER } from '../logger/constants' ;
10- import { SDK_SPLITS_ARRIVED , SDK_SEGMENTS_ARRIVED , SDK_SPLITS_CACHE_LOADED } from '../readiness/constants' ;
10+ import { SDK_SPLITS_ARRIVED , SDK_SEGMENTS_ARRIVED } from '../readiness/constants' ;
1111import { objectAssign } from '../utils/lang/objectAssign' ;
1212import { strategyDebugFactory } from '../trackers/strategy/strategyDebug' ;
1313import { strategyOptimizedFactory } from '../trackers/strategy/strategyOptimized' ;
@@ -23,14 +23,20 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
2323 const { settings, platform, storageFactory, splitApiFactory, extraProps,
2424 syncManagerFactory, SignalListener, impressionsObserverFactory,
2525 integrationsManagerFactory, sdkManagerFactory, sdkClientMethodFactory,
26- filterAdapterFactory } = params ;
26+ filterAdapterFactory, lazyInit } = params ;
2727 const { log, sync : { impressionsMode } } = settings ;
2828
2929 // @TODO handle non-recoverable errors, such as, global `fetch` not available, invalid SDK Key, etc.
3030 // On non-recoverable errors, we should mark the SDK as destroyed and not start synchronization.
3131
32- // We will just log and allow for the SDK to end up throwing an SDK_TIMEOUT event for devs to handle.
33- validateAndTrackApiKey ( log , settings . core . authorizationKey ) ;
32+ // initialization
33+ let hasInit = false ;
34+ const initCallbacks : ( ( ) => void ) [ ] = [ ] ;
35+
36+ function whenInit ( cb : ( ) => void ) {
37+ if ( hasInit ) cb ( ) ;
38+ else initCallbacks . push ( cb ) ;
39+ }
3440
3541 const sdkReadinessManager = sdkReadinessManagerFactory ( platform . EventEmitter , settings ) ;
3642 const readiness = sdkReadinessManager . readinessManager ;
@@ -46,9 +52,6 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
4652 readiness . splits . emit ( SDK_SPLITS_ARRIVED ) ;
4753 readiness . segments . emit ( SDK_SEGMENTS_ARRIVED ) ;
4854 } ,
49- onReadyFromCacheCb : ( ) => {
50- readiness . splits . emit ( SDK_SPLITS_CACHE_LOADED ) ;
51- }
5255 } ) ;
5356 // @TODO add support for dataloader: `if (params.dataLoader) params.dataLoader(storage);`
5457 const clients : Record < string , IBasicClient > = { } ;
@@ -70,8 +73,8 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
7073 strategy = strategyDebugFactory ( observer ) ;
7174 }
7275
73- const impressionsTracker = impressionsTrackerFactory ( settings , storage . impressions , strategy , integrationsManager , storage . telemetry ) ;
74- const eventTracker = eventTrackerFactory ( settings , storage . events , integrationsManager , storage . telemetry ) ;
76+ const impressionsTracker = impressionsTrackerFactory ( settings , storage . impressions , strategy , whenInit , integrationsManager , storage . telemetry ) ;
77+ const eventTracker = eventTrackerFactory ( settings , storage . events , whenInit , integrationsManager , storage . telemetry ) ;
7578
7679 // splitApi is used by SyncManager and Browser signal listener
7780 const splitApi = splitApiFactory && splitApiFactory ( settings , platform , telemetryTracker ) ;
@@ -88,8 +91,21 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
8891 const clientMethod = sdkClientMethodFactory ( ctx ) ;
8992 const managerInstance = sdkManagerFactory ( settings , storage . splits , sdkReadinessManager ) ;
9093
91- syncManager && syncManager . start ( ) ;
92- signalListener && signalListener . start ( ) ;
94+
95+ function init ( ) {
96+ if ( hasInit ) return ;
97+ hasInit = true ;
98+
99+ // We will just log and allow for the SDK to end up throwing an SDK_TIMEOUT event for devs to handle.
100+ validateAndTrackApiKey ( log , settings . core . authorizationKey ) ;
101+ readiness . init ( ) ;
102+ uniqueKeysTracker && uniqueKeysTracker . start ( ) ;
103+ syncManager && syncManager . start ( ) ;
104+ signalListener && signalListener . start ( ) ;
105+
106+ initCallbacks . forEach ( ( cb ) => cb ( ) ) ;
107+ initCallbacks . length = 0 ;
108+ }
93109
94110 log . info ( NEW_FACTORY ) ;
95111
@@ -110,7 +126,7 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ICsSDK | SplitIO.
110126 settings,
111127
112128 destroy ( ) {
113- return Promise . all ( Object . keys ( clients ) . map ( key => clients [ key ] . destroy ( ) ) ) . then ( ( ) => { } ) ;
129+ return Promise . all ( Object . keys ( clients ) . map ( key => clients [ key ] . destroy ( ) ) ) . then ( ( ) => { } ) ;
114130 }
115- } , extraProps && extraProps ( ctx ) ) ;
131+ } , extraProps && extraProps ( ctx ) , lazyInit ? { init } : init ( ) ) ;
116132}
0 commit comments