@@ -9,8 +9,8 @@ export const reactSdkVersion = `react-${reactSdkPackageJson.version}`;
99export const Event = {
1010 SDK_READY_TIMED_OUT : 'init::timeout' ,
1111 SDK_READY : 'init::ready' ,
12- SDK_UPDATE : 'state::update' ,
1312 SDK_READY_FROM_CACHE : 'init::cache-ready' ,
13+ SDK_UPDATE : 'state::update' ,
1414} ;
1515
1616function parseKey ( key : SplitIO . SplitKey ) : SplitIO . SplitKey {
@@ -32,14 +32,22 @@ function buildInstanceId(key: any, trafficType: string | undefined) {
3232
3333function mockClient ( _key : SplitIO . SplitKey , _trafficType ?: string ) {
3434 // Readiness
35- let __isReady__ : boolean | undefined ;
36- let __isReadyFromCache__ : boolean | undefined ;
37- let __hasTimedout__ : boolean | undefined ;
38- let __isDestroyed__ : boolean | undefined ;
35+ let isReady = false ;
36+ let isReadyFromCache = false ;
37+ let hasTimedout = false ;
38+ let isDestroyed = false ;
39+ let lastUpdate = 0 ;
40+
41+ function syncLastUpdate ( ) {
42+ const dateNow = Date . now ( ) ;
43+ lastUpdate = dateNow > lastUpdate ? dateNow : lastUpdate + 1 ;
44+ }
45+
3946 const __emitter__ = new EventEmitter ( ) ;
40- __emitter__ . on ( Event . SDK_READY , ( ) => { __isReady__ = true ; } ) ;
41- __emitter__ . on ( Event . SDK_READY_FROM_CACHE , ( ) => { __isReadyFromCache__ = true ; } ) ;
42- __emitter__ . on ( Event . SDK_READY_TIMED_OUT , ( ) => { __hasTimedout__ = true ; } ) ;
47+ __emitter__ . on ( Event . SDK_READY , ( ) => { isReady = true ; syncLastUpdate ( ) ; } ) ;
48+ __emitter__ . on ( Event . SDK_READY_FROM_CACHE , ( ) => { isReadyFromCache = true ; syncLastUpdate ( ) ; } ) ;
49+ __emitter__ . on ( Event . SDK_READY_TIMED_OUT , ( ) => { hasTimedout = true ; syncLastUpdate ( ) ; } ) ;
50+ __emitter__ . on ( Event . SDK_UPDATE , ( ) => { syncLastUpdate ( ) ; } ) ;
4351
4452 let attributesCache = { } ;
4553
@@ -72,21 +80,24 @@ function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
7280 } ) ;
7381 const ready : jest . Mock = jest . fn ( ( ) => {
7482 return new Promise < void > ( ( res , rej ) => {
75- if ( __isReady__ ) res ( ) ;
83+ if ( isReady ) res ( ) ;
7684 else { __emitter__ . on ( Event . SDK_READY , res ) ; }
77- if ( __hasTimedout__ ) rej ( ) ;
85+ if ( hasTimedout ) rej ( ) ;
7886 else { __emitter__ . on ( Event . SDK_READY_TIMED_OUT , rej ) ; }
7987 } ) ;
8088 } ) ;
8189 const __getStatus = ( ) => ( {
82- isReady : __isReady__ || false ,
83- isReadyFromCache : __isReadyFromCache__ || false ,
84- hasTimedout : __hasTimedout__ || false ,
85- isDestroyed : __isDestroyed__ || false ,
86- isOperational : ( ( __isReady__ || __isReadyFromCache__ ) && ! __isDestroyed__ ) || false ,
90+ isReady,
91+ isReadyFromCache,
92+ isTimedout : hasTimedout && ! isReady ,
93+ hasTimedout,
94+ isDestroyed,
95+ isOperational : ( isReady || isReadyFromCache ) && ! isDestroyed ,
96+ lastUpdate,
8797 } ) ;
8898 const destroy : jest . Mock = jest . fn ( ( ) => {
89- __isDestroyed__ = true ;
99+ isDestroyed = true ;
100+ syncLastUpdate ( ) ;
90101 // __emitter__.removeAllListeners();
91102 return Promise . resolve ( ) ;
92103 } ) ;
@@ -108,7 +119,8 @@ function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
108119 // Restore the mock client to its initial NO-READY status.
109120 // Useful when you want to reuse the same mock between tests after emitting events or destroying the instance.
110121 __restore ( ) {
111- __isReady__ = __isReadyFromCache__ = __hasTimedout__ = __isDestroyed__ = undefined ;
122+ isReady = isReadyFromCache = hasTimedout = isDestroyed = false ;
123+ lastUpdate = 0 ;
112124 }
113125 } ) ;
114126}
0 commit comments