@splitsoftware/splitio-commons 1.5.1-rc.0 → 1.5.1-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.txt +1 -2
- package/cjs/integrations/ga/GaToSplit.js +11 -12
- package/cjs/services/splitApi.js +4 -4
- package/cjs/sync/polling/fetchers/segmentChangesFetcher.js +5 -5
- package/cjs/sync/polling/fetchers/splitChangesFetcher.js +2 -2
- package/cjs/sync/polling/updaters/segmentChangesUpdater.js +34 -34
- package/cjs/sync/polling/updaters/splitChangesUpdater.js +4 -3
- package/cjs/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +46 -46
- package/cjs/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +82 -64
- package/cjs/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +74 -58
- package/cjs/sync/streaming/UpdateWorkers/constants.js +6 -0
- package/cjs/sync/streaming/pushManager.js +6 -7
- package/cjs/sync/syncTask.js +13 -16
- package/cjs/utils/Backoff.js +3 -2
- package/esm/integrations/ga/GaToSplit.js +11 -12
- package/esm/services/splitApi.js +4 -4
- package/esm/sync/polling/fetchers/segmentChangesFetcher.js +5 -5
- package/esm/sync/polling/fetchers/splitChangesFetcher.js +2 -2
- package/esm/sync/polling/updaters/segmentChangesUpdater.js +34 -34
- package/esm/sync/polling/updaters/splitChangesUpdater.js +4 -3
- package/esm/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +46 -47
- package/esm/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +82 -65
- package/esm/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +74 -59
- package/esm/sync/streaming/UpdateWorkers/constants.js +3 -0
- package/esm/sync/streaming/pushManager.js +6 -7
- package/esm/sync/syncTask.js +13 -16
- package/esm/utils/Backoff.js +3 -2
- package/package.json +1 -1
- package/src/integrations/ga/GaToSplit.ts +8 -14
- package/src/integrations/ga/types.ts +2 -13
- package/src/services/splitApi.ts +4 -4
- package/src/services/types.ts +2 -2
- package/src/sync/polling/fetchers/segmentChangesFetcher.ts +5 -4
- package/src/sync/polling/fetchers/splitChangesFetcher.ts +2 -1
- package/src/sync/polling/fetchers/types.ts +2 -0
- package/src/sync/polling/pollingManagerCS.ts +5 -5
- package/src/sync/polling/syncTasks/mySegmentsSyncTask.ts +2 -2
- package/src/sync/polling/types.ts +14 -6
- package/src/sync/polling/updaters/mySegmentsUpdater.ts +4 -4
- package/src/sync/polling/updaters/segmentChangesUpdater.ts +34 -32
- package/src/sync/polling/updaters/splitChangesUpdater.ts +5 -4
- package/src/sync/streaming/SSEHandler/types.ts +0 -7
- package/src/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.ts +45 -54
- package/src/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.ts +78 -63
- package/src/sync/streaming/UpdateWorkers/SplitsUpdateWorker.ts +73 -61
- package/src/sync/streaming/UpdateWorkers/constants.ts +3 -0
- package/src/sync/streaming/UpdateWorkers/types.ts +2 -4
- package/src/sync/streaming/pushManager.ts +12 -12
- package/src/sync/streaming/types.ts +2 -2
- package/src/sync/syncTask.ts +16 -18
- package/src/utils/Backoff.ts +7 -2
- package/types/integrations/ga/types.d.ts +2 -13
- package/types/services/types.d.ts +2 -2
- package/types/sync/polling/fetchers/types.d.ts +2 -2
- package/types/sync/polling/syncTasks/mySegmentsSyncTask.d.ts +2 -2
- package/types/sync/polling/types.d.ts +11 -6
- package/types/sync/polling/updaters/segmentChangesUpdater.d.ts +1 -1
- package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -1
- package/types/sync/streaming/SSEHandler/types.d.ts +0 -4
- package/types/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.d.ts +3 -24
- package/types/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.d.ts +3 -23
- package/types/sync/streaming/UpdateWorkers/SplitsUpdateWorker.d.ts +6 -33
- package/types/sync/streaming/UpdateWorkers/types.d.ts +1 -2
- package/types/sync/streaming/types.d.ts +2 -2
- package/types/sync/syncTask.d.ts +2 -3
- package/types/utils/Backoff.d.ts +2 -0
- package/src/integrations/ga/autoRequire.js +0 -33
|
@@ -3,80 +3,96 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SplitsUpdateWorker = void 0;
|
|
4
4
|
var constants_1 = require("../../../readiness/constants");
|
|
5
5
|
var Backoff_1 = require("../../../utils/Backoff");
|
|
6
|
+
var constants_2 = require("./constants");
|
|
6
7
|
/**
|
|
7
|
-
* SplitsUpdateWorker
|
|
8
|
+
* SplitsUpdateWorker factory
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.segmentsSyncTask = segmentsSyncTask;
|
|
20
|
-
this.maxChangeNumber = 0;
|
|
21
|
-
this.handleNewEvent = false;
|
|
22
|
-
this.put = this.put.bind(this);
|
|
23
|
-
this.killSplit = this.killSplit.bind(this);
|
|
24
|
-
this.__handleSplitUpdateCall = this.__handleSplitUpdateCall.bind(this);
|
|
25
|
-
this.backoff = new Backoff_1.Backoff(this.__handleSplitUpdateCall);
|
|
26
|
-
}
|
|
27
|
-
// Private method
|
|
28
|
-
// Preconditions: this.splitsSyncTask.isSynchronizingSplits === false
|
|
29
|
-
SplitsUpdateWorker.prototype.__handleSplitUpdateCall = function () {
|
|
30
|
-
var _this = this;
|
|
31
|
-
if (this.maxChangeNumber > this.splitsCache.getChangeNumber()) {
|
|
32
|
-
this.handleNewEvent = false;
|
|
10
|
+
function SplitsUpdateWorker(log, splitsCache, splitsSyncTask, splitsEventEmitter, segmentsSyncTask) {
|
|
11
|
+
var maxChangeNumber = 0;
|
|
12
|
+
var handleNewEvent = false;
|
|
13
|
+
var isHandlingEvent;
|
|
14
|
+
var cdnBypass;
|
|
15
|
+
var backoff = new Backoff_1.Backoff(__handleSplitUpdateCall, constants_2.FETCH_BACKOFF_BASE, constants_2.FETCH_BACKOFF_MAX_WAIT);
|
|
16
|
+
function __handleSplitUpdateCall() {
|
|
17
|
+
isHandlingEvent = true;
|
|
18
|
+
if (maxChangeNumber > splitsCache.getChangeNumber()) {
|
|
19
|
+
handleNewEvent = false;
|
|
33
20
|
// fetch splits revalidating data if cached
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
|
|
21
|
+
splitsSyncTask.execute(true, cdnBypass ? maxChangeNumber : undefined).then(function () {
|
|
22
|
+
if (!isHandlingEvent)
|
|
23
|
+
return; // halt if `stop` has been called
|
|
24
|
+
if (handleNewEvent) {
|
|
25
|
+
__handleSplitUpdateCall();
|
|
37
26
|
}
|
|
38
27
|
else {
|
|
39
28
|
// fetch new registered segments for server-side API. Not retrying on error
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
29
|
+
if (segmentsSyncTask)
|
|
30
|
+
segmentsSyncTask.execute(true);
|
|
31
|
+
var attempts = backoff.attempts + 1;
|
|
32
|
+
if (maxChangeNumber <= splitsCache.getChangeNumber()) {
|
|
33
|
+
log.debug("Refresh completed" + (cdnBypass ? ' bypassing the CDN' : '') + " in " + attempts + " attempts.");
|
|
34
|
+
isHandlingEvent = false;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (attempts < constants_2.FETCH_BACKOFF_MAX_RETRIES) {
|
|
38
|
+
backoff.scheduleCall();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (cdnBypass) {
|
|
42
|
+
log.debug("No changes fetched after " + attempts + " attempts with CDN bypassed.");
|
|
43
|
+
isHandlingEvent = false;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
backoff.reset();
|
|
47
|
+
cdnBypass = true;
|
|
48
|
+
__handleSplitUpdateCall();
|
|
49
|
+
}
|
|
43
50
|
}
|
|
44
51
|
});
|
|
45
52
|
}
|
|
46
|
-
|
|
53
|
+
else {
|
|
54
|
+
isHandlingEvent = false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
47
57
|
/**
|
|
48
58
|
* Invoked by NotificationProcessor on SPLIT_UPDATE event
|
|
49
59
|
*
|
|
50
60
|
* @param {number} changeNumber change number of the SPLIT_UPDATE notification
|
|
51
61
|
*/
|
|
52
|
-
|
|
62
|
+
function put(_a) {
|
|
53
63
|
var changeNumber = _a.changeNumber;
|
|
54
|
-
var currentChangeNumber =
|
|
55
|
-
if (changeNumber <= currentChangeNumber || changeNumber <=
|
|
56
|
-
return;
|
|
57
|
-
this.maxChangeNumber = changeNumber;
|
|
58
|
-
this.handleNewEvent = true;
|
|
59
|
-
this.backoff.reset();
|
|
60
|
-
if (this.splitsSyncTask.isExecuting())
|
|
64
|
+
var currentChangeNumber = splitsCache.getChangeNumber();
|
|
65
|
+
if (changeNumber <= currentChangeNumber || changeNumber <= maxChangeNumber)
|
|
61
66
|
return;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
maxChangeNumber = changeNumber;
|
|
68
|
+
handleNewEvent = true;
|
|
69
|
+
cdnBypass = false;
|
|
70
|
+
if (backoff.timeoutID || !isHandlingEvent)
|
|
71
|
+
__handleSplitUpdateCall();
|
|
72
|
+
backoff.reset();
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
put: put,
|
|
76
|
+
/**
|
|
77
|
+
* Invoked by NotificationProcessor on SPLIT_KILL event
|
|
78
|
+
*
|
|
79
|
+
* @param {number} changeNumber change number of the SPLIT_UPDATE notification
|
|
80
|
+
* @param {string} splitName name of split to kill
|
|
81
|
+
* @param {string} defaultTreatment default treatment value
|
|
82
|
+
*/
|
|
83
|
+
killSplit: function (_a) {
|
|
84
|
+
var changeNumber = _a.changeNumber, splitName = _a.splitName, defaultTreatment = _a.defaultTreatment;
|
|
85
|
+
if (splitsCache.killLocally(splitName, defaultTreatment, changeNumber)) {
|
|
86
|
+
// trigger an SDK_UPDATE if Split was killed locally
|
|
87
|
+
splitsEventEmitter.emit(constants_1.SDK_SPLITS_ARRIVED, true);
|
|
88
|
+
}
|
|
89
|
+
// queues the SplitChanges fetch (only if changeNumber is newer)
|
|
90
|
+
put({ changeNumber: changeNumber });
|
|
91
|
+
},
|
|
92
|
+
stop: function () {
|
|
93
|
+
isHandlingEvent = false;
|
|
94
|
+
backoff.reset();
|
|
76
95
|
}
|
|
77
|
-
// queues the SplitChanges fetch (only if changeNumber is newer)
|
|
78
|
-
this.put({ changeNumber: changeNumber });
|
|
79
96
|
};
|
|
80
|
-
|
|
81
|
-
}());
|
|
97
|
+
}
|
|
82
98
|
exports.SplitsUpdateWorker = SplitsUpdateWorker;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FETCH_BACKOFF_MAX_RETRIES = exports.FETCH_BACKOFF_MAX_WAIT = exports.FETCH_BACKOFF_BASE = void 0;
|
|
4
|
+
exports.FETCH_BACKOFF_BASE = 10000; // backoff base starting at 10 seconds
|
|
5
|
+
exports.FETCH_BACKOFF_MAX_WAIT = 60000; // don't wait for more than 1 minute
|
|
6
|
+
exports.FETCH_BACKOFF_MAX_RETRIES = 10; // max retries
|
|
@@ -45,9 +45,9 @@ function pushManagerFactory(params, pollingManager) {
|
|
|
45
45
|
sseClient.setEventHandler(sseHandler);
|
|
46
46
|
// init workers
|
|
47
47
|
// MySegmentsUpdateWorker (client-side) are initiated in `add` method
|
|
48
|
-
var segmentsUpdateWorker = userKey ? undefined :
|
|
48
|
+
var segmentsUpdateWorker = userKey ? undefined : (0, SegmentsUpdateWorker_1.SegmentsUpdateWorker)(log, pollingManager.segmentsSyncTask, storage.segments);
|
|
49
49
|
// For server-side we pass the segmentsSyncTask, used by SplitsUpdateWorker to fetch new segments
|
|
50
|
-
var splitsUpdateWorker =
|
|
50
|
+
var splitsUpdateWorker = (0, SplitsUpdateWorker_1.SplitsUpdateWorker)(log, storage.splits, pollingManager.splitsSyncTask, readiness.splits, userKey ? undefined : pollingManager.segmentsSyncTask);
|
|
51
51
|
// [Only for client-side] map of hashes to user keys, to dispatch MY_SEGMENTS_UPDATE events to the corresponding MySegmentsUpdateWorker
|
|
52
52
|
var userKeyHashes = {};
|
|
53
53
|
// [Only for client-side] map of user keys to their corresponding hash64 and MySegmentsUpdateWorkers.
|
|
@@ -140,21 +140,20 @@ function pushManagerFactory(params, pollingManager) {
|
|
|
140
140
|
}
|
|
141
141
|
// cancel scheduled fetch retries of Splits, Segments, and MySegments Update Workers
|
|
142
142
|
function stopWorkers() {
|
|
143
|
-
splitsUpdateWorker.
|
|
143
|
+
splitsUpdateWorker.stop();
|
|
144
144
|
if (userKey)
|
|
145
145
|
(0, lang_1.forOwn)(clients, function (_a) {
|
|
146
146
|
var worker = _a.worker;
|
|
147
|
-
return worker.
|
|
147
|
+
return worker.stop();
|
|
148
148
|
});
|
|
149
149
|
else
|
|
150
|
-
segmentsUpdateWorker.
|
|
150
|
+
segmentsUpdateWorker.stop();
|
|
151
151
|
}
|
|
152
152
|
pushEmitter.on(constants_1.PUSH_SUBSYSTEM_DOWN, stopWorkers);
|
|
153
153
|
// Only required when streaming connects after a PUSH_RETRYABLE_ERROR.
|
|
154
154
|
// Otherwise it is unnecessary (e.g, STREAMING_RESUMED).
|
|
155
155
|
pushEmitter.on(constants_1.PUSH_SUBSYSTEM_UP, function () {
|
|
156
156
|
connectPushRetryBackoff.reset();
|
|
157
|
-
stopWorkers();
|
|
158
157
|
});
|
|
159
158
|
/** Fallback to polling without retry due to: STREAMING_DISABLED control event, or 'pushEnabled: false', or non-recoverable SSE and Authentication errors */
|
|
160
159
|
pushEmitter.on(constants_1.PUSH_NONRETRYABLE_ERROR, function handleNonRetryableError() {
|
|
@@ -290,7 +289,7 @@ function pushManagerFactory(params, pollingManager) {
|
|
|
290
289
|
var hash = (0, AuthClient_1.hashUserKey)(userKey);
|
|
291
290
|
if (!userKeyHashes[hash]) {
|
|
292
291
|
userKeyHashes[hash] = userKey;
|
|
293
|
-
clients[userKey] = { hash64: (0, murmur3_64_1.hash64)(userKey), worker:
|
|
292
|
+
clients[userKey] = { hash64: (0, murmur3_64_1.hash64)(userKey), worker: (0, MySegmentsUpdateWorker_1.MySegmentsUpdateWorker)(mySegmentsSyncTask) };
|
|
294
293
|
connectForNewClient = true; // we must reconnect on start, to listen the channel for the new user key
|
|
295
294
|
// Reconnects in case of a new client.
|
|
296
295
|
// Run in next event-loop cycle to save authentication calls
|
package/cjs/sync/syncTask.js
CHANGED
|
@@ -3,9 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.syncTaskFactory = void 0;
|
|
4
4
|
var constants_1 = require("../logger/constants");
|
|
5
5
|
/**
|
|
6
|
-
* Creates
|
|
7
|
-
* The task can be also executed by calling the "execute" method. Multiple
|
|
8
|
-
* For example, submitters executed on SDK destroy or full queue, while periodic execution is pending.
|
|
6
|
+
* Creates an object that handles the periodic execution of a given task via "start" and "stop" methods.
|
|
7
|
+
* The task can be also executed by calling the "execute" method. Multiple calls run sequentially to avoid race conditions (e.g., submitters executed on SDK destroy or full queue, while periodic execution is pending).
|
|
9
8
|
*
|
|
10
9
|
* @param log Logger instance.
|
|
11
10
|
* @param task Task to execute that returns a promise that NEVER REJECTS. Otherwise, periodic execution can result in Unhandled Promise Rejections.
|
|
@@ -15,8 +14,10 @@ var constants_1 = require("../logger/constants");
|
|
|
15
14
|
*/
|
|
16
15
|
function syncTaskFactory(log, task, period, taskName) {
|
|
17
16
|
if (taskName === void 0) { taskName = 'task'; }
|
|
18
|
-
//
|
|
19
|
-
var
|
|
17
|
+
// Flag that indicates if the task is executing
|
|
18
|
+
var executing = 0;
|
|
19
|
+
// Promise chain to resolve tasks sequentially
|
|
20
|
+
var promiseChain;
|
|
20
21
|
// flag that indicates if the task periodic execution has been started/stopped.
|
|
21
22
|
var running = false;
|
|
22
23
|
// Auxiliar counter used to avoid race condition when calling `start` & `stop` intermittently
|
|
@@ -30,19 +31,15 @@ function syncTaskFactory(log, task, period, taskName) {
|
|
|
30
31
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
31
32
|
args[_i] = arguments[_i];
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
if (pendingTask) {
|
|
35
|
-
return pendingTask.then(function () {
|
|
36
|
-
return execute.apply(void 0, args);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
// Execute task
|
|
34
|
+
executing++;
|
|
40
35
|
log.debug(constants_1.SYNC_TASK_EXECUTE, [taskName]);
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
// Update `promiseChain` with last promise, to run tasks serially
|
|
37
|
+
promiseChain = (promiseChain ? promiseChain.then(function () { return task.apply(void 0, args); }) : task.apply(void 0, args))
|
|
38
|
+
.then(function (result) {
|
|
39
|
+
executing--;
|
|
43
40
|
return result;
|
|
44
41
|
});
|
|
45
|
-
return
|
|
42
|
+
return promiseChain;
|
|
46
43
|
}
|
|
47
44
|
function periodicExecute(currentRunningId) {
|
|
48
45
|
return execute.apply(void 0, runningArgs).then(function (result) {
|
|
@@ -56,7 +53,7 @@ function syncTaskFactory(log, task, period, taskName) {
|
|
|
56
53
|
return {
|
|
57
54
|
execute: execute,
|
|
58
55
|
isExecuting: function () {
|
|
59
|
-
return
|
|
56
|
+
return executing > 0;
|
|
60
57
|
},
|
|
61
58
|
start: function () {
|
|
62
59
|
var args = [];
|
package/cjs/utils/Backoff.js
CHANGED
|
@@ -10,8 +10,8 @@ var Backoff = /** @class */ (function () {
|
|
|
10
10
|
* @param {number} maxMillis
|
|
11
11
|
*/
|
|
12
12
|
function Backoff(cb, baseMillis, maxMillis) {
|
|
13
|
-
this.baseMillis = baseMillis || Backoff.DEFAULT_BASE_MILLIS;
|
|
14
|
-
this.maxMillis = maxMillis || Backoff.DEFAULT_MAX_MILLIS;
|
|
13
|
+
this.baseMillis = Backoff.__TEST__BASE_MILLIS || baseMillis || Backoff.DEFAULT_BASE_MILLIS;
|
|
14
|
+
this.maxMillis = Backoff.__TEST__MAX_MILLIS || maxMillis || Backoff.DEFAULT_MAX_MILLIS;
|
|
15
15
|
this.attempts = 0;
|
|
16
16
|
this.cb = cb;
|
|
17
17
|
}
|
|
@@ -25,6 +25,7 @@ var Backoff = /** @class */ (function () {
|
|
|
25
25
|
if (this.timeoutID)
|
|
26
26
|
clearTimeout(this.timeoutID);
|
|
27
27
|
this.timeoutID = setTimeout(function () {
|
|
28
|
+
_this.timeoutID = undefined;
|
|
28
29
|
_this.cb();
|
|
29
30
|
}, delayInMillis);
|
|
30
31
|
this.attempts++;
|
|
@@ -7,24 +7,23 @@ var logNameMapper = 'ga-to-split:mapper';
|
|
|
7
7
|
/**
|
|
8
8
|
* Provides a plugin to use with analytics.js, accounting for the possibility
|
|
9
9
|
* that the global command queue has been renamed or not yet defined.
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
12
|
-
* @param pluginConstructor The plugin constructor function.
|
|
13
|
-
* @param log Logger instance.
|
|
14
|
-
* @param autoRequire If true, log error when auto-require script is not detected
|
|
10
|
+
* @param {string} pluginName The plugin name identifier.
|
|
11
|
+
* @param {Function} pluginConstructor The plugin constructor function.
|
|
15
12
|
*/
|
|
16
|
-
function providePlugin(
|
|
13
|
+
function providePlugin(pluginName, pluginConstructor) {
|
|
17
14
|
// get reference to global command queue. Init it if not defined yet.
|
|
15
|
+
// @ts-expect-error
|
|
18
16
|
var gaAlias = window.GoogleAnalyticsObject || 'ga';
|
|
19
17
|
window[gaAlias] = window[gaAlias] || function () {
|
|
20
|
-
|
|
18
|
+
var args = [];
|
|
19
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
20
|
+
args[_i] = arguments[_i];
|
|
21
|
+
}
|
|
22
|
+
(window[gaAlias].q = window[gaAlias].q || []).push(args);
|
|
21
23
|
};
|
|
22
24
|
// provides the plugin for use with analytics.js.
|
|
25
|
+
// @ts-expect-error
|
|
23
26
|
window[gaAlias]('provide', pluginName, pluginConstructor);
|
|
24
|
-
if (autoRequire && (!window[gaAlias].q || window[gaAlias].q.push === [].push)) {
|
|
25
|
-
// Expecting spy on ga.q push method but not found
|
|
26
|
-
log.error(logPrefix + 'integration is configured to autorequire the splitTracker plugin, but the necessary script does not seem to have run.');
|
|
27
|
-
}
|
|
28
27
|
}
|
|
29
28
|
// Default mapping: object used for building the default mapper from hits to Split events
|
|
30
29
|
var defaultMapping = {
|
|
@@ -241,5 +240,5 @@ export function GaToSplit(sdkOptions, params) {
|
|
|
241
240
|
return SplitTracker;
|
|
242
241
|
}());
|
|
243
242
|
// Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
|
|
244
|
-
providePlugin(
|
|
243
|
+
providePlugin('splitTracker', SplitTracker);
|
|
245
244
|
}
|
package/esm/services/splitApi.js
CHANGED
|
@@ -34,12 +34,12 @@ export function splitApiFactory(settings, platform, telemetryTracker) {
|
|
|
34
34
|
}
|
|
35
35
|
return splitHttpClient(url, undefined, telemetryTracker.trackHttp(TOKEN));
|
|
36
36
|
},
|
|
37
|
-
fetchSplitChanges: function (since, noCache) {
|
|
38
|
-
var url = urls.sdk + "/splitChanges?since=" + since + (filterQueryString || '');
|
|
37
|
+
fetchSplitChanges: function (since, noCache, till) {
|
|
38
|
+
var url = urls.sdk + "/splitChanges?since=" + since + (till ? '&till=' + till : '') + (filterQueryString || '');
|
|
39
39
|
return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SPLITS));
|
|
40
40
|
},
|
|
41
|
-
fetchSegmentChanges: function (since, segmentName, noCache) {
|
|
42
|
-
var url = urls.sdk + "/segmentChanges/" + segmentName + "?since=" + since;
|
|
41
|
+
fetchSegmentChanges: function (since, segmentName, noCache, till) {
|
|
42
|
+
var url = urls.sdk + "/segmentChanges/" + segmentName + "?since=" + since + (till ? '&till=' + till : '');
|
|
43
43
|
return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SEGMENT));
|
|
44
44
|
},
|
|
45
45
|
fetchMySegments: function (userMatchingKey, noCache) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { __spreadArray } from "tslib";
|
|
2
|
-
function greedyFetch(fetchSegmentChanges, since, segmentName, noCache) {
|
|
3
|
-
return fetchSegmentChanges(since, segmentName, noCache)
|
|
2
|
+
function greedyFetch(fetchSegmentChanges, since, segmentName, noCache, targetTill) {
|
|
3
|
+
return fetchSegmentChanges(since, segmentName, noCache, targetTill)
|
|
4
4
|
.then(function (resp) { return resp.json(); })
|
|
5
5
|
.then(function (json) {
|
|
6
6
|
var since = json.since, till = json.till;
|
|
@@ -8,7 +8,7 @@ function greedyFetch(fetchSegmentChanges, since, segmentName, noCache) {
|
|
|
8
8
|
return [json];
|
|
9
9
|
}
|
|
10
10
|
else {
|
|
11
|
-
return Promise.all([json, greedyFetch(fetchSegmentChanges, till, segmentName, noCache)]).then(function (flatMe) {
|
|
11
|
+
return Promise.all([json, greedyFetch(fetchSegmentChanges, till, segmentName, noCache, targetTill)]).then(function (flatMe) {
|
|
12
12
|
return __spreadArray([flatMe[0]], flatMe[1], true);
|
|
13
13
|
});
|
|
14
14
|
}
|
|
@@ -19,10 +19,10 @@ function greedyFetch(fetchSegmentChanges, since, segmentName, noCache) {
|
|
|
19
19
|
* SegmentChanges fetcher is a wrapper around `segmentChanges` API service that parses the response and handle errors and retries.
|
|
20
20
|
*/
|
|
21
21
|
export function segmentChangesFetcherFactory(fetchSegmentChanges) {
|
|
22
|
-
return function segmentChangesFetcher(since, segmentName, noCache,
|
|
22
|
+
return function segmentChangesFetcher(since, segmentName, noCache, till,
|
|
23
23
|
// Optional decorator for `fetchMySegments` promise, such as timeout or time tracker
|
|
24
24
|
decorator) {
|
|
25
|
-
var segmentsPromise = greedyFetch(fetchSegmentChanges, since, segmentName, noCache);
|
|
25
|
+
var segmentsPromise = greedyFetch(fetchSegmentChanges, since, segmentName, noCache, till);
|
|
26
26
|
if (decorator)
|
|
27
27
|
segmentsPromise = decorator(segmentsPromise);
|
|
28
28
|
return segmentsPromise;
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* SplitChanges fetcher is a wrapper around `splitChanges` API service that parses the response and handle errors.
|
|
4
4
|
*/
|
|
5
5
|
export function splitChangesFetcherFactory(fetchSplitChanges) {
|
|
6
|
-
return function splitChangesFetcher(since, noCache,
|
|
6
|
+
return function splitChangesFetcher(since, noCache, till,
|
|
7
7
|
// Optional decorator for `fetchSplitChanges` promise, such as timeout or time tracker
|
|
8
8
|
decorator) {
|
|
9
|
-
var splitsPromise = fetchSplitChanges(since, noCache);
|
|
9
|
+
var splitsPromise = fetchSplitChanges(since, noCache, till);
|
|
10
10
|
if (decorator)
|
|
11
11
|
splitsPromise = decorator(splitsPromise);
|
|
12
12
|
return splitsPromise.then(function (resp) { return resp.json(); });
|
|
@@ -15,54 +15,54 @@ import { thenable } from '../../../utils/promise/thenable';
|
|
|
15
15
|
*/
|
|
16
16
|
export function segmentChangesUpdaterFactory(log, segmentChangesFetcher, segments, readiness) {
|
|
17
17
|
var readyOnAlreadyExistentState = true;
|
|
18
|
+
function updateSegment(segmentName, noCache, till, fetchOnlyNew) {
|
|
19
|
+
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Processing segment " + segmentName);
|
|
20
|
+
var sincePromise = Promise.resolve(segments.getChangeNumber(segmentName));
|
|
21
|
+
return sincePromise.then(function (since) {
|
|
22
|
+
// if fetchOnlyNew flag, avoid processing already fetched segments
|
|
23
|
+
if (fetchOnlyNew && since !== -1)
|
|
24
|
+
return -1;
|
|
25
|
+
return segmentChangesFetcher(since, segmentName, noCache, till).then(function (changes) {
|
|
26
|
+
var changeNumber = -1;
|
|
27
|
+
var results = [];
|
|
28
|
+
changes.forEach(function (x) {
|
|
29
|
+
if (x.added.length > 0)
|
|
30
|
+
results.push(segments.addToSegment(segmentName, x.added));
|
|
31
|
+
if (x.removed.length > 0)
|
|
32
|
+
results.push(segments.removeFromSegment(segmentName, x.removed));
|
|
33
|
+
if (x.added.length > 0 || x.removed.length > 0) {
|
|
34
|
+
results.push(segments.setChangeNumber(segmentName, x.till));
|
|
35
|
+
changeNumber = x.till;
|
|
36
|
+
}
|
|
37
|
+
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Processed " + segmentName + " with till = " + x.till + ". Added: " + x.added.length + ". Removed: " + x.removed.length);
|
|
38
|
+
});
|
|
39
|
+
// If at least one storage operation result is a promise, join all in a single promise.
|
|
40
|
+
if (results.some(function (result) { return thenable(result); }))
|
|
41
|
+
return Promise.all(results).then(function () { return changeNumber; });
|
|
42
|
+
return changeNumber;
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
18
46
|
/**
|
|
19
47
|
* Segments updater returns a promise that resolves with a `false` boolean value if it fails at least to fetch a segment or synchronize it with the storage.
|
|
20
48
|
* Thus, a false result doesn't imply that SDK_SEGMENTS_ARRIVED was not emitted.
|
|
21
49
|
* Returned promise will not be rejected.
|
|
22
50
|
*
|
|
23
|
-
* @param {string[] | undefined} segmentNames list of segment names to fetch. By passing `undefined` it fetches the list of segments registered at the storage
|
|
24
|
-
* @param {boolean | undefined} noCache true to revalidate data to fetch on a SEGMENT_UPDATE notifications.
|
|
25
51
|
* @param {boolean | undefined} fetchOnlyNew if true, only fetch the segments that not exists, i.e., which `changeNumber` is equal to -1.
|
|
26
52
|
* This param is used by SplitUpdateWorker on server-side SDK, to fetch new registered segments on SPLIT_UPDATE notifications.
|
|
53
|
+
* @param {string | undefined} segmentName segment name to fetch. By passing `undefined` it fetches the list of segments registered at the storage
|
|
54
|
+
* @param {boolean | undefined} noCache true to revalidate data to fetch on a SEGMENT_UPDATE notifications.
|
|
55
|
+
* @param {number | undefined} till till target for the provided segmentName, for CDN bypass.
|
|
27
56
|
*/
|
|
28
|
-
return function segmentChangesUpdater(
|
|
57
|
+
return function segmentChangesUpdater(fetchOnlyNew, segmentName, noCache, till) {
|
|
29
58
|
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Started segments update");
|
|
30
59
|
// If not a segment name provided, read list of available segments names to be updated.
|
|
31
|
-
var segmentsPromise = Promise.resolve(
|
|
60
|
+
var segmentsPromise = Promise.resolve(segmentName ? [segmentName] : segments.getRegisteredSegments());
|
|
32
61
|
return segmentsPromise.then(function (segmentNames) {
|
|
33
62
|
// Async fetchers are collected here.
|
|
34
63
|
var updaters = [];
|
|
35
|
-
var _loop_1 = function (index) {
|
|
36
|
-
var segmentName = segmentNames[index];
|
|
37
|
-
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Processing segment " + segmentName);
|
|
38
|
-
var sincePromise = Promise.resolve(segments.getChangeNumber(segmentName));
|
|
39
|
-
updaters.push(sincePromise.then(function (since) {
|
|
40
|
-
// if fetchOnlyNew flag, avoid processing already fetched segments
|
|
41
|
-
if (fetchOnlyNew && since !== -1)
|
|
42
|
-
return -1;
|
|
43
|
-
return segmentChangesFetcher(since, segmentName, noCache).then(function (changes) {
|
|
44
|
-
var changeNumber = -1;
|
|
45
|
-
var results = [];
|
|
46
|
-
changes.forEach(function (x) {
|
|
47
|
-
if (x.added.length > 0)
|
|
48
|
-
results.push(segments.addToSegment(segmentName, x.added));
|
|
49
|
-
if (x.removed.length > 0)
|
|
50
|
-
results.push(segments.removeFromSegment(segmentName, x.removed));
|
|
51
|
-
if (x.added.length > 0 || x.removed.length > 0) {
|
|
52
|
-
results.push(segments.setChangeNumber(segmentName, x.till));
|
|
53
|
-
changeNumber = x.till;
|
|
54
|
-
}
|
|
55
|
-
log.debug(LOG_PREFIX_SYNC_SEGMENTS + "Processed " + segmentName + " with till = " + x.till + ". Added: " + x.added.length + ". Removed: " + x.removed.length);
|
|
56
|
-
});
|
|
57
|
-
// If at least one storage operation result is a promise, join all in a single promise.
|
|
58
|
-
if (results.some(function (result) { return thenable(result); }))
|
|
59
|
-
return Promise.all(results).then(function () { return changeNumber; });
|
|
60
|
-
return changeNumber;
|
|
61
|
-
});
|
|
62
|
-
}));
|
|
63
|
-
};
|
|
64
64
|
for (var index = 0; index < segmentNames.length; index++) {
|
|
65
|
-
|
|
65
|
+
updaters.push(updateSegment(segmentNames[index], noCache, till, fetchOnlyNew));
|
|
66
66
|
}
|
|
67
67
|
return Promise.all(updaters).then(function (shouldUpdateFlags) {
|
|
68
68
|
// if at least one segment fetch succeeded, mark segments ready
|
|
@@ -79,16 +79,17 @@ export function splitChangesUpdaterFactory(log, splitChangesFetcher, splits, seg
|
|
|
79
79
|
* Returned promise will not be rejected.
|
|
80
80
|
*
|
|
81
81
|
* @param {boolean | undefined} noCache true to revalidate data to fetch
|
|
82
|
+
* @param {boolean | undefined} till query param to bypass CDN requests
|
|
82
83
|
*/
|
|
83
|
-
return function splitChangesUpdater(noCache) {
|
|
84
|
+
return function splitChangesUpdater(noCache, till) {
|
|
84
85
|
/**
|
|
85
86
|
* @param {number} since current changeNumber at splitsCache
|
|
86
|
-
* @param {number} retry current number of retry
|
|
87
|
+
* @param {number} retry current number of retry attempts
|
|
87
88
|
*/
|
|
88
89
|
function _splitChangesUpdater(since, retry) {
|
|
89
90
|
if (retry === void 0) { retry = 0; }
|
|
90
91
|
log.debug(SYNC_SPLITS_FETCH, [since]);
|
|
91
|
-
var fetcherPromise = splitChangesFetcher(since, noCache, _promiseDecorator)
|
|
92
|
+
var fetcherPromise = splitChangesFetcher(since, noCache, till, _promiseDecorator)
|
|
92
93
|
.then(function (splitChanges) {
|
|
93
94
|
startingUp = false;
|
|
94
95
|
var mutation = computeSplitsMutation(splitChanges.splits);
|
|
@@ -1,58 +1,57 @@
|
|
|
1
1
|
import { Backoff } from '../../../utils/Backoff';
|
|
2
2
|
/**
|
|
3
|
-
* MySegmentsUpdateWorker
|
|
3
|
+
* MySegmentsUpdateWorker factory
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.backoff = new Backoff(this.__handleMySegmentsUpdateCall);
|
|
18
|
-
}
|
|
19
|
-
// Private method
|
|
20
|
-
// Precondition: this.mySegmentsSyncTask.isSynchronizingMySegments === false
|
|
21
|
-
MySegmentsUpdateWorker.prototype.__handleMySegmentsUpdateCall = function () {
|
|
22
|
-
var _this = this;
|
|
23
|
-
if (this.maxChangeNumber > this.currentChangeNumber) {
|
|
24
|
-
this.handleNewEvent = false;
|
|
25
|
-
var currentMaxChangeNumber_1 = this.maxChangeNumber;
|
|
5
|
+
export function MySegmentsUpdateWorker(mySegmentsSyncTask) {
|
|
6
|
+
var maxChangeNumber = 0; // keeps the maximum changeNumber among queued events
|
|
7
|
+
var currentChangeNumber = -1;
|
|
8
|
+
var handleNewEvent = false;
|
|
9
|
+
var isHandlingEvent;
|
|
10
|
+
var _segmentsData; // keeps the segmentsData (if included in notification payload) from the queued event with maximum changeNumber
|
|
11
|
+
var backoff = new Backoff(__handleMySegmentsUpdateCall);
|
|
12
|
+
function __handleMySegmentsUpdateCall() {
|
|
13
|
+
isHandlingEvent = true;
|
|
14
|
+
if (maxChangeNumber > currentChangeNumber) {
|
|
15
|
+
handleNewEvent = false;
|
|
16
|
+
var currentMaxChangeNumber_1 = maxChangeNumber;
|
|
26
17
|
// fetch mySegments revalidating data if cached
|
|
27
|
-
|
|
18
|
+
mySegmentsSyncTask.execute(_segmentsData, true).then(function (result) {
|
|
19
|
+
if (!isHandlingEvent)
|
|
20
|
+
return; // halt if `stop` has been called
|
|
28
21
|
if (result !== false) // Unlike `Splits|SegmentsUpdateWorker`, we cannot use `mySegmentsCache.getChangeNumber` since `/mySegments` endpoint doesn't provide this value.
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
22
|
+
currentChangeNumber = Math.max(currentChangeNumber, currentMaxChangeNumber_1); // use `currentMaxChangeNumber`, in case that `maxChangeNumber` was updated during fetch.
|
|
23
|
+
if (handleNewEvent) {
|
|
24
|
+
__handleMySegmentsUpdateCall();
|
|
32
25
|
}
|
|
33
26
|
else {
|
|
34
|
-
|
|
27
|
+
backoff.scheduleCall();
|
|
35
28
|
}
|
|
36
29
|
});
|
|
37
30
|
}
|
|
31
|
+
else {
|
|
32
|
+
isHandlingEvent = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
/**
|
|
37
|
+
* Invoked by NotificationProcessor on MY_SEGMENTS_UPDATE event
|
|
38
|
+
*
|
|
39
|
+
* @param {number} changeNumber change number of the MY_SEGMENTS_UPDATE notification
|
|
40
|
+
* @param {SegmentsData | undefined} segmentsData might be undefined
|
|
41
|
+
*/
|
|
42
|
+
put: function (changeNumber, segmentsData) {
|
|
43
|
+
if (changeNumber <= currentChangeNumber || changeNumber <= maxChangeNumber)
|
|
44
|
+
return;
|
|
45
|
+
maxChangeNumber = changeNumber;
|
|
46
|
+
handleNewEvent = true;
|
|
47
|
+
_segmentsData = segmentsData;
|
|
48
|
+
if (backoff.timeoutID || !isHandlingEvent)
|
|
49
|
+
__handleMySegmentsUpdateCall();
|
|
50
|
+
backoff.reset();
|
|
51
|
+
},
|
|
52
|
+
stop: function () {
|
|
53
|
+
isHandlingEvent = false;
|
|
54
|
+
backoff.reset();
|
|
55
|
+
}
|
|
38
56
|
};
|
|
39
|
-
|
|
40
|
-
* Invoked by NotificationProcessor on MY_SEGMENTS_UPDATE event
|
|
41
|
-
*
|
|
42
|
-
* @param {number} changeNumber change number of the MY_SEGMENTS_UPDATE notification
|
|
43
|
-
* @param {SegmentsData | undefined} segmentsData might be undefined
|
|
44
|
-
*/
|
|
45
|
-
MySegmentsUpdateWorker.prototype.put = function (changeNumber, segmentsData) {
|
|
46
|
-
if (changeNumber <= this.currentChangeNumber || changeNumber <= this.maxChangeNumber)
|
|
47
|
-
return;
|
|
48
|
-
this.maxChangeNumber = changeNumber;
|
|
49
|
-
this.handleNewEvent = true;
|
|
50
|
-
this.backoff.reset();
|
|
51
|
-
this.segmentsData = segmentsData;
|
|
52
|
-
if (this.mySegmentsSyncTask.isExecuting())
|
|
53
|
-
return;
|
|
54
|
-
this.__handleMySegmentsUpdateCall();
|
|
55
|
-
};
|
|
56
|
-
return MySegmentsUpdateWorker;
|
|
57
|
-
}());
|
|
58
|
-
export { MySegmentsUpdateWorker };
|
|
57
|
+
}
|