@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
70 lines (69 loc) • 3.94 kB
JavaScript
import { ImpressionsCacheInMemory } from '../inMemory/ImpressionsCacheInMemory';
import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
import { EventsCacheInMemory } from '../inMemory/EventsCacheInMemory';
import { validatePrefix } from '../KeyBuilder';
import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable';
import { SplitsCacheInLocal } from './SplitsCacheInLocal';
import { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
import { LOG_PREFIX } from './constants';
import { STORAGE_LOCALSTORAGE } from '../../utils/constants';
import { shouldRecordTelemetry, TelemetryCacheInMemory } from '../inMemory/TelemetryCacheInMemory';
import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS';
import { getMatching } from '../../utils/key';
import { validateCache } from './validateCache';
/**
* InLocal storage factory for standalone client-side SplitFactory
*/
export function InLocalStorage(options) {
if (options === void 0) { options = {}; }
var prefix = validatePrefix(options.prefix);
function InLocalStorageCSFactory(params) {
// Fallback to InMemoryStorage if LocalStorage API is not available
if (!isLocalStorageAvailable()) {
params.settings.log.warn(LOG_PREFIX + 'LocalStorage API is unavailable. Falling back to default MEMORY storage');
return InMemoryStorageCSFactory(params);
}
var settings = params.settings, _a = params.settings, log = _a.log, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize;
var matchingKey = getMatching(settings.core.key);
var keys = new KeyBuilderCS(prefix, matchingKey);
var splits = new SplitsCacheInLocal(settings, keys);
var rbSegments = new RBSegmentsCacheInLocal(settings, keys);
var segments = new MySegmentsCacheInLocal(log, keys);
var largeSegments = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey));
return {
splits: splits,
rbSegments: rbSegments,
segments: segments,
largeSegments: largeSegments,
impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
impressionCounts: new ImpressionCountsCacheInMemory(),
events: new EventsCacheInMemory(eventsQueueSize),
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
uniqueKeys: new UniqueKeysCacheInMemoryCS(),
validateCache: function () {
return 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(log, new KeyBuilderCS(prefix, matchingKey)),
largeSegments: new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey)),
impressions: this.impressions,
impressionCounts: this.impressionCounts,
events: this.events,
telemetry: this.telemetry,
uniqueKeys: this.uniqueKeys,
destroy: function () { }
};
},
};
}
InLocalStorageCSFactory.type = STORAGE_LOCALSTORAGE;
return InLocalStorageCSFactory;
}