UNPKG

@splitsoftware/splitio-commons

Version:
72 lines (71 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UniqueKeysCacheInMemoryCS = void 0; var constants_1 = require("../inRedis/constants"); var sets_1 = require("../../utils/lang/sets"); var UniqueKeysCacheInMemoryCS = /** @class */ (function () { function UniqueKeysCacheInMemoryCS(uniqueKeysQueueSize) { if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = constants_1.DEFAULT_CACHE_SIZE; } this.uniqueTrackerSize = 0; this.uniqueKeysTracker = {}; this.maxStorage = uniqueKeysQueueSize; } UniqueKeysCacheInMemoryCS.prototype.setOnFullQueueCb = function (cb) { this.onFullQueue = cb; }; /** * Store unique keys per feature. */ UniqueKeysCacheInMemoryCS.prototype.track = function (userKey, featureName) { if (!this.uniqueKeysTracker[userKey]) this.uniqueKeysTracker[userKey] = new Set(); var tracker = this.uniqueKeysTracker[userKey]; if (!tracker.has(featureName)) { tracker.add(featureName); this.uniqueTrackerSize++; } if (this.uniqueTrackerSize >= this.maxStorage && this.onFullQueue) { this.onFullQueue(); } }; /** * Clear the data stored on the cache. */ UniqueKeysCacheInMemoryCS.prototype.clear = function () { this.uniqueTrackerSize = 0; this.uniqueKeysTracker = {}; }; /** * Pop the collected data, used as payload for posting. */ UniqueKeysCacheInMemoryCS.prototype.pop = function () { var data = this.uniqueKeysTracker; this.clear(); return this.fromUniqueKeysCollector(data); }; /** * Check if the cache is empty. */ UniqueKeysCacheInMemoryCS.prototype.isEmpty = function () { return Object.keys(this.uniqueKeysTracker).length === 0; }; /** * Converts `uniqueKeys` data from cache into request payload. */ UniqueKeysCacheInMemoryCS.prototype.fromUniqueKeysCollector = function (uniqueKeys) { var payload = []; var userKeys = Object.keys(uniqueKeys); for (var k = 0; k < userKeys.length; k++) { var userKey = userKeys[k]; var featureNames = (0, sets_1.setToArray)(uniqueKeys[userKey]); var uniqueKeysPayload = { k: userKey, fs: featureNames }; payload.push(uniqueKeysPayload); } return { keys: payload }; }; return UniqueKeysCacheInMemoryCS; }()); exports.UniqueKeysCacheInMemoryCS = UniqueKeysCacheInMemoryCS;