-
Notifications
You must be signed in to change notification settings - Fork 35
/
statusManager.js
100 lines (83 loc) · 2.85 KB
/
statusManager.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import logFactory from '../utils/logger';
const log = logFactory('');
const NEW_LISTENER_EVENT = 'newListener';
const REMOVE_LISTENER_EVENT = 'removeListener';
export default function callbackHandlerContext(context, forSharedClient = false) {
const gate = context.get(context.constants.READINESS).gate;
let readyCbCount = 0;
let isReady = false;
const {
SDK_READY,
SDK_READY_FROM_CACHE,
SDK_UPDATE,
SDK_READY_TIMED_OUT
} = gate;
const readyPromise = getReadyPromise();
gate.once(SDK_READY, () => {
if (readyCbCount === 0) log.warn('No listeners for SDK Readiness detected. Incorrect control treatments could have been logged if you called getTreatment/s while the SDK was not yet ready.');
context.put(context.constants.READY, true);
isReady = true;
});
gate.once(SDK_READY_FROM_CACHE, () => {
log.info('Split SDK is ready from cache.');
context.put(context.constants.READY_FROM_CACHE, true);
});
gate.on(REMOVE_LISTENER_EVENT, event => {
if (event === SDK_READY) readyCbCount--;
});
gate.on(NEW_LISTENER_EVENT, event => {
if (event === SDK_READY || event === SDK_READY_TIMED_OUT) {
if (isReady) {
log.error(`A listener was added for ${event === SDK_READY ? 'SDK_READY' : 'SDK_READY_TIMED_OUT'} on the SDK, which has already fired and won't be emitted again. The callback won't be executed.`);
} else if (event === SDK_READY) {
readyCbCount++;
}
}
});
function generateReadyPromise() {
let hasCatch = false;
const promise = new Promise((resolve, reject) => {
gate.once(SDK_READY, resolve);
gate.once(SDK_READY_TIMED_OUT, reject);
}).catch(function(err) {
// If the promise has a custom error handler, just propagate
if (hasCatch) throw err;
// If not handle the error to prevent unhandled promise exception.
log.error(err);
});
const originalThen = promise.then;
// Using .catch(fn) is the same than using .then(null, fn)
promise.then = function () {
if (arguments.length > 0 && typeof arguments[0] === 'function')
readyCbCount++;
if (arguments.length > 1 && typeof arguments[1] === 'function')
hasCatch = true;
return originalThen.apply(this, arguments);
};
return promise;
}
function getReadyPromise() {
if (forSharedClient) {
return Promise.resolve();
}
// Non-shared clients use the full blown ready promise implementation.
return generateReadyPromise();
}
return Object.assign(
// Expose Event Emitter functionality
Object.create(gate),
{
// Expose the event constants without changing the interface
Event: {
SDK_READY,
SDK_READY_FROM_CACHE,
SDK_UPDATE,
SDK_READY_TIMED_OUT,
},
// Expose the ready promise flag
ready: () => {
return readyPromise;
}
}
);
}