@@ -9,8 +9,8 @@ export const reactSdkVersion = `react-${reactSdkPackageJson.version}`;
9
9
export const Event = {
10
10
SDK_READY_TIMED_OUT : 'init::timeout' ,
11
11
SDK_READY : 'init::ready' ,
12
- SDK_UPDATE : 'state::update' ,
13
12
SDK_READY_FROM_CACHE : 'init::cache-ready' ,
13
+ SDK_UPDATE : 'state::update' ,
14
14
} ;
15
15
16
16
function parseKey ( key : SplitIO . SplitKey ) : SplitIO . SplitKey {
@@ -32,14 +32,22 @@ function buildInstanceId(key: any, trafficType: string | undefined) {
32
32
33
33
function mockClient ( _key : SplitIO . SplitKey , _trafficType ?: string ) {
34
34
// 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
+
39
46
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 ( ) ; } ) ;
43
51
44
52
let attributesCache = { } ;
45
53
@@ -72,21 +80,24 @@ function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
72
80
} ) ;
73
81
const ready : jest . Mock = jest . fn ( ( ) => {
74
82
return new Promise < void > ( ( res , rej ) => {
75
- if ( __isReady__ ) res ( ) ;
83
+ if ( isReady ) res ( ) ;
76
84
else { __emitter__ . on ( Event . SDK_READY , res ) ; }
77
- if ( __hasTimedout__ ) rej ( ) ;
85
+ if ( hasTimedout ) rej ( ) ;
78
86
else { __emitter__ . on ( Event . SDK_READY_TIMED_OUT , rej ) ; }
79
87
} ) ;
80
88
} ) ;
81
89
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,
87
97
} ) ;
88
98
const destroy : jest . Mock = jest . fn ( ( ) => {
89
- __isDestroyed__ = true ;
99
+ isDestroyed = true ;
100
+ syncLastUpdate ( ) ;
90
101
// __emitter__.removeAllListeners();
91
102
return Promise . resolve ( ) ;
92
103
} ) ;
@@ -108,7 +119,8 @@ function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
108
119
// Restore the mock client to its initial NO-READY status.
109
120
// Useful when you want to reuse the same mock between tests after emitting events or destroying the instance.
110
121
__restore ( ) {
111
- __isReady__ = __isReadyFromCache__ = __hasTimedout__ = __isDestroyed__ = undefined ;
122
+ isReady = isReadyFromCache = hasTimedout = isDestroyed = false ;
123
+ lastUpdate = 0 ;
112
124
}
113
125
} ) ;
114
126
}
0 commit comments