@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
91 lines (90 loc) • 4.88 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, isValidStorageWrapper, isWebStorage } from '../../utils/env/isLocalStorageAvailable';
import { DefinitionsCacheInLocal } from './DefinitionsCacheInLocal';
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';
import { storageAdapter } from './storageAdapter';
function validateStorage(log, prefix, wrapper) {
if (wrapper) {
if (isValidStorageWrapper(wrapper)) {
return isWebStorage(wrapper) ?
wrapper : // localStorage and sessionStorage don't need adapter
storageAdapter(log, prefix, wrapper);
}
log.warn(LOG_PREFIX + 'Invalid storage provided. Falling back to LocalStorage API');
}
if (isLocalStorageAvailable())
return localStorage;
log.warn(LOG_PREFIX + 'LocalStorage API is unavailable. Falling back to default MEMORY storage');
}
/**
* 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) {
var settings = params.settings, _a = params.settings, log = _a.log, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize;
var storage = validateStorage(log, prefix, options.wrapper);
if (!storage)
return InMemoryStorageCSFactory(params);
var matchingKey = getMatching(settings.core.key);
var keys = new KeyBuilderCS(prefix, matchingKey);
var definitions = new DefinitionsCacheInLocal(settings, keys, storage);
var rbSegments = new RBSegmentsCacheInLocal(settings, keys, storage);
var segments = new MySegmentsCacheInLocal(log, keys, storage);
var largeSegments = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey), storage);
var validateCachePromise;
return {
definitions: definitions,
rbSegments: rbSegments,
segments: segments,
largeSegments: largeSegments,
impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
impressionCounts: new ImpressionCountsCacheInMemory(),
events: new EventsCacheInMemory(eventsQueueSize),
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(definitions, segments) : undefined,
uniqueKeys: new UniqueKeysCacheInMemoryCS(),
validateCache: function () {
if (!validateCachePromise) {
validateCachePromise = validateCache(options, storage, settings, keys, definitions, rbSegments, segments, largeSegments);
}
return validateCachePromise;
},
save: function () {
return storage.save && storage.save();
},
destroy: function () {
return storage.whenSaved && storage.whenSaved();
},
// When using shared instantiation with MEMORY we reuse everything but segments (they are customer per key).
shared: function (matchingKey) {
return {
definitions: this.definitions,
rbSegments: this.rbSegments,
segments: new MySegmentsCacheInLocal(log, new KeyBuilderCS(prefix, matchingKey), storage),
largeSegments: new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey), storage),
impressions: this.impressions,
impressionCounts: this.impressionCounts,
events: this.events,
telemetry: this.telemetry,
uniqueKeys: this.uniqueKeys,
destroy: function () { }
};
},
};
}
InLocalStorageCSFactory.type = STORAGE_LOCALSTORAGE;
return InLocalStorageCSFactory;
}