@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
74 lines (73 loc) • 4.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InLocalStorage = void 0;
var ImpressionsCacheInMemory_1 = require("../inMemory/ImpressionsCacheInMemory");
var ImpressionCountsCacheInMemory_1 = require("../inMemory/ImpressionCountsCacheInMemory");
var EventsCacheInMemory_1 = require("../inMemory/EventsCacheInMemory");
var KeyBuilder_1 = require("../KeyBuilder");
var KeyBuilderCS_1 = require("../KeyBuilderCS");
var isLocalStorageAvailable_1 = require("../../utils/env/isLocalStorageAvailable");
var SplitsCacheInLocal_1 = require("./SplitsCacheInLocal");
var RBSegmentsCacheInLocal_1 = require("./RBSegmentsCacheInLocal");
var MySegmentsCacheInLocal_1 = require("./MySegmentsCacheInLocal");
var InMemoryStorageCS_1 = require("../inMemory/InMemoryStorageCS");
var constants_1 = require("./constants");
var constants_2 = require("../../utils/constants");
var TelemetryCacheInMemory_1 = require("../inMemory/TelemetryCacheInMemory");
var UniqueKeysCacheInMemoryCS_1 = require("../inMemory/UniqueKeysCacheInMemoryCS");
var key_1 = require("../../utils/key");
var validateCache_1 = require("./validateCache");
/**
* InLocal storage factory for standalone client-side SplitFactory
*/
function InLocalStorage(options) {
if (options === void 0) { options = {}; }
var prefix = (0, KeyBuilder_1.validatePrefix)(options.prefix);
function InLocalStorageCSFactory(params) {
// Fallback to InMemoryStorage if LocalStorage API is not available
if (!(0, isLocalStorageAvailable_1.isLocalStorageAvailable)()) {
params.settings.log.warn(constants_1.LOG_PREFIX + 'LocalStorage API is unavailable. Falling back to default MEMORY storage');
return (0, InMemoryStorageCS_1.InMemoryStorageCSFactory)(params);
}
var settings = params.settings, _a = params.settings, log = _a.log, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize;
var matchingKey = (0, key_1.getMatching)(settings.core.key);
var keys = new KeyBuilderCS_1.KeyBuilderCS(prefix, matchingKey);
var splits = new SplitsCacheInLocal_1.SplitsCacheInLocal(settings, keys);
var rbSegments = new RBSegmentsCacheInLocal_1.RBSegmentsCacheInLocal(settings, keys);
var segments = new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, keys);
var largeSegments = new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, (0, KeyBuilderCS_1.myLargeSegmentsKeyBuilder)(prefix, matchingKey));
return {
splits: splits,
rbSegments: rbSegments,
segments: segments,
largeSegments: largeSegments,
impressions: new ImpressionsCacheInMemory_1.ImpressionsCacheInMemory(impressionsQueueSize),
impressionCounts: new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory(),
events: new EventsCacheInMemory_1.EventsCacheInMemory(eventsQueueSize),
telemetry: (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)(params) ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory(splits, segments) : undefined,
uniqueKeys: new UniqueKeysCacheInMemoryCS_1.UniqueKeysCacheInMemoryCS(),
validateCache: function () {
return (0, validateCache_1.validateCache)(options, settings, keys, splits, rbSegments, segments, largeSegments);
},
destroy: function () { },
// When using shared instantiation with MEMORY we reuse everything but segments (they are customer per key).
shared: function (matchingKey) {
return {
splits: this.splits,
rbSegments: this.rbSegments,
segments: new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, new KeyBuilderCS_1.KeyBuilderCS(prefix, matchingKey)),
largeSegments: new MySegmentsCacheInLocal_1.MySegmentsCacheInLocal(log, (0, KeyBuilderCS_1.myLargeSegmentsKeyBuilder)(prefix, matchingKey)),
impressions: this.impressions,
impressionCounts: this.impressionCounts,
events: this.events,
telemetry: this.telemetry,
uniqueKeys: this.uniqueKeys,
destroy: function () { }
};
},
};
}
InLocalStorageCSFactory.type = constants_2.STORAGE_LOCALSTORAGE;
return InLocalStorageCSFactory;
}
exports.InLocalStorage = InLocalStorage;