Skip to content

Commit eb408ad

Browse files
Merge pull request #385 from splitio/SDKS-9171_sdk_ready_from_cache
[SDKS-9171] Emit SDK_READY_FROM_CACHE event alongside SDK_READY event in case it has not been emitted
2 parents e57e045 + 96e5aa6 commit eb408ad

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Added two new configuration options for the SDK storage in browsers when using storage type `LOCALSTORAGE`:
33
- `storage.expirationDays` to specify the validity period of the rollout cache.
44
- `storage.clearOnInit` to clear the rollout cache on SDK initialization.
5+
- Updated SDK_READY_FROM_CACHE event when using the `LOCALSTORAGE` storage type to be emitted alongside the SDK_READY event if it has not already been emitted.
56

67
2.1.0 (January 17, 2025)
78
- Added support for the new impressions tracking toggle available on feature flags, both respecting the setting and including the new field being returned on `SplitView` type objects. Read more in our docs.

src/readiness/__tests__/readinessManager.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import { EventEmitter } from '../../utils/MinEvents';
33
import { IReadinessManager } from '../types';
44
import { SDK_READY, SDK_UPDATE, SDK_SPLITS_ARRIVED, SDK_SEGMENTS_ARRIVED, SDK_READY_FROM_CACHE, SDK_SPLITS_CACHE_LOADED, SDK_READY_TIMED_OUT } from '../constants';
55
import { ISettings } from '../../types';
6+
import { STORAGE_LOCALSTORAGE } from '../../utils/constants';
67

78
const settings = {
89
startup: {
910
readyTimeout: 0,
11+
},
12+
storage: {
13+
type: STORAGE_LOCALSTORAGE
1014
}
1115
} as unknown as ISettings;
1216

@@ -67,7 +71,14 @@ test('READINESS MANAGER / Ready event should be fired once', () => {
6771
const readinessManager = readinessManagerFactory(EventEmitter, settings);
6872
let counter = 0;
6973

74+
readinessManager.gate.on(SDK_READY_FROM_CACHE, () => {
75+
expect(readinessManager.isReadyFromCache()).toBe(true);
76+
expect(readinessManager.isReady()).toBe(true);
77+
counter++;
78+
});
79+
7080
readinessManager.gate.on(SDK_READY, () => {
81+
expect(readinessManager.isReadyFromCache()).toBe(true);
7182
expect(readinessManager.isReady()).toBe(true);
7283
counter++;
7384
});
@@ -79,7 +90,7 @@ test('READINESS MANAGER / Ready event should be fired once', () => {
7990
readinessManager.splits.emit(SDK_SPLITS_ARRIVED);
8091
readinessManager.segments.emit(SDK_SEGMENTS_ARRIVED);
8192

82-
expect(counter).toBe(1); // should be called once
93+
expect(counter).toBe(2); // should be called once
8394
});
8495

8596
test('READINESS MANAGER / Ready from cache event should be fired once', (done) => {
@@ -88,6 +99,7 @@ test('READINESS MANAGER / Ready from cache event should be fired once', (done) =
8899

89100
readinessManager.gate.on(SDK_READY_FROM_CACHE, () => {
90101
expect(readinessManager.isReadyFromCache()).toBe(true);
102+
expect(readinessManager.isReady()).toBe(false);
91103
counter++;
92104
});
93105

src/readiness/readinessManager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ISettings } from '../types';
33
import SplitIO from '../../types/splitio';
44
import { SDK_SPLITS_ARRIVED, SDK_SPLITS_CACHE_LOADED, SDK_SEGMENTS_ARRIVED, SDK_READY_TIMED_OUT, SDK_READY_FROM_CACHE, SDK_UPDATE, SDK_READY } from './constants';
55
import { IReadinessEventEmitter, IReadinessManager, ISegmentsEventEmitter, ISplitsEventEmitter } from './types';
6+
import { STORAGE_LOCALSTORAGE } from '../utils/constants';
67

78
function splitsEventEmitterFactory(EventEmitter: new () => SplitIO.IEventEmitter): ISplitsEventEmitter {
89
const splitsEventEmitter = objectAssign(new EventEmitter(), {
@@ -114,6 +115,10 @@ export function readinessManagerFactory(
114115
isReady = true;
115116
try {
116117
syncLastUpdate();
118+
if (!isReadyFromCache && settings.storage?.type === STORAGE_LOCALSTORAGE) {
119+
isReadyFromCache = true;
120+
gate.emit(SDK_READY_FROM_CACHE);
121+
}
117122
gate.emit(SDK_READY);
118123
} catch (e) {
119124
// throws user callback exceptions in next tick

0 commit comments

Comments
 (0)