Skip to content

Commit 266f091

Browse files
Added lastUpdate property to ReadinessManager to keep track of the timestamp of the last SDK event
1 parent 39f2a92 commit 266f091

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
1.16.1 (July 10, 2024)
2+
- Added lastUpdate property to ReadinessManager to keep track of the timestamp of the last SDK event, used on React and Redux SDKs.
23
- Updated some transitive dependencies for vulnerability fixes.
34

45
1.16.0 (June 13, 2024)

src/readiness/readinessManager.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export function readinessManagerFactory(
3939
const segments: ISegmentsEventEmitter = segmentsEventEmitterFactory(EventEmitter);
4040
const gate: IReadinessEventEmitter = new EventEmitter();
4141

42+
let lastUpdate = 0;
43+
function syncLastUpdate() {
44+
const dateNow = Date.now();
45+
lastUpdate = dateNow > lastUpdate ? dateNow : lastUpdate + 1;
46+
}
47+
4248
// emit SDK_READY_FROM_CACHE
4349
let isReadyFromCache = false;
4450
if (splits.splitsCacheLoaded) isReadyFromCache = true; // ready from cache, but doesn't emit SDK_READY_FROM_CACHE
@@ -50,6 +56,7 @@ export function readinessManagerFactory(
5056
function timeout() {
5157
if (hasTimedout) return;
5258
hasTimedout = true;
59+
syncLastUpdate();
5360
gate.emit(SDK_READY_TIMED_OUT, 'Split SDK emitted SDK_READY_TIMED_OUT event.');
5461
}
5562

@@ -70,6 +77,7 @@ export function readinessManagerFactory(
7077
// Don't emit SDK_READY_FROM_CACHE if SDK_READY has been emitted
7178
if (!isReady) {
7279
try {
80+
syncLastUpdate();
7381
gate.emit(SDK_READY_FROM_CACHE);
7482
} catch (e) {
7583
// throws user callback exceptions in next tick
@@ -81,6 +89,7 @@ export function readinessManagerFactory(
8189
function checkIsReadyOrUpdate(diff: any) {
8290
if (isReady) {
8391
try {
92+
syncLastUpdate();
8493
gate.emit(SDK_UPDATE, diff);
8594
} catch (e) {
8695
// throws user callback exceptions in next tick
@@ -91,6 +100,7 @@ export function readinessManagerFactory(
91100
clearTimeout(readyTimeoutId);
92101
isReady = true;
93102
try {
103+
syncLastUpdate();
94104
gate.emit(SDK_READY);
95105
} catch (e) {
96106
// throws user callback exceptions in next tick
@@ -121,6 +131,7 @@ export function readinessManagerFactory(
121131

122132
destroy() {
123133
isDestroyed = true;
134+
syncLastUpdate();
124135

125136
segments.removeAllListeners();
126137
gate.removeAllListeners();
@@ -134,7 +145,8 @@ export function readinessManagerFactory(
134145
hasTimedout() { return hasTimedout; },
135146
isReadyFromCache() { return isReadyFromCache; },
136147
isDestroyed() { return isDestroyed; },
137-
isOperational() { return (isReady || isReadyFromCache) && !isDestroyed; }
148+
isOperational() { return (isReady || isReadyFromCache) && !isDestroyed; },
149+
lastUpdate() { return lastUpdate; }
138150
};
139151

140152
}

src/readiness/sdkReadinessManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export function sdkReadinessManagerFactory(
129129
isOperational: readinessManager.isOperational(),
130130
hasTimedout: readinessManager.hasTimedout(),
131131
isDestroyed: readinessManager.isDestroyed(),
132+
lastUpdate: readinessManager.lastUpdate(),
132133
};
133134
},
134135
}

src/readiness/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface IReadinessManager {
5353
hasTimedout(): boolean,
5454
isDestroyed(): boolean,
5555
isOperational(): boolean,
56+
lastUpdate(): number,
5657

5758
timeout(): void,
5859
setDestroyed(): void,

0 commit comments

Comments
 (0)