Skip to content

Commit

Permalink
Update shared client status to store isReadyFromCache property
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Sep 13, 2024
1 parent 31c5c76 commit a37e2c0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splitsoftware/splitio-redux",
"version": "1.14.0",
"version": "1.13.1-rc.5",
"description": "A library to easily use Split JS SDK with Redux and React Redux",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/helpers.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,17 @@ describe('getStatus', () => {
it('should return the status of the client associated to the provided key', () => {
initSplitSdk({ config: sdkBrowserConfig });
getTreatments({ key: 'user_2', splitNames: ['split_1'] });
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);
(splitSdk.factory as any).client('user_2').__emitter__.emit(Event.SDK_READY_FROM_CACHE);
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE);
(splitSdk.factory as any).client('user_2').__emitter__.emit(Event.SDK_READY);

// Main client
const MAIN_CLIENT_STATUS = { ...STATUS_INITIAL, isReady: true, isOperational: true, lastUpdate: (splitSdk.factory.client() as any).__getStatus().lastUpdate };
const MAIN_CLIENT_STATUS = { ...STATUS_INITIAL, isReadyFromCache: true, isOperational: true, lastUpdate: (splitSdk.factory.client() as any).__getStatus().lastUpdate };
expect(getStatus()).toEqual(MAIN_CLIENT_STATUS);
expect(getStatus(sdkBrowserConfig.core.key)).toEqual(MAIN_CLIENT_STATUS);
expect(getStatus({ matchingKey: sdkBrowserConfig.core.key as string, bucketingKey: '' })).toEqual(MAIN_CLIENT_STATUS);

// Client for user_2
const USER_2_STATUS = { ...STATUS_INITIAL, isReadyFromCache: true, isOperational: true, lastUpdate: (splitSdk.factory.client('user_2') as any).__getStatus().lastUpdate };
const USER_2_STATUS = { ...STATUS_INITIAL, isReady: true, isReadyFromCache: true, isOperational: true, lastUpdate: (splitSdk.factory.client('user_2') as any).__getStatus().lastUpdate };
expect(getStatus('user_2')).toEqual(USER_2_STATUS);
expect(getStatus({ matchingKey: 'user_2', bucketingKey: '' })).toEqual(USER_2_STATUS);

Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/utils/mockBrowserSplitSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export function mockSdk() {

return jest.fn((config: SplitIO.IBrowserSettings, __updateModules?: (modules: { settings: { version: string } }) => void) => {

// isReadyFromCache is a shared status among clients
let isReadyFromCache = false;

function mockClient(key?: SplitIO.SplitKey) {
// Readiness
let isReady = false;
let isReadyFromCache = false;
let hasTimedout = false;
let isDestroyed = false;
let lastUpdate = 0;
Expand Down
32 changes: 19 additions & 13 deletions src/asyncActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export function getTreatments(params: IGetTreatmentsParams): Action | (() => voi
client.evalOnReady.push(params);
}

// @TODO remove `evalOnReadyFromCache` config option, since `false` value has no effect on shared clients (they are ready from cache immediately) and on the main client if its ready from cache when `getTreatments` is called
// If the SDK is not ready from cache and flag `evalOnReadyFromCache`, it stores the action to execute when ready from cache
if (!status.isReadyFromCache && params.evalOnReadyFromCache) {
client.evalOnReadyFromCache.push(params);
Expand Down Expand Up @@ -241,19 +242,24 @@ export function getClient(splitSdk: ISplitSdk, key?: SplitIO.SplitKey): IClientN
if (splitSdk.dispatch) splitSdk.dispatch(splitTimedout(__getStatus(client).lastUpdate, key));
});

// On SDK timed out, dispatch `splitReadyFromCache` action
client.once(client.Event.SDK_READY_FROM_CACHE, function onReadyFromCache() {
if (!splitSdk.dispatch) return;

const lastUpdate = __getStatus(client).lastUpdate;
if (client.evalOnReadyFromCache.length) {
const treatments = __getTreatments(client, client.evalOnReadyFromCache);

splitSdk.dispatch(splitReadyFromCacheWithEvaluations(key || (splitSdk.config as SplitIO.IBrowserSettings).core.key, treatments, lastUpdate, key && true));
} else {
splitSdk.dispatch(splitReadyFromCache(lastUpdate, key));
}
});
// On SDK ready from cache, dispatch `splitReadyFromCache` action
const status = __getStatus(client);
if (status.isReadyFromCache) { // can be true immediately for shared clients
if (splitSdk.dispatch) splitSdk.dispatch(splitReadyFromCache(status.lastUpdate, key));
} else {
client.once(client.Event.SDK_READY_FROM_CACHE, function onReadyFromCache() {
if (!splitSdk.dispatch) return;

const lastUpdate = __getStatus(client).lastUpdate;
if (client.evalOnReadyFromCache.length) {
const treatments = __getTreatments(client, client.evalOnReadyFromCache);

splitSdk.dispatch(splitReadyFromCacheWithEvaluations(key || (splitSdk.config as SplitIO.IBrowserSettings).core.key, treatments, lastUpdate, key && true));
} else {
splitSdk.dispatch(splitReadyFromCache(lastUpdate, key));
}
});
}

// On SDK update, evaluate the registered `getTreatments` actions and dispatch `splitUpdate` action
client.on(client.Event.SDK_UPDATE, function onUpdate() {
Expand Down

0 comments on commit a37e2c0

Please sign in to comment.