@splitsoftware/splitio-commons 2.4.2-rc.2 → 2.5.0-rc.0
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 +2 -10
- package/cjs/storages/AbstractMySegmentsCacheSync.js +23 -31
- package/cjs/storages/AbstractSplitsCacheSync.js +2 -3
- package/cjs/storages/dataLoader.js +102 -43
- package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +16 -16
- package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +23 -20
- package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +37 -33
- package/cjs/storages/inLocalStorage/index.js +13 -28
- package/cjs/storages/inLocalStorage/validateCache.js +23 -25
- package/cjs/storages/inMemory/InMemoryStorageCS.js +32 -14
- package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +4 -0
- package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +3 -2
- package/cjs/sync/syncManagerOnline.js +24 -28
- package/cjs/utils/env/isLocalStorageAvailable.js +5 -28
- package/cjs/utils/settingsValidation/storage/storageCS.js +1 -1
- package/esm/storages/AbstractMySegmentsCacheSync.js +23 -31
- package/esm/storages/AbstractSplitsCacheSync.js +2 -3
- package/esm/storages/dataLoader.js +99 -41
- package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +16 -16
- package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +23 -20
- package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +37 -33
- package/esm/storages/inLocalStorage/index.js +14 -29
- package/esm/storages/inLocalStorage/validateCache.js +23 -25
- package/esm/storages/inMemory/InMemoryStorageCS.js +32 -14
- package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +4 -0
- package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +3 -2
- package/esm/sync/syncManagerOnline.js +24 -28
- package/esm/utils/env/isLocalStorageAvailable.js +3 -24
- package/esm/utils/settingsValidation/storage/storageCS.js +1 -1
- package/package.json +1 -1
- package/src/sdkFactory/index.ts +3 -2
- package/src/storages/AbstractMySegmentsCacheSync.ts +20 -26
- package/src/storages/AbstractSplitsCacheSync.ts +2 -3
- package/src/storages/dataLoader.ts +107 -49
- package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +17 -18
- package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +24 -22
- package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +37 -34
- package/src/storages/inLocalStorage/index.ts +16 -33
- package/src/storages/inLocalStorage/validateCache.ts +23 -26
- package/src/storages/inMemory/InMemoryStorageCS.ts +37 -14
- package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +4 -0
- package/src/storages/types.ts +6 -20
- package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +2 -1
- package/src/sync/syncManagerOnline.ts +22 -27
- package/src/types.ts +0 -35
- package/src/utils/env/isLocalStorageAvailable.ts +3 -24
- package/src/utils/settingsValidation/storage/storageCS.ts +1 -1
- package/types/splitio.d.ts +46 -27
- package/cjs/storages/inLocalStorage/storageAdapter.js +0 -48
- package/esm/storages/inLocalStorage/storageAdapter.js +0 -44
- package/src/storages/inLocalStorage/storageAdapter.ts +0 -50
package/CHANGES.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
2.5.0 (August XX, 2025)
|
|
2
|
-
- Added `
|
|
2
|
+
- Added `factory.getCache()` method for standalone server-side SDKs, which returns the rollout plan snapshot from the storage.
|
|
3
|
+
- Added `preloadedData` configuration option for standalone client-side SDKs, which allows preloading the SDK storage with a snapshot of the rollout plan.
|
|
3
4
|
|
|
4
5
|
2.4.1 (June 3, 2025)
|
|
5
6
|
- Bugfix - Improved the Proxy fallback to flag spec version 1.2 to handle cases where the Proxy does not return an end-of-stream marker in 400 status code responses.
|
|
@@ -50,15 +51,6 @@
|
|
|
50
51
|
- Removed internal ponyfills for `Map` and `Set` global objects, dropping support for IE and other outdated browsers. The SDK now requires the runtime environment to support these features natively or to provide a polyfill.
|
|
51
52
|
- Removed the `sync.localhostMode` configuration option to plug the LocalhostMode module.
|
|
52
53
|
|
|
53
|
-
1.17.1 (July 25, 2025)
|
|
54
|
-
- Updated the Redis storage to avoid lazy require of the `ioredis` dependency when the SDK is initialized.
|
|
55
|
-
- Updated some transitive dependencies for vulnerability fixes.
|
|
56
|
-
- Bugfix - Enhanced HTTP client module to implement timeouts for failing requests that might otherwise remain pending indefinitely on some Fetch API implementations, pausing the SDK synchronization process.
|
|
57
|
-
- Bugfix - Properly handle rejected promises when using targeting rules with segment matchers in consumer modes (e.g., Redis and Pluggable storages).
|
|
58
|
-
- Bugfix - Sanitize the `SplitSDKMachineName` header value to avoid exceptions on HTTP/S requests when it contains non ISO-8859-1 characters (Related to issue https://github.com/splitio/javascript-client/issues/847).
|
|
59
|
-
- Bugfix - Fixed an issue with the SDK_UPDATE event on server-side, where it was not being emitted if there was an empty segment and the SDK received a feature flag update notification.
|
|
60
|
-
- Bugfix - Fixed an issue with the server-side polling manager that caused dangling timers when the SDK was destroyed before it was ready.
|
|
61
|
-
|
|
62
54
|
1.17.0 (September 6, 2024)
|
|
63
55
|
- Added `sync.requestOptions.getHeaderOverrides` configuration option to enhance SDK HTTP request Headers for Authorization Frameworks.
|
|
64
56
|
- Added `isTimedout` and `lastUpdate` properties to IStatusInterface to keep track of the timestamp of the last SDK event, used on React and Redux SDKs.
|
|
@@ -23,45 +23,37 @@ var AbstractMySegmentsCacheSync = /** @class */ (function () {
|
|
|
23
23
|
*/
|
|
24
24
|
AbstractMySegmentsCacheSync.prototype.resetSegments = function (segmentsData) {
|
|
25
25
|
var _this = this;
|
|
26
|
+
this.setChangeNumber(segmentsData.cn);
|
|
26
27
|
var _a = segmentsData, added = _a.added, removed = _a.removed;
|
|
27
|
-
var isDiff = false;
|
|
28
28
|
if (added && removed) {
|
|
29
|
+
var isDiff_1 = false;
|
|
29
30
|
added.forEach(function (segment) {
|
|
30
|
-
|
|
31
|
+
isDiff_1 = _this.addSegment(segment) || isDiff_1;
|
|
31
32
|
});
|
|
32
33
|
removed.forEach(function (segment) {
|
|
33
|
-
|
|
34
|
+
isDiff_1 = _this.removeSegment(segment) || isDiff_1;
|
|
34
35
|
});
|
|
36
|
+
return isDiff_1;
|
|
35
37
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
// Slowest path => add and/or remove segments
|
|
53
|
-
for (var removeIndex = index; removeIndex < storedSegmentKeys.length; removeIndex++) {
|
|
54
|
-
this.removeSegment(storedSegmentKeys[removeIndex]);
|
|
55
|
-
}
|
|
56
|
-
for (var addIndex = index; addIndex < names.length; addIndex++) {
|
|
57
|
-
this.addSegment(names[addIndex]);
|
|
58
|
-
}
|
|
59
|
-
isDiff = true;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
38
|
+
var names = (segmentsData.k || []).map(function (s) { return s.n; }).sort();
|
|
39
|
+
var storedSegmentKeys = this.getRegisteredSegments().sort();
|
|
40
|
+
// Extreme fast => everything is empty
|
|
41
|
+
if (!names.length && !storedSegmentKeys.length)
|
|
42
|
+
return false;
|
|
43
|
+
var index = 0;
|
|
44
|
+
while (index < names.length && index < storedSegmentKeys.length && names[index] === storedSegmentKeys[index])
|
|
45
|
+
index++;
|
|
46
|
+
// Quick path => no changes
|
|
47
|
+
if (index === names.length && index === storedSegmentKeys.length)
|
|
48
|
+
return false;
|
|
49
|
+
// Slowest path => add and/or remove segments
|
|
50
|
+
for (var removeIndex = index; removeIndex < storedSegmentKeys.length; removeIndex++) {
|
|
51
|
+
this.removeSegment(storedSegmentKeys[removeIndex]);
|
|
62
52
|
}
|
|
63
|
-
|
|
64
|
-
|
|
53
|
+
for (var addIndex = index; addIndex < names.length; addIndex++) {
|
|
54
|
+
this.addSegment(names[addIndex]);
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
65
57
|
};
|
|
66
58
|
return AbstractMySegmentsCacheSync;
|
|
67
59
|
}());
|
|
@@ -12,10 +12,9 @@ var AbstractSplitsCacheSync = /** @class */ (function () {
|
|
|
12
12
|
}
|
|
13
13
|
AbstractSplitsCacheSync.prototype.update = function (toAdd, toRemove, changeNumber) {
|
|
14
14
|
var _this = this;
|
|
15
|
-
var updated = toAdd.map(function (addedFF) { return _this.addSplit(addedFF); }).some(function (result) { return result; });
|
|
16
|
-
updated = toRemove.map(function (removedFF) { return _this.removeSplit(removedFF.name); }).some(function (result) { return result; }) || updated;
|
|
17
15
|
this.setChangeNumber(changeNumber);
|
|
18
|
-
return
|
|
16
|
+
var updated = toAdd.map(function (addedFF) { return _this.addSplit(addedFF); }).some(function (result) { return result; });
|
|
17
|
+
return toRemove.map(function (removedFF) { return _this.removeSplit(removedFF.name); }).some(function (result) { return result; }) || updated;
|
|
19
18
|
};
|
|
20
19
|
AbstractSplitsCacheSync.prototype.getSplits = function (names) {
|
|
21
20
|
var _this = this;
|
|
@@ -1,50 +1,109 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
var
|
|
3
|
+
exports.getCache = exports.setCache = void 0;
|
|
4
|
+
var sets_1 = require("../utils/lang/sets");
|
|
5
|
+
var key_1 = require("../utils/key");
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* and extended with a `mySegmentsData` property.
|
|
11
|
-
* @returns function to preload the storage
|
|
7
|
+
* Sets the given synchronous storage with the provided preloaded data snapshot.
|
|
8
|
+
* If `matchingKey` is provided, the storage is handled as a client-side storage (segments and largeSegments are instances of MySegmentsCache).
|
|
9
|
+
* Otherwise, the storage is handled as a server-side storage (segments is an instance of SegmentsCache).
|
|
12
10
|
*/
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
var mySegmentsData = preloadedData.mySegmentsData && preloadedData.mySegmentsData[userId];
|
|
40
|
-
if (!mySegmentsData) {
|
|
41
|
-
// segmentsData in an object where the property is the segment name and the pertaining value is a stringified object that contains the `added` array of userIds
|
|
42
|
-
mySegmentsData = Object.keys(segmentsData).filter(function (segmentName) {
|
|
43
|
-
var userIds = JSON.parse(segmentsData[segmentName]).added;
|
|
44
|
-
return Array.isArray(userIds) && userIds.indexOf(userId) > -1;
|
|
45
|
-
});
|
|
11
|
+
function setCache(log, preloadedData, storage, matchingKey) {
|
|
12
|
+
// Do not load data if current preloadedData is empty
|
|
13
|
+
if (Object.keys(preloadedData).length === 0)
|
|
14
|
+
return;
|
|
15
|
+
var splits = storage.splits, rbSegments = storage.rbSegments, segments = storage.segments, largeSegments = storage.largeSegments;
|
|
16
|
+
log.debug("set cache" + (matchingKey ? " for key " + matchingKey : ''));
|
|
17
|
+
if (splits) {
|
|
18
|
+
splits.clear();
|
|
19
|
+
splits.update(preloadedData.flags || [], [], preloadedData.since || -1);
|
|
20
|
+
}
|
|
21
|
+
if (rbSegments) {
|
|
22
|
+
rbSegments.clear();
|
|
23
|
+
rbSegments.update(preloadedData.rbSegments || [], [], preloadedData.rbSince || -1);
|
|
24
|
+
}
|
|
25
|
+
var segmentsData = preloadedData.segments || {};
|
|
26
|
+
if (matchingKey) { // add memberships data (client-side)
|
|
27
|
+
var memberships = preloadedData.memberships && preloadedData.memberships[matchingKey];
|
|
28
|
+
if (!memberships && segmentsData) {
|
|
29
|
+
memberships = {
|
|
30
|
+
ms: {
|
|
31
|
+
k: Object.keys(segmentsData).filter(function (segmentName) {
|
|
32
|
+
var segmentKeys = segmentsData[segmentName];
|
|
33
|
+
return segmentKeys.indexOf(matchingKey) > -1;
|
|
34
|
+
}).map(function (segmentName) { return ({ n: segmentName }); })
|
|
35
|
+
}
|
|
36
|
+
};
|
|
46
37
|
}
|
|
47
|
-
|
|
38
|
+
if (memberships) {
|
|
39
|
+
if (memberships.ms)
|
|
40
|
+
segments.resetSegments(memberships.ms);
|
|
41
|
+
if (memberships.ls && largeSegments)
|
|
42
|
+
largeSegments.resetSegments(memberships.ls);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else { // add segments data (server-side)
|
|
46
|
+
Object.keys(segmentsData).forEach(function (segmentName) {
|
|
47
|
+
var segmentKeys = segmentsData[segmentName];
|
|
48
|
+
segments.update(segmentName, segmentKeys, [], -1);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.setCache = setCache;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the preloaded data snapshot from the given synchronous storage.
|
|
55
|
+
* If `keys` are provided, the memberships for those keys is returned, to protect segments data.
|
|
56
|
+
* Otherwise, the segments data is returned.
|
|
57
|
+
*/
|
|
58
|
+
function getCache(log, storage, keys) {
|
|
59
|
+
log.debug("get cache" + (keys ? " for keys " + keys : ''));
|
|
60
|
+
return {
|
|
61
|
+
since: storage.splits.getChangeNumber(),
|
|
62
|
+
flags: storage.splits.getAll(),
|
|
63
|
+
rbSince: storage.rbSegments.getChangeNumber(),
|
|
64
|
+
rbSegments: storage.rbSegments.getAll(),
|
|
65
|
+
segments: keys ?
|
|
66
|
+
undefined : // @ts-ignore accessing private prop
|
|
67
|
+
Object.keys(storage.segments.segmentCache).reduce(function (prev, cur) {
|
|
68
|
+
prev[cur] = (0, sets_1.setToArray)(storage.segments.segmentCache[cur]);
|
|
69
|
+
return prev;
|
|
70
|
+
}, {}),
|
|
71
|
+
memberships: keys ?
|
|
72
|
+
keys.reduce(function (prev, key) {
|
|
73
|
+
if (storage.shared) {
|
|
74
|
+
// Client-side segments
|
|
75
|
+
// @ts-ignore accessing private prop
|
|
76
|
+
var sharedStorage = storage.shared(key);
|
|
77
|
+
prev[(0, key_1.getMatching)(key)] = {
|
|
78
|
+
ms: {
|
|
79
|
+
// @ts-ignore accessing private prop
|
|
80
|
+
k: Object.keys(sharedStorage.segments.segmentCache).map(function (segmentName) { return ({ n: segmentName }); }),
|
|
81
|
+
},
|
|
82
|
+
ls: sharedStorage.largeSegments ? {
|
|
83
|
+
// @ts-ignore accessing private prop
|
|
84
|
+
k: Object.keys(sharedStorage.largeSegments.segmentCache).map(function (segmentName) { return ({ n: segmentName }); }),
|
|
85
|
+
} : undefined
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
prev[(0, key_1.getMatching)(key)] = {
|
|
90
|
+
ms: {
|
|
91
|
+
// Server-side segments
|
|
92
|
+
// @ts-ignore accessing private prop
|
|
93
|
+
k: Object.keys(storage.segments.segmentCache).reduce(function (prev, segmentName) {
|
|
94
|
+
return storage.segments.segmentCache[segmentName].has(key) ?
|
|
95
|
+
prev.concat({ n: segmentName }) :
|
|
96
|
+
prev;
|
|
97
|
+
}, [])
|
|
98
|
+
},
|
|
99
|
+
ls: {
|
|
100
|
+
k: []
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return prev;
|
|
105
|
+
}, {}) :
|
|
106
|
+
undefined
|
|
48
107
|
};
|
|
49
108
|
}
|
|
50
|
-
exports.
|
|
109
|
+
exports.getCache = getCache;
|
|
@@ -7,20 +7,19 @@ var AbstractMySegmentsCacheSync_1 = require("../AbstractMySegmentsCacheSync");
|
|
|
7
7
|
var constants_1 = require("./constants");
|
|
8
8
|
var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
9
9
|
(0, tslib_1.__extends)(MySegmentsCacheInLocal, _super);
|
|
10
|
-
function MySegmentsCacheInLocal(log, keys
|
|
10
|
+
function MySegmentsCacheInLocal(log, keys) {
|
|
11
11
|
var _this = _super.call(this) || this;
|
|
12
12
|
_this.log = log;
|
|
13
13
|
_this.keys = keys;
|
|
14
|
-
_this.storage = storage;
|
|
15
14
|
return _this;
|
|
16
15
|
// There is not need to flush segments cache like splits cache, since resetSegments receives the up-to-date list of active segments
|
|
17
16
|
}
|
|
18
17
|
MySegmentsCacheInLocal.prototype.addSegment = function (name) {
|
|
19
18
|
var segmentKey = this.keys.buildSegmentNameKey(name);
|
|
20
19
|
try {
|
|
21
|
-
if (
|
|
20
|
+
if (localStorage.getItem(segmentKey) === constants_1.DEFINED)
|
|
22
21
|
return false;
|
|
23
|
-
|
|
22
|
+
localStorage.setItem(segmentKey, constants_1.DEFINED);
|
|
24
23
|
return true;
|
|
25
24
|
}
|
|
26
25
|
catch (e) {
|
|
@@ -31,9 +30,9 @@ var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
|
31
30
|
MySegmentsCacheInLocal.prototype.removeSegment = function (name) {
|
|
32
31
|
var segmentKey = this.keys.buildSegmentNameKey(name);
|
|
33
32
|
try {
|
|
34
|
-
if (
|
|
33
|
+
if (localStorage.getItem(segmentKey) !== constants_1.DEFINED)
|
|
35
34
|
return false;
|
|
36
|
-
|
|
35
|
+
localStorage.removeItem(segmentKey);
|
|
37
36
|
return true;
|
|
38
37
|
}
|
|
39
38
|
catch (e) {
|
|
@@ -42,16 +41,17 @@ var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
|
42
41
|
}
|
|
43
42
|
};
|
|
44
43
|
MySegmentsCacheInLocal.prototype.isInSegment = function (name) {
|
|
45
|
-
return
|
|
44
|
+
return localStorage.getItem(this.keys.buildSegmentNameKey(name)) === constants_1.DEFINED;
|
|
46
45
|
};
|
|
47
46
|
MySegmentsCacheInLocal.prototype.getRegisteredSegments = function () {
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
var _this = this;
|
|
48
|
+
// Scan current values from localStorage
|
|
49
|
+
return Object.keys(localStorage).reduce(function (accum, key) {
|
|
50
|
+
var segmentName = _this.keys.extractSegmentName(key);
|
|
51
51
|
if (segmentName)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
accum.push(segmentName);
|
|
53
|
+
return accum;
|
|
54
|
+
}, []);
|
|
55
55
|
};
|
|
56
56
|
MySegmentsCacheInLocal.prototype.getKeysCount = function () {
|
|
57
57
|
return 1;
|
|
@@ -59,9 +59,9 @@ var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
|
59
59
|
MySegmentsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
|
|
60
60
|
try {
|
|
61
61
|
if (changeNumber)
|
|
62
|
-
|
|
62
|
+
localStorage.setItem(this.keys.buildTillKey(), changeNumber + '');
|
|
63
63
|
else
|
|
64
|
-
|
|
64
|
+
localStorage.removeItem(this.keys.buildTillKey());
|
|
65
65
|
}
|
|
66
66
|
catch (e) {
|
|
67
67
|
this.log.error(e);
|
|
@@ -69,7 +69,7 @@ var MySegmentsCacheInLocal = /** @class */ (function (_super) {
|
|
|
69
69
|
};
|
|
70
70
|
MySegmentsCacheInLocal.prototype.getChangeNumber = function () {
|
|
71
71
|
var n = -1;
|
|
72
|
-
var value =
|
|
72
|
+
var value = localStorage.getItem(this.keys.buildTillKey());
|
|
73
73
|
if (value !== null) {
|
|
74
74
|
value = parseInt(value, 10);
|
|
75
75
|
return (0, lang_1.isNaNNumber)(value) ? n : value;
|
|
@@ -6,27 +6,25 @@ var sets_1 = require("../../utils/lang/sets");
|
|
|
6
6
|
var AbstractSplitsCacheSync_1 = require("../AbstractSplitsCacheSync");
|
|
7
7
|
var constants_1 = require("./constants");
|
|
8
8
|
var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
9
|
-
function RBSegmentsCacheInLocal(settings, keys
|
|
9
|
+
function RBSegmentsCacheInLocal(settings, keys) {
|
|
10
10
|
this.keys = keys;
|
|
11
11
|
this.log = settings.log;
|
|
12
|
-
this.storage = storage;
|
|
13
12
|
}
|
|
14
13
|
RBSegmentsCacheInLocal.prototype.clear = function () {
|
|
15
14
|
var _this = this;
|
|
16
15
|
this.getNames().forEach(function (name) { return _this.remove(name); });
|
|
17
|
-
|
|
16
|
+
localStorage.removeItem(this.keys.buildRBSegmentsTillKey());
|
|
18
17
|
};
|
|
19
18
|
RBSegmentsCacheInLocal.prototype.update = function (toAdd, toRemove, changeNumber) {
|
|
20
19
|
var _this = this;
|
|
21
|
-
var updated = toAdd.map(function (toAdd) { return _this.add(toAdd); }).some(function (result) { return result; });
|
|
22
|
-
updated = toRemove.map(function (toRemove) { return _this.remove(toRemove.name); }).some(function (result) { return result; }) || updated;
|
|
23
20
|
this.setChangeNumber(changeNumber);
|
|
24
|
-
return
|
|
21
|
+
var updated = toAdd.map(function (toAdd) { return _this.add(toAdd); }).some(function (result) { return result; });
|
|
22
|
+
return toRemove.map(function (toRemove) { return _this.remove(toRemove.name); }).some(function (result) { return result; }) || updated;
|
|
25
23
|
};
|
|
26
24
|
RBSegmentsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
|
|
27
25
|
try {
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
localStorage.setItem(this.keys.buildRBSegmentsTillKey(), changeNumber + '');
|
|
27
|
+
localStorage.setItem(this.keys.buildLastUpdatedKey(), Date.now() + '');
|
|
30
28
|
}
|
|
31
29
|
catch (e) {
|
|
32
30
|
this.log.error(constants_1.LOG_PREFIX + e);
|
|
@@ -34,19 +32,20 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
34
32
|
};
|
|
35
33
|
RBSegmentsCacheInLocal.prototype.updateSegmentCount = function (diff) {
|
|
36
34
|
var segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
|
|
37
|
-
var count = (0, lang_1.toNumber)(
|
|
35
|
+
var count = (0, lang_1.toNumber)(localStorage.getItem(segmentsCountKey)) + diff;
|
|
36
|
+
// @ts-expect-error
|
|
38
37
|
if (count > 0)
|
|
39
|
-
|
|
38
|
+
localStorage.setItem(segmentsCountKey, count);
|
|
40
39
|
else
|
|
41
|
-
|
|
40
|
+
localStorage.removeItem(segmentsCountKey);
|
|
42
41
|
};
|
|
43
42
|
RBSegmentsCacheInLocal.prototype.add = function (rbSegment) {
|
|
44
43
|
try {
|
|
45
44
|
var name_1 = rbSegment.name;
|
|
46
45
|
var rbSegmentKey = this.keys.buildRBSegmentKey(name_1);
|
|
47
|
-
var
|
|
48
|
-
var previous =
|
|
49
|
-
|
|
46
|
+
var rbSegmentFromLocalStorage = localStorage.getItem(rbSegmentKey);
|
|
47
|
+
var previous = rbSegmentFromLocalStorage ? JSON.parse(rbSegmentFromLocalStorage) : null;
|
|
48
|
+
localStorage.setItem(rbSegmentKey, JSON.stringify(rbSegment));
|
|
50
49
|
var usesSegmentsDiff = 0;
|
|
51
50
|
if (previous && (0, AbstractSplitsCacheSync_1.usesSegments)(previous))
|
|
52
51
|
usesSegmentsDiff--;
|
|
@@ -66,7 +65,7 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
66
65
|
var rbSegment = this.get(name);
|
|
67
66
|
if (!rbSegment)
|
|
68
67
|
return false;
|
|
69
|
-
|
|
68
|
+
localStorage.removeItem(this.keys.buildRBSegmentKey(name));
|
|
70
69
|
if ((0, AbstractSplitsCacheSync_1.usesSegments)(rbSegment))
|
|
71
70
|
this.updateSegmentCount(-1);
|
|
72
71
|
return true;
|
|
@@ -77,11 +76,11 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
77
76
|
}
|
|
78
77
|
};
|
|
79
78
|
RBSegmentsCacheInLocal.prototype.getNames = function () {
|
|
80
|
-
var len =
|
|
79
|
+
var len = localStorage.length;
|
|
81
80
|
var accum = [];
|
|
82
81
|
var cur = 0;
|
|
83
82
|
while (cur < len) {
|
|
84
|
-
var key =
|
|
83
|
+
var key = localStorage.key(cur);
|
|
85
84
|
if (key != null && this.keys.isRBSegmentKey(key))
|
|
86
85
|
accum.push(this.keys.extractKey(key));
|
|
87
86
|
cur++;
|
|
@@ -89,9 +88,13 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
89
88
|
return accum;
|
|
90
89
|
};
|
|
91
90
|
RBSegmentsCacheInLocal.prototype.get = function (name) {
|
|
92
|
-
var item =
|
|
91
|
+
var item = localStorage.getItem(this.keys.buildRBSegmentKey(name));
|
|
93
92
|
return item && JSON.parse(item);
|
|
94
93
|
};
|
|
94
|
+
RBSegmentsCacheInLocal.prototype.getAll = function () {
|
|
95
|
+
var _this = this;
|
|
96
|
+
return this.getNames().map(function (key) { return _this.get(key); });
|
|
97
|
+
};
|
|
95
98
|
RBSegmentsCacheInLocal.prototype.contains = function (names) {
|
|
96
99
|
var namesArray = (0, sets_1.setToArray)(names);
|
|
97
100
|
var namesInStorage = this.getNames();
|
|
@@ -99,7 +102,7 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
99
102
|
};
|
|
100
103
|
RBSegmentsCacheInLocal.prototype.getChangeNumber = function () {
|
|
101
104
|
var n = -1;
|
|
102
|
-
var value =
|
|
105
|
+
var value = localStorage.getItem(this.keys.buildRBSegmentsTillKey());
|
|
103
106
|
if (value !== null) {
|
|
104
107
|
value = parseInt(value, 10);
|
|
105
108
|
return (0, lang_1.isNaNNumber)(value) ? n : value;
|
|
@@ -107,7 +110,7 @@ var RBSegmentsCacheInLocal = /** @class */ (function () {
|
|
|
107
110
|
return n;
|
|
108
111
|
};
|
|
109
112
|
RBSegmentsCacheInLocal.prototype.usesSegments = function () {
|
|
110
|
-
var storedCount =
|
|
113
|
+
var storedCount = localStorage.getItem(this.keys.buildSplitsWithSegmentCountKey());
|
|
111
114
|
var splitsWithSegmentsCount = storedCount === null ? 0 : (0, lang_1.toNumber)(storedCount);
|
|
112
115
|
return (0, lang_1.isFiniteNumber)(splitsWithSegmentsCount) ?
|
|
113
116
|
splitsWithSegmentsCount > 0 :
|