Repository navigation
Expand file tree
/
Copy pathnode.js
More file actions
66 lines (58 loc) · 1.91 KB
/
Copy pathnode.js
File metadata and controls
66 lines (58 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
import PushManagerFactory from './PushManager';
import FullProducerFactory from '../producer';
import logFactory from '../utils/logger';
import { PUSH_SUBSYSTEM_DOWN, PUSH_SUBSYSTEM_UP } from './constants';
const log = logFactory('splitio-sync:sync-manager');
/**
* Factory of SyncManager for node
*
* @param context main client context
*/
export default function NodeSyncManagerFactory(context) {
const producer = FullProducerFactory(context);
const settings = context.get(context.constants.SETTINGS);
// add producer into the context before creating the PushManager
context.put(context.constants.PRODUCER, producer);
const pushManager = settings.streamingEnabled ? PushManagerFactory(context) : undefined;
function startPolling() {
if (!producer.isRunning()) {
log.info('Streaming not available. Starting periodic fetch of data.');
producer.start();
} else {
log.info('Streaming couldn\'t connect. Continue periodic fetch of data.');
}
}
function stopPollingAndSyncAll() {
log.info('PUSH (re)connected. Syncing and stopping periodic fetch of data.');
// if polling, stop
if (producer.isRunning())
producer.stop();
syncAll();
}
function syncAll() {
// fetch splits and segments. There is no need to catch this promise (it is handled by `SplitChangesUpdater`)
producer.synchronizeSplits().then(() => {
producer.synchronizeSegment();
});
}
return {
start() {
// start syncing
if (pushManager) {
syncAll();
pushManager.on(PUSH_SUBSYSTEM_UP, stopPollingAndSyncAll);
pushManager.on(PUSH_SUBSYSTEM_DOWN, startPolling);
setTimeout(pushManager.start); // Run in next event-loop cycle as in browser
} else {
producer.start();
}
},
stop() {
// stop syncing
if (pushManager)
pushManager.stop();
if (producer.isRunning())
producer.stop();
}
};
}