-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpollingManagerSS.ts
More file actions
56 lines (46 loc) · 2.04 KB
/
pollingManagerSS.ts
File metadata and controls
56 lines (46 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { splitsSyncTaskFactory } from './syncTasks/splitsSyncTask';
import { segmentsSyncTaskFactory } from './syncTasks/segmentsSyncTask';
import { IPollingManager, ISegmentsSyncTask, ISplitsSyncTask } from './types';
import { POLLING_START, POLLING_STOP, LOG_PREFIX_SYNC_POLLING } from '../../logger/constants';
import { ISdkFactoryContextSync } from '../../sdkFactory/types';
/**
* Expose start / stop mechanism for pulling data from services.
*/
export function pollingManagerSSFactory(
params: ISdkFactoryContextSync
): IPollingManager {
const { splitApi, storage, readiness, settings } = params;
const log = settings.log;
const splitsSyncTask: ISplitsSyncTask = splitsSyncTaskFactory(splitApi.fetchSplitChanges, storage, readiness, settings);
const segmentsSyncTask: ISegmentsSyncTask = segmentsSyncTaskFactory(splitApi.fetchSegmentChanges, storage, readiness, settings);
return {
splitsSyncTask,
segmentsSyncTask,
// Start periodic fetching (polling)
start() {
log.info(POLLING_START);
log.debug(LOG_PREFIX_SYNC_POLLING + `Definitions will be refreshed each ${settings.scheduler.featuresRefreshRate} millis`);
log.debug(LOG_PREFIX_SYNC_POLLING + `Segments will be refreshed each ${settings.scheduler.segmentsRefreshRate} millis`);
const startingUp = splitsSyncTask.start();
if (startingUp) {
startingUp.then(() => {
if (splitsSyncTask.isRunning()) segmentsSyncTask.start();
});
}
},
// Stop periodic fetching (polling)
stop() {
log.info(POLLING_STOP);
if (splitsSyncTask.isRunning()) splitsSyncTask.stop();
if (segmentsSyncTask.isRunning()) segmentsSyncTask.stop();
},
// Used by SyncManager to know if running in polling mode.
isRunning: splitsSyncTask.isRunning,
syncAll() {
// fetch splits and segments. There is no need to catch this promise (`SplitChangesUpdater` is always resolved with a boolean value)
return splitsSyncTask.execute().then(() => {
return segmentsSyncTask.execute();
});
}
};
}