@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.
Files changed (67) hide show
  1. package/CHANGES.txt +1 -2
  2. package/cjs/integrations/ga/GaToSplit.js +11 -12
  3. package/cjs/services/splitApi.js +4 -4
  4. package/cjs/sync/polling/fetchers/segmentChangesFetcher.js +5 -5
  5. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +2 -2
  6. package/cjs/sync/polling/updaters/segmentChangesUpdater.js +34 -34
  7. package/cjs/sync/polling/updaters/splitChangesUpdater.js +4 -3
  8. package/cjs/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +46 -46
  9. package/cjs/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +82 -64
  10. package/cjs/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +74 -58
  11. package/cjs/sync/streaming/UpdateWorkers/constants.js +6 -0
  12. package/cjs/sync/streaming/pushManager.js +6 -7
  13. package/cjs/sync/syncTask.js +13 -16
  14. package/cjs/utils/Backoff.js +3 -2
  15. package/esm/integrations/ga/GaToSplit.js +11 -12
  16. package/esm/services/splitApi.js +4 -4
  17. package/esm/sync/polling/fetchers/segmentChangesFetcher.js +5 -5
  18. package/esm/sync/polling/fetchers/splitChangesFetcher.js +2 -2
  19. package/esm/sync/polling/updaters/segmentChangesUpdater.js +34 -34
  20. package/esm/sync/polling/updaters/splitChangesUpdater.js +4 -3
  21. package/esm/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +46 -47
  22. package/esm/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +82 -65
  23. package/esm/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +74 -59
  24. package/esm/sync/streaming/UpdateWorkers/constants.js +3 -0
  25. package/esm/sync/streaming/pushManager.js +6 -7
  26. package/esm/sync/syncTask.js +13 -16
  27. package/esm/utils/Backoff.js +3 -2
  28. package/package.json +1 -1
  29. package/src/integrations/ga/GaToSplit.ts +8 -14
  30. package/src/integrations/ga/types.ts +2 -13
  31. package/src/services/splitApi.ts +4 -4
  32. package/src/services/types.ts +2 -2
  33. package/src/sync/polling/fetchers/segmentChangesFetcher.ts +5 -4
  34. package/src/sync/polling/fetchers/splitChangesFetcher.ts +2 -1
  35. package/src/sync/polling/fetchers/types.ts +2 -0
  36. package/src/sync/polling/pollingManagerCS.ts +5 -5
  37. package/src/sync/polling/syncTasks/mySegmentsSyncTask.ts +2 -2
  38. package/src/sync/polling/types.ts +14 -6
  39. package/src/sync/polling/updaters/mySegmentsUpdater.ts +4 -4
  40. package/src/sync/polling/updaters/segmentChangesUpdater.ts +34 -32
  41. package/src/sync/polling/updaters/splitChangesUpdater.ts +5 -4
  42. package/src/sync/streaming/SSEHandler/types.ts +0 -7
  43. package/src/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.ts +45 -54
  44. package/src/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.ts +78 -63
  45. package/src/sync/streaming/UpdateWorkers/SplitsUpdateWorker.ts +73 -61
  46. package/src/sync/streaming/UpdateWorkers/constants.ts +3 -0
  47. package/src/sync/streaming/UpdateWorkers/types.ts +2 -4
  48. package/src/sync/streaming/pushManager.ts +12 -12
  49. package/src/sync/streaming/types.ts +2 -2
  50. package/src/sync/syncTask.ts +16 -18
  51. package/src/utils/Backoff.ts +7 -2
  52. package/types/integrations/ga/types.d.ts +2 -13
  53. package/types/services/types.d.ts +2 -2
  54. package/types/sync/polling/fetchers/types.d.ts +2 -2
  55. package/types/sync/polling/syncTasks/mySegmentsSyncTask.d.ts +2 -2
  56. package/types/sync/polling/types.d.ts +11 -6
  57. package/types/sync/polling/updaters/segmentChangesUpdater.d.ts +1 -1
  58. package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -1
  59. package/types/sync/streaming/SSEHandler/types.d.ts +0 -4
  60. package/types/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.d.ts +3 -24
  61. package/types/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.d.ts +3 -23
  62. package/types/sync/streaming/UpdateWorkers/SplitsUpdateWorker.d.ts +6 -33
  63. package/types/sync/streaming/UpdateWorkers/types.d.ts +1 -2
  64. package/types/sync/streaming/types.d.ts +2 -2
  65. package/types/sync/syncTask.d.ts +2 -3
  66. package/types/utils/Backoff.d.ts +2 -0
  67. package/src/integrations/ga/autoRequire.js +0 -33
@@ -1,71 +1,88 @@
1
1
  import { Backoff } from '../../../utils/Backoff';
2
+ import { FETCH_BACKOFF_BASE, FETCH_BACKOFF_MAX_RETRIES, FETCH_BACKOFF_MAX_WAIT } from './constants';
2
3
  /**
3
- * SegmentUpdateWorker class
4
+ * SegmentsUpdateWorker factory
4
5
  */
5
- var SegmentsUpdateWorker = /** @class */ (function () {
6
- /**
7
- * @param {Object} segmentsCache segments data cache
8
- * @param {Object} segmentsSyncTask task for syncing segments data
9
- */
10
- function SegmentsUpdateWorker(segmentsSyncTask, segmentsCache) {
11
- this.segmentsCache = segmentsCache;
12
- this.segmentsSyncTask = segmentsSyncTask;
13
- this.maxChangeNumbers = {};
14
- this.handleNewEvent = false;
15
- this.put = this.put.bind(this);
16
- this.__handleSegmentUpdateCall = this.__handleSegmentUpdateCall.bind(this);
17
- this.backoff = new Backoff(this.__handleSegmentUpdateCall);
6
+ export function SegmentsUpdateWorker(log, segmentsSyncTask, segmentsCache) {
7
+ // Handles retries with CDN bypass per segment name
8
+ function SegmentUpdateWorker(segment) {
9
+ var maxChangeNumber = 0;
10
+ var handleNewEvent = false;
11
+ var isHandlingEvent;
12
+ var cdnBypass;
13
+ var backoff = new Backoff(__handleSegmentUpdateCall, FETCH_BACKOFF_BASE, FETCH_BACKOFF_MAX_WAIT);
14
+ function __handleSegmentUpdateCall() {
15
+ isHandlingEvent = true;
16
+ if (maxChangeNumber > segmentsCache.getChangeNumber(segment)) {
17
+ handleNewEvent = false;
18
+ // fetch segments revalidating data if cached
19
+ segmentsSyncTask.execute(false, segment, true, cdnBypass ? maxChangeNumber : undefined).then(function () {
20
+ if (!isHandlingEvent)
21
+ return; // halt if `stop` has been called
22
+ if (handleNewEvent) {
23
+ __handleSegmentUpdateCall();
24
+ }
25
+ else {
26
+ var attempts = backoff.attempts + 1;
27
+ if (maxChangeNumber <= segmentsCache.getChangeNumber(segment)) {
28
+ log.debug("Refresh completed" + (cdnBypass ? ' bypassing the CDN' : '') + " in " + attempts + " attempts.");
29
+ isHandlingEvent = false;
30
+ return;
31
+ }
32
+ if (attempts < FETCH_BACKOFF_MAX_RETRIES) {
33
+ backoff.scheduleCall();
34
+ return;
35
+ }
36
+ if (cdnBypass) {
37
+ log.debug("No changes fetched after " + attempts + " attempts with CDN bypassed.");
38
+ isHandlingEvent = false;
39
+ }
40
+ else {
41
+ backoff.reset();
42
+ cdnBypass = true;
43
+ __handleSegmentUpdateCall();
44
+ }
45
+ }
46
+ });
47
+ }
48
+ else {
49
+ isHandlingEvent = false;
50
+ }
51
+ }
52
+ return {
53
+ put: function (changeNumber) {
54
+ var currentChangeNumber = segmentsCache.getChangeNumber(segment);
55
+ if (changeNumber <= currentChangeNumber || changeNumber <= maxChangeNumber)
56
+ return;
57
+ maxChangeNumber = changeNumber;
58
+ handleNewEvent = true;
59
+ cdnBypass = false;
60
+ if (backoff.timeoutID || !isHandlingEvent)
61
+ __handleSegmentUpdateCall();
62
+ backoff.reset();
63
+ },
64
+ stop: function () {
65
+ isHandlingEvent = false;
66
+ backoff.reset();
67
+ }
68
+ };
18
69
  }
19
- // Private method
20
- // Precondition: this.segmentsSyncTask.isSynchronizingSegments === false
21
- // Approach similar to MySegmentsUpdateWorker due to differences on Segments notifications and endpoint changeNumbers
22
- SegmentsUpdateWorker.prototype.__handleSegmentUpdateCall = function () {
23
- var _this = this;
24
- var segmentsToFetch = Object.keys(this.maxChangeNumbers).filter(function (segmentName) {
25
- return _this.maxChangeNumbers[segmentName] > _this.segmentsCache.getChangeNumber(segmentName);
26
- });
27
- if (segmentsToFetch.length > 0) {
28
- this.handleNewEvent = false;
29
- var currentMaxChangeNumbers_1 = segmentsToFetch.map(function (segmentToFetch) { return _this.maxChangeNumbers[segmentToFetch]; });
30
- // fetch segments revalidating data if cached
31
- this.segmentsSyncTask.execute(segmentsToFetch, true).then(function (result) {
32
- // Unlike `SplitUpdateWorker` where changeNumber is consistent between notification and endpoint, if there is no error,
33
- // we must clean the `maxChangeNumbers` of those segments that didn't receive a new update notification during the fetch.
34
- if (result !== false) {
35
- segmentsToFetch.forEach(function (fetchedSegment, index) {
36
- if (_this.maxChangeNumbers[fetchedSegment] === currentMaxChangeNumbers_1[index])
37
- _this.maxChangeNumbers[fetchedSegment] = -1;
38
- });
39
- }
40
- else {
41
- // recursive invocation with backoff if there was some error
42
- _this.backoff.scheduleCall();
43
- }
44
- // immediate recursive invocation if a new notification was queued during fetch
45
- if (_this.handleNewEvent) {
46
- _this.__handleSegmentUpdateCall();
47
- }
48
- });
70
+ var segments = {};
71
+ return {
72
+ /**
73
+ * Invoked by NotificationProcessor on SEGMENT_UPDATE event
74
+ *
75
+ * @param {number} changeNumber change number of the SEGMENT_UPDATE notification
76
+ * @param {string} segmentName segment name of the SEGMENT_UPDATE notification
77
+ */
78
+ put: function (_a) {
79
+ var changeNumber = _a.changeNumber, segmentName = _a.segmentName;
80
+ if (!segments[segmentName])
81
+ segments[segmentName] = SegmentUpdateWorker(segmentName);
82
+ segments[segmentName].put(changeNumber);
83
+ },
84
+ stop: function () {
85
+ Object.keys(segments).forEach(function (segmentName) { return segments[segmentName].stop(); });
49
86
  }
50
87
  };
51
- /**
52
- * Invoked by NotificationProcessor on SEGMENT_UPDATE event
53
- *
54
- * @param {number} changeNumber change number of the SEGMENT_UPDATE notification
55
- * @param {string} segmentName segment name of the SEGMENT_UPDATE notification
56
- */
57
- SegmentsUpdateWorker.prototype.put = function (_a) {
58
- var changeNumber = _a.changeNumber, segmentName = _a.segmentName;
59
- var currentChangeNumber = this.segmentsCache.getChangeNumber(segmentName);
60
- if (changeNumber <= currentChangeNumber || changeNumber <= this.maxChangeNumbers[segmentName])
61
- return;
62
- this.maxChangeNumbers[segmentName] = changeNumber;
63
- this.handleNewEvent = true;
64
- this.backoff.reset();
65
- if (this.segmentsSyncTask.isExecuting())
66
- return;
67
- this.__handleSegmentUpdateCall();
68
- };
69
- return SegmentsUpdateWorker;
70
- }());
71
- export { SegmentsUpdateWorker };
88
+ }
@@ -1,79 +1,94 @@
1
1
  import { SDK_SPLITS_ARRIVED } from '../../../readiness/constants';
2
2
  import { Backoff } from '../../../utils/Backoff';
3
+ import { FETCH_BACKOFF_BASE, FETCH_BACKOFF_MAX_WAIT, FETCH_BACKOFF_MAX_RETRIES } from './constants';
3
4
  /**
4
- * SplitsUpdateWorker class
5
+ * SplitsUpdateWorker factory
5
6
  */
6
- var SplitsUpdateWorker = /** @class */ (function () {
7
- /**
8
- * @param {Object} splitsCache splits data cache
9
- * @param {Object} splitsSyncTask task for syncing splits data
10
- * @param {Object} splitsEventEmitter emitter for splits data events
11
- */
12
- function SplitsUpdateWorker(splitsCache, splitsSyncTask, splitsEventEmitter, segmentsSyncTask) {
13
- this.splitsCache = splitsCache;
14
- this.splitsSyncTask = splitsSyncTask;
15
- this.splitsEventEmitter = splitsEventEmitter;
16
- this.segmentsSyncTask = segmentsSyncTask;
17
- this.maxChangeNumber = 0;
18
- this.handleNewEvent = false;
19
- this.put = this.put.bind(this);
20
- this.killSplit = this.killSplit.bind(this);
21
- this.__handleSplitUpdateCall = this.__handleSplitUpdateCall.bind(this);
22
- this.backoff = new Backoff(this.__handleSplitUpdateCall);
23
- }
24
- // Private method
25
- // Preconditions: this.splitsSyncTask.isSynchronizingSplits === false
26
- SplitsUpdateWorker.prototype.__handleSplitUpdateCall = function () {
27
- var _this = this;
28
- if (this.maxChangeNumber > this.splitsCache.getChangeNumber()) {
29
- this.handleNewEvent = false;
7
+ export function SplitsUpdateWorker(log, splitsCache, splitsSyncTask, splitsEventEmitter, segmentsSyncTask) {
8
+ var maxChangeNumber = 0;
9
+ var handleNewEvent = false;
10
+ var isHandlingEvent;
11
+ var cdnBypass;
12
+ var backoff = new Backoff(__handleSplitUpdateCall, FETCH_BACKOFF_BASE, FETCH_BACKOFF_MAX_WAIT);
13
+ function __handleSplitUpdateCall() {
14
+ isHandlingEvent = true;
15
+ if (maxChangeNumber > splitsCache.getChangeNumber()) {
16
+ handleNewEvent = false;
30
17
  // fetch splits revalidating data if cached
31
- this.splitsSyncTask.execute(true).then(function () {
32
- if (_this.handleNewEvent) {
33
- _this.__handleSplitUpdateCall();
18
+ splitsSyncTask.execute(true, cdnBypass ? maxChangeNumber : undefined).then(function () {
19
+ if (!isHandlingEvent)
20
+ return; // halt if `stop` has been called
21
+ if (handleNewEvent) {
22
+ __handleSplitUpdateCall();
34
23
  }
35
24
  else {
36
25
  // fetch new registered segments for server-side API. Not retrying on error
37
- if (_this.segmentsSyncTask)
38
- _this.segmentsSyncTask.execute(undefined, false, true);
39
- _this.backoff.scheduleCall();
26
+ if (segmentsSyncTask)
27
+ segmentsSyncTask.execute(true);
28
+ var attempts = backoff.attempts + 1;
29
+ if (maxChangeNumber <= splitsCache.getChangeNumber()) {
30
+ log.debug("Refresh completed" + (cdnBypass ? ' bypassing the CDN' : '') + " in " + attempts + " attempts.");
31
+ isHandlingEvent = false;
32
+ return;
33
+ }
34
+ if (attempts < FETCH_BACKOFF_MAX_RETRIES) {
35
+ backoff.scheduleCall();
36
+ return;
37
+ }
38
+ if (cdnBypass) {
39
+ log.debug("No changes fetched after " + attempts + " attempts with CDN bypassed.");
40
+ isHandlingEvent = false;
41
+ }
42
+ else {
43
+ backoff.reset();
44
+ cdnBypass = true;
45
+ __handleSplitUpdateCall();
46
+ }
40
47
  }
41
48
  });
42
49
  }
43
- };
50
+ else {
51
+ isHandlingEvent = false;
52
+ }
53
+ }
44
54
  /**
45
55
  * Invoked by NotificationProcessor on SPLIT_UPDATE event
46
56
  *
47
57
  * @param {number} changeNumber change number of the SPLIT_UPDATE notification
48
58
  */
49
- SplitsUpdateWorker.prototype.put = function (_a) {
59
+ function put(_a) {
50
60
  var changeNumber = _a.changeNumber;
51
- var currentChangeNumber = this.splitsCache.getChangeNumber();
52
- if (changeNumber <= currentChangeNumber || changeNumber <= this.maxChangeNumber)
53
- return;
54
- this.maxChangeNumber = changeNumber;
55
- this.handleNewEvent = true;
56
- this.backoff.reset();
57
- if (this.splitsSyncTask.isExecuting())
61
+ var currentChangeNumber = splitsCache.getChangeNumber();
62
+ if (changeNumber <= currentChangeNumber || changeNumber <= maxChangeNumber)
58
63
  return;
59
- this.__handleSplitUpdateCall();
60
- };
61
- /**
62
- * Invoked by NotificationProcessor on SPLIT_KILL event
63
- *
64
- * @param {number} changeNumber change number of the SPLIT_UPDATE notification
65
- * @param {string} splitName name of split to kill
66
- * @param {string} defaultTreatment default treatment value
67
- */
68
- SplitsUpdateWorker.prototype.killSplit = function (_a) {
69
- var changeNumber = _a.changeNumber, splitName = _a.splitName, defaultTreatment = _a.defaultTreatment;
70
- if (this.splitsCache.killLocally(splitName, defaultTreatment, changeNumber)) {
71
- // trigger an SDK_UPDATE if Split was killed locally
72
- this.splitsEventEmitter.emit(SDK_SPLITS_ARRIVED, true);
64
+ maxChangeNumber = changeNumber;
65
+ handleNewEvent = true;
66
+ cdnBypass = false;
67
+ if (backoff.timeoutID || !isHandlingEvent)
68
+ __handleSplitUpdateCall();
69
+ backoff.reset();
70
+ }
71
+ return {
72
+ put: put,
73
+ /**
74
+ * Invoked by NotificationProcessor on SPLIT_KILL event
75
+ *
76
+ * @param {number} changeNumber change number of the SPLIT_UPDATE notification
77
+ * @param {string} splitName name of split to kill
78
+ * @param {string} defaultTreatment default treatment value
79
+ */
80
+ killSplit: function (_a) {
81
+ var changeNumber = _a.changeNumber, splitName = _a.splitName, defaultTreatment = _a.defaultTreatment;
82
+ if (splitsCache.killLocally(splitName, defaultTreatment, changeNumber)) {
83
+ // trigger an SDK_UPDATE if Split was killed locally
84
+ splitsEventEmitter.emit(SDK_SPLITS_ARRIVED, true);
85
+ }
86
+ // queues the SplitChanges fetch (only if changeNumber is newer)
87
+ put({ changeNumber: changeNumber });
88
+ },
89
+ stop: function () {
90
+ isHandlingEvent = false;
91
+ backoff.reset();
73
92
  }
74
- // queues the SplitChanges fetch (only if changeNumber is newer)
75
- this.put({ changeNumber: changeNumber });
76
93
  };
77
- return SplitsUpdateWorker;
78
- }());
79
- export { SplitsUpdateWorker };
94
+ }
@@ -0,0 +1,3 @@
1
+ export var FETCH_BACKOFF_BASE = 10000; // backoff base starting at 10 seconds
2
+ export var FETCH_BACKOFF_MAX_WAIT = 60000; // don't wait for more than 1 minute
3
+ export var FETCH_BACKOFF_MAX_RETRIES = 10; // max retries
@@ -42,9 +42,9 @@ export function pushManagerFactory(params, pollingManager) {
42
42
  sseClient.setEventHandler(sseHandler);
43
43
  // init workers
44
44
  // MySegmentsUpdateWorker (client-side) are initiated in `add` method
45
- var segmentsUpdateWorker = userKey ? undefined : new SegmentsUpdateWorker(pollingManager.segmentsSyncTask, storage.segments);
45
+ var segmentsUpdateWorker = userKey ? undefined : SegmentsUpdateWorker(log, pollingManager.segmentsSyncTask, storage.segments);
46
46
  // For server-side we pass the segmentsSyncTask, used by SplitsUpdateWorker to fetch new segments
47
- var splitsUpdateWorker = new SplitsUpdateWorker(storage.splits, pollingManager.splitsSyncTask, readiness.splits, userKey ? undefined : pollingManager.segmentsSyncTask);
47
+ var splitsUpdateWorker = SplitsUpdateWorker(log, storage.splits, pollingManager.splitsSyncTask, readiness.splits, userKey ? undefined : pollingManager.segmentsSyncTask);
48
48
  // [Only for client-side] map of hashes to user keys, to dispatch MY_SEGMENTS_UPDATE events to the corresponding MySegmentsUpdateWorker
49
49
  var userKeyHashes = {};
50
50
  // [Only for client-side] map of user keys to their corresponding hash64 and MySegmentsUpdateWorkers.
@@ -137,21 +137,20 @@ export function pushManagerFactory(params, pollingManager) {
137
137
  }
138
138
  // cancel scheduled fetch retries of Splits, Segments, and MySegments Update Workers
139
139
  function stopWorkers() {
140
- splitsUpdateWorker.backoff.reset();
140
+ splitsUpdateWorker.stop();
141
141
  if (userKey)
142
142
  forOwn(clients, function (_a) {
143
143
  var worker = _a.worker;
144
- return worker.backoff.reset();
144
+ return worker.stop();
145
145
  });
146
146
  else
147
- segmentsUpdateWorker.backoff.reset();
147
+ segmentsUpdateWorker.stop();
148
148
  }
149
149
  pushEmitter.on(PUSH_SUBSYSTEM_DOWN, stopWorkers);
150
150
  // Only required when streaming connects after a PUSH_RETRYABLE_ERROR.
151
151
  // Otherwise it is unnecessary (e.g, STREAMING_RESUMED).
152
152
  pushEmitter.on(PUSH_SUBSYSTEM_UP, function () {
153
153
  connectPushRetryBackoff.reset();
154
- stopWorkers();
155
154
  });
156
155
  /** Fallback to polling without retry due to: STREAMING_DISABLED control event, or 'pushEnabled: false', or non-recoverable SSE and Authentication errors */
157
156
  pushEmitter.on(PUSH_NONRETRYABLE_ERROR, function handleNonRetryableError() {
@@ -287,7 +286,7 @@ export function pushManagerFactory(params, pollingManager) {
287
286
  var hash = hashUserKey(userKey);
288
287
  if (!userKeyHashes[hash]) {
289
288
  userKeyHashes[hash] = userKey;
290
- clients[userKey] = { hash64: hash64(userKey), worker: new MySegmentsUpdateWorker(mySegmentsSyncTask) };
289
+ clients[userKey] = { hash64: hash64(userKey), worker: MySegmentsUpdateWorker(mySegmentsSyncTask) };
291
290
  connectForNewClient = true; // we must reconnect on start, to listen the channel for the new user key
292
291
  // Reconnects in case of a new client.
293
292
  // Run in next event-loop cycle to save authentication calls
@@ -1,8 +1,7 @@
1
1
  import { SYNC_TASK_EXECUTE, SYNC_TASK_START, SYNC_TASK_STOP } from '../logger/constants';
2
2
  /**
3
- * Creates a syncTask that handles the periodic execution of a given task ("start" and "stop" methods).
4
- * The task can be also executed by calling the "execute" method. Multiple execute calls are chained to run secuentially and avoid race conditions.
5
- * For example, submitters executed on SDK destroy or full queue, while periodic execution is pending.
3
+ * Creates an object that handles the periodic execution of a given task via "start" and "stop" methods.
4
+ * 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).
6
5
  *
7
6
  * @param log Logger instance.
8
7
  * @param task Task to execute that returns a promise that NEVER REJECTS. Otherwise, periodic execution can result in Unhandled Promise Rejections.
@@ -12,8 +11,10 @@ import { SYNC_TASK_EXECUTE, SYNC_TASK_START, SYNC_TASK_STOP } from '../logger/co
12
11
  */
13
12
  export function syncTaskFactory(log, task, period, taskName) {
14
13
  if (taskName === void 0) { taskName = 'task'; }
15
- // Task promise while it is pending. Undefined once the promise is resolved
16
- var pendingTask;
14
+ // Flag that indicates if the task is executing
15
+ var executing = 0;
16
+ // Promise chain to resolve tasks sequentially
17
+ var promiseChain;
17
18
  // flag that indicates if the task periodic execution has been started/stopped.
18
19
  var running = false;
19
20
  // Auxiliar counter used to avoid race condition when calling `start` & `stop` intermittently
@@ -27,19 +28,15 @@ export function syncTaskFactory(log, task, period, taskName) {
27
28
  for (var _i = 0; _i < arguments.length; _i++) {
28
29
  args[_i] = arguments[_i];
29
30
  }
30
- // If task is executing, chain the new execution
31
- if (pendingTask) {
32
- return pendingTask.then(function () {
33
- return execute.apply(void 0, args);
34
- });
35
- }
36
- // Execute task
31
+ executing++;
37
32
  log.debug(SYNC_TASK_EXECUTE, [taskName]);
38
- pendingTask = task.apply(void 0, args).then(function (result) {
39
- pendingTask = undefined;
33
+ // Update `promiseChain` with last promise, to run tasks serially
34
+ promiseChain = (promiseChain ? promiseChain.then(function () { return task.apply(void 0, args); }) : task.apply(void 0, args))
35
+ .then(function (result) {
36
+ executing--;
40
37
  return result;
41
38
  });
42
- return pendingTask;
39
+ return promiseChain;
43
40
  }
44
41
  function periodicExecute(currentRunningId) {
45
42
  return execute.apply(void 0, runningArgs).then(function (result) {
@@ -53,7 +50,7 @@ export function syncTaskFactory(log, task, period, taskName) {
53
50
  return {
54
51
  execute: execute,
55
52
  isExecuting: function () {
56
- return pendingTask !== undefined;
53
+ return executing > 0;
57
54
  },
58
55
  start: function () {
59
56
  var args = [];
@@ -7,8 +7,8 @@ var Backoff = /** @class */ (function () {
7
7
  * @param {number} maxMillis
8
8
  */
9
9
  function Backoff(cb, baseMillis, maxMillis) {
10
- this.baseMillis = baseMillis || Backoff.DEFAULT_BASE_MILLIS;
11
- this.maxMillis = maxMillis || Backoff.DEFAULT_MAX_MILLIS;
10
+ this.baseMillis = Backoff.__TEST__BASE_MILLIS || baseMillis || Backoff.DEFAULT_BASE_MILLIS;
11
+ this.maxMillis = Backoff.__TEST__MAX_MILLIS || maxMillis || Backoff.DEFAULT_MAX_MILLIS;
12
12
  this.attempts = 0;
13
13
  this.cb = cb;
14
14
  }
@@ -22,6 +22,7 @@ var Backoff = /** @class */ (function () {
22
22
  if (this.timeoutID)
23
23
  clearTimeout(this.timeoutID);
24
24
  this.timeoutID = setTimeout(function () {
25
+ _this.timeoutID = undefined;
25
26
  _this.cb();
26
27
  }, delayInMillis);
27
28
  this.attempts++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.5.1-rc.0",
3
+ "version": "1.5.1-rc.1",
4
4
  "description": "Split Javascript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -19,26 +19,20 @@ const logNameMapper = 'ga-to-split:mapper';
19
19
  /**
20
20
  * Provides a plugin to use with analytics.js, accounting for the possibility
21
21
  * that the global command queue has been renamed or not yet defined.
22
- * @param window Reference to global object.
23
- * @param pluginName The plugin name identifier.
24
- * @param pluginConstructor The plugin constructor function.
25
- * @param log Logger instance.
26
- * @param autoRequire If true, log error when auto-require script is not detected
22
+ * @param {string} pluginName The plugin name identifier.
23
+ * @param {Function} pluginConstructor The plugin constructor function.
27
24
  */
28
- function providePlugin(window: any, pluginName: string, pluginConstructor: Function, log: ILogger, autoRequire?: boolean) {
25
+ function providePlugin(pluginName: string, pluginConstructor: Function) {
29
26
  // get reference to global command queue. Init it if not defined yet.
27
+ // @ts-expect-error
30
28
  const gaAlias = window.GoogleAnalyticsObject || 'ga';
31
- window[gaAlias] = window[gaAlias] || function () {
32
- (window[gaAlias].q = window[gaAlias].q || []).push(arguments);
29
+ window[gaAlias] = window[gaAlias] || function (...args: any[]) { // @ts-expect-error
30
+ (window[gaAlias].q = window[gaAlias].q || []).push(args);
33
31
  };
34
32
 
35
33
  // provides the plugin for use with analytics.js.
34
+ // @ts-expect-error
36
35
  window[gaAlias]('provide', pluginName, pluginConstructor);
37
-
38
- if (autoRequire && (!window[gaAlias].q || window[gaAlias].q.push === [].push)) {
39
- // Expecting spy on ga.q push method but not found
40
- log.error(logPrefix + 'integration is configured to autorequire the splitTracker plugin, but the necessary script does not seem to have run.');
41
- }
42
36
  }
43
37
 
44
38
  // Default mapping: object used for building the default mapper from hits to Split events
@@ -290,5 +284,5 @@ export function GaToSplit(sdkOptions: GoogleAnalyticsToSplitOptions, params: IIn
290
284
  }
291
285
 
292
286
  // Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
293
- providePlugin(window, 'splitTracker', SplitTracker, log, sdkOptions.autoRequire);
287
+ providePlugin('splitTracker', SplitTracker);
294
288
  }
@@ -53,24 +53,13 @@ export interface GoogleAnalyticsToSplitOptions {
53
53
  * If not provided, events are sent using the key and traffic type provided at SDK config
54
54
  */
55
55
  identities?: Identity[],
56
- /**
57
- * Optional flag to log an error if the `auto-require` script is not detected.
58
- * The auto-require script automatically requires the `splitTracker` plugin for created trackers,
59
- * and should be placed right after your Google Analytics, Google Tag Manager or gtag.js script tag.
60
- *
61
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#set-up-with-gtm-and-gtag.js}
62
- *
63
- * @property {boolean} autoRequire
64
- * @default false
65
- */
66
- autoRequire?: boolean,
67
56
  }
68
57
 
69
58
  /**
70
59
  * Enable 'Google Analytics to Split' integration, to track Google Analytics hits as Split events.
71
60
  * Used by the browser variant of the isomorphic JS SDK.
72
61
  *
73
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#google-analytics-to-split}
62
+ * @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#google-analytics-to-split}
74
63
  */
75
64
  export interface IGoogleAnalyticsToSplitConfig extends GoogleAnalyticsToSplitOptions {
76
65
  type: 'GOOGLE_ANALYTICS_TO_SPLIT'
@@ -140,7 +129,7 @@ export interface SplitToGoogleAnalyticsOptions {
140
129
  * Enable 'Split to Google Analytics' integration, to track Split impressions and events as Google Analytics hits.
141
130
  * Used by the browser variant of the isomorphic JS SDK.
142
131
  *
143
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#split-to-google-analytics}
132
+ * @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#split-to-google-analytics}
144
133
  */
145
134
  export interface ISplitToGoogleAnalyticsConfig extends SplitToGoogleAnalyticsOptions {
146
135
  type: 'SPLIT_TO_GOOGLE_ANALYTICS'
@@ -50,13 +50,13 @@ export function splitApiFactory(
50
50
  return splitHttpClient(url, undefined, telemetryTracker.trackHttp(TOKEN));
51
51
  },
52
52
 
53
- fetchSplitChanges(since: number, noCache?: boolean) {
54
- const url = `${urls.sdk}/splitChanges?since=${since}${filterQueryString || ''}`;
53
+ fetchSplitChanges(since: number, noCache?: boolean, till?: number) {
54
+ const url = `${urls.sdk}/splitChanges?since=${since}${till ? '&till=' + till : ''}${filterQueryString || ''}`;
55
55
  return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SPLITS));
56
56
  },
57
57
 
58
- fetchSegmentChanges(since: number, segmentName: string, noCache?: boolean) {
59
- const url = `${urls.sdk}/segmentChanges/${segmentName}?since=${since}`;
58
+ fetchSegmentChanges(since: number, segmentName: string, noCache?: boolean, till?: number) {
59
+ const url = `${urls.sdk}/segmentChanges/${segmentName}?since=${since}${till ? '&till=' + till : ''}`;
60
60
  return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SEGMENT));
61
61
  },
62
62
 
@@ -35,9 +35,9 @@ export type ISplitHttpClient = (url: string, options?: IRequestOptions, latencyT
35
35
 
36
36
  export type IFetchAuth = (userKeys?: string[]) => Promise<IResponse>
37
37
 
38
- export type IFetchSplitChanges = (since: number, noCache?: boolean) => Promise<IResponse>
38
+ export type IFetchSplitChanges = (since: number, noCache?: boolean, till?: number) => Promise<IResponse>
39
39
 
40
- export type IFetchSegmentChanges = (since: number, segmentName: string, noCache?: boolean) => Promise<IResponse>
40
+ export type IFetchSegmentChanges = (since: number, segmentName: string, noCache?: boolean, till?: number) => Promise<IResponse>
41
41
 
42
42
  export type IFetchMySegments = (userMatchingKey: string, noCache?: boolean) => Promise<IResponse>
43
43
 
@@ -2,15 +2,15 @@ import { IFetchSegmentChanges } from '../../../services/types';
2
2
  import { ISegmentChangesResponse } from '../../../dtos/types';
3
3
  import { ISegmentChangesFetcher } from './types';
4
4
 
5
- function greedyFetch(fetchSegmentChanges: IFetchSegmentChanges, since: number, segmentName: string, noCache?: boolean): Promise<ISegmentChangesResponse[]> {
6
- return fetchSegmentChanges(since, segmentName, noCache)
5
+ function greedyFetch(fetchSegmentChanges: IFetchSegmentChanges, since: number, segmentName: string, noCache?: boolean, targetTill?: number): Promise<ISegmentChangesResponse[]> {
6
+ return fetchSegmentChanges(since, segmentName, noCache, targetTill)
7
7
  .then(resp => resp.json())
8
8
  .then((json: ISegmentChangesResponse) => {
9
9
  let { since, till } = json;
10
10
  if (since === till) {
11
11
  return [json];
12
12
  } else {
13
- return Promise.all([json, greedyFetch(fetchSegmentChanges, till, segmentName, noCache)]).then(flatMe => {
13
+ return Promise.all([json, greedyFetch(fetchSegmentChanges, till, segmentName, noCache, targetTill)]).then(flatMe => {
14
14
  return [flatMe[0], ...flatMe[1]];
15
15
  });
16
16
  }
@@ -27,11 +27,12 @@ export function segmentChangesFetcherFactory(fetchSegmentChanges: IFetchSegmentC
27
27
  since: number,
28
28
  segmentName: string,
29
29
  noCache?: boolean,
30
+ till?: number,
30
31
  // Optional decorator for `fetchMySegments` promise, such as timeout or time tracker
31
32
  decorator?: (promise: Promise<ISegmentChangesResponse[]>) => Promise<ISegmentChangesResponse[]>
32
33
  ): Promise<ISegmentChangesResponse[]> {
33
34
 
34
- let segmentsPromise = greedyFetch(fetchSegmentChanges, since, segmentName, noCache);
35
+ let segmentsPromise = greedyFetch(fetchSegmentChanges, since, segmentName, noCache, till);
35
36
  if (decorator) segmentsPromise = decorator(segmentsPromise);
36
37
 
37
38
  return segmentsPromise;
@@ -10,11 +10,12 @@ export function splitChangesFetcherFactory(fetchSplitChanges: IFetchSplitChanges
10
10
  return function splitChangesFetcher(
11
11
  since: number,
12
12
  noCache?: boolean,
13
+ till?: number,
13
14
  // Optional decorator for `fetchSplitChanges` promise, such as timeout or time tracker
14
15
  decorator?: (promise: Promise<IResponse>) => Promise<IResponse>
15
16
  ) {
16
17
 
17
- let splitsPromise = fetchSplitChanges(since, noCache);
18
+ let splitsPromise = fetchSplitChanges(since, noCache, till);
18
19
  if (decorator) splitsPromise = decorator(splitsPromise);
19
20
 
20
21
  return splitsPromise.then(resp => resp.json());
@@ -4,6 +4,7 @@ import { IResponse } from '../../../services/types';
4
4
  export type ISplitChangesFetcher = (
5
5
  since: number,
6
6
  noCache?: boolean,
7
+ till?: number,
7
8
  decorator?: (promise: Promise<IResponse>) => Promise<IResponse>
8
9
  ) => Promise<ISplitChangesResponse>
9
10
 
@@ -11,6 +12,7 @@ export type ISegmentChangesFetcher = (
11
12
  since: number,
12
13
  segmentName: string,
13
14
  noCache?: boolean,
15
+ till?: number,
14
16
  decorator?: (promise: Promise<ISegmentChangesResponse[]>) => Promise<ISegmentChangesResponse[]>
15
17
  ) => Promise<ISegmentChangesResponse[]>
16
18