diff --git a/src/sdkClient/clientAttributesDecoration.ts b/src/sdkClient/clientAttributesDecoration.ts index 5d55c7df..8d160108 100644 --- a/src/sdkClient/clientAttributesDecoration.ts +++ b/src/sdkClient/clientAttributesDecoration.ts @@ -5,7 +5,7 @@ import { ILogger } from '../logger/types'; import { objectAssign } from '../utils/lang/objectAssign'; /** - * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call + * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call */ export function clientAttributesDecoration(log: ILogger, client: TClient) { @@ -52,10 +52,10 @@ export function clientAttributesDecoration { @@ -101,8 +101,8 @@ export function clientAttributesDecoration { return { @@ -7,7 +11,16 @@ jest.mock('../submitters/submitterManager', () => { }; }); -import { syncManagerOnlineFactory } from '../syncManagerOnline'; +const paramsMock = { + platform: { + getEventSource: jest.fn(() => { return () => { }; }), + EventEmitter + }, + settings: fullSettings, + storage: {}, + readiness: {}, + start: jest.fn() +}; test('syncManagerOnline should start or not the submitter depending on user consent status', () => { const settings = { ...fullSettings }; @@ -40,3 +53,45 @@ test('syncManagerOnline should start or not the submitter depending on user cons expect(submitter.stop).toBeCalledTimes(4); }); + +test('syncManagerOnline should syncAll a single time in singleSync mode', () => { + const settings = { ...fullSettings }; + + settings.sync.singleSync = true; + + const pollingManager = { + syncAll: jest.fn(), + start: jest.fn(), + stop: jest.fn(), + isRunning: jest.fn() + }; + + const fetchAuthMock = jest.fn(); + + // @ts-ignore + const pollingSyncManager = syncManagerOnlineFactory(() => pollingManager)({ settings }); + + pollingSyncManager.start(); + + expect(pollingManager.start).not.toBeCalled(); + expect(pollingManager.syncAll).toBeCalledTimes(1); + + pollingSyncManager.stop(); + + const pushManager = pushManagerFactory({ // @ts-ignore + ...paramsMock, splitApi: { fetchAuth: fetchAuthMock } + }, {}) as IPushManager; + + pushManager.start = jest.fn(); + + // @ts-ignore + const pushingSyncManager = syncManagerOnlineFactory(() => pollingManager, () => pushManager)({ settings }); + + pushingSyncManager.start(); + + expect(pushManager.start).not.toBeCalled(); + expect(pollingManager.start).not.toBeCalled(); + + pushingSyncManager.stop(); + +}); diff --git a/src/sync/syncManagerOnline.ts b/src/sync/syncManagerOnline.ts index 61f0603d..73f5658a 100644 --- a/src/sync/syncManagerOnline.ts +++ b/src/sync/syncManagerOnline.ts @@ -89,15 +89,24 @@ export function syncManagerOnlineFactory( // start syncing splits and segments if (pollingManager) { - if (pushManager) { - // Doesn't call `syncAll` when the syncManager is resuming + + // If singleSync is enabled pushManager and pollingManager should not start + if (settings.sync.singleSync === true) { if (startFirstTime) { pollingManager.syncAll(); startFirstTime = false; } - pushManager.start(); } else { - pollingManager.start(); + if (pushManager) { + // Doesn't call `syncAll` when the syncManager is resuming + if (startFirstTime) { + pollingManager.syncAll(); + startFirstTime = false; + } + pushManager.start(); + } else { + pollingManager.start(); + } } } @@ -138,18 +147,22 @@ export function syncManagerOnlineFactory( return { isRunning: mySegmentsSyncTask.isRunning, start() { - if (pushManager) { - if (pollingManager!.isRunning()) { - // if doing polling, we must start the periodic fetch of data - if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); + if (settings.sync.singleSync === true) { + if (!readinessManager.isReady()) mySegmentsSyncTask.execute(); + } else { + if (pushManager) { + if (pollingManager!.isRunning()) { + // if doing polling, we must start the periodic fetch of data + if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); + } else { + // if not polling, we must execute the sync task for the initial fetch + // of segments since `syncAll` was already executed when starting the main client + mySegmentsSyncTask.execute(); + } + pushManager.add(matchingKey, mySegmentsSyncTask); } else { - // if not polling, we must execute the sync task for the initial fetch - // of segments since `syncAll` was already executed when starting the main client - mySegmentsSyncTask.execute(); + if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); } - pushManager.add(matchingKey, mySegmentsSyncTask); - } else { - if (storage.splits.usesSegments()) mySegmentsSyncTask.start(); } }, stop() {