-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (70 loc) · 3.63 KB
/
index.ts
File metadata and controls
83 lines (70 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { ImpressionsCacheInMemory } from '../inMemory/ImpressionsCacheInMemory';
import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
import { EventsCacheInMemory } from '../inMemory/EventsCacheInMemory';
import { IStorageFactoryParams, IStorageSync, IStorageSyncFactory } from '../types';
import { validatePrefix } from '../KeyBuilder';
import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable';
import { SplitsCacheInLocal } from './SplitsCacheInLocal';
import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
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 { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
export interface InLocalStorageOptions {
prefix?: string
}
/**
* InLocal storage factory for standalone client-side SplitFactory
*/
export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyncFactory {
const prefix = validatePrefix(options.prefix);
function InLocalStorageCSFactory(params: IStorageFactoryParams): IStorageSync {
// 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);
}
const { settings, settings: { log, scheduler: { impressionsQueueSize, eventsQueueSize } } } = params;
const matchingKey = getMatching(settings.core.key);
const keys = new KeyBuilderCS(prefix, matchingKey);
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
const splits = new SplitsCacheInLocal(settings, keys, expirationTimestamp);
const rbSegments = new RBSegmentsCacheInLocal(settings, keys);
const segments = new MySegmentsCacheInLocal(log, keys);
const largeSegments = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey));
return {
splits,
rbSegments,
segments,
largeSegments,
impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
impressionCounts: new ImpressionCountsCacheInMemory(),
events: new EventsCacheInMemory(eventsQueueSize),
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
uniqueKeys: new UniqueKeysCacheInMemoryCS(),
destroy() { },
// When using shared instantiation with MEMORY we reuse everything but segments (they are customer per key).
shared(matchingKey: string) {
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() { }
};
},
};
}
InLocalStorageCSFactory.type = STORAGE_LOCALSTORAGE;
return InLocalStorageCSFactory;
}