-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathbrowser.js
More file actions
120 lines (102 loc) · 3.92 KB
/
browser.js
File metadata and controls
120 lines (102 loc) · 3.92 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import SplitCacheInMemory from './SplitCache/InMemory';
import SplitCacheInLocalStorage from './SplitCache/InLocalStorage';
import SegmentCacheInMemory from './SegmentCache/InMemory';
import SegmentCacheInLocalStorage from './SegmentCache/InLocalStorage';
import ImpressionsCacheInMemory from './ImpressionsCache/InMemory';
import LatencyCacheInMemory from './LatencyCache/InMemory';
import CountCacheInMemory from './CountCache/InMemory';
import EventsCacheInMemory from './EventsCache/InMemory';
import KeyBuilder from './Keys';
import KeyBuilderLocalStorage from './KeysLocalStorage';
import { STORAGE_MEMORY, STORAGE_LOCALSTORAGE } from '../utils/constants';
// This value might be eventually set via a config parameter
export const DEFAULT_CACHE_EXPIRATION_IN_MILLIS = 864000000; // 10 days
const BrowserStorageFactory = context => {
const settings = context.get(context.constants.SETTINGS);
const { storage } = settings;
let instance;
switch (storage.type) {
case STORAGE_MEMORY: {
const keys = new KeyBuilder(settings);
instance = {
splits: new SplitCacheInMemory,
segments: new SegmentCacheInMemory(keys),
impressions: new ImpressionsCacheInMemory,
metrics: new LatencyCacheInMemory,
count: new CountCacheInMemory,
events: new EventsCacheInMemory(context),
// When using shared instanciation with MEMORY we reuse everything but segments (they are customer per key).
shared(settings) {
const childKeyBuilder = new KeyBuilder(settings);
return {
splits: this.splits,
segments: new SegmentCacheInMemory(childKeyBuilder),
impressions: this.impressions,
metrics: this.metrics,
count: this.count,
events: this.events,
destroy() {
this.splits = new SplitCacheInMemory;
this.segments.flush();
}
};
},
destroy() {
this.splits.flush();
this.segments.flush();
this.impressions.clear();
this.metrics.clear();
this.count.clear();
this.events.clear();
}
};
break;
}
case STORAGE_LOCALSTORAGE: {
const keys = new KeyBuilderLocalStorage(settings);
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
instance = {
splits: new SplitCacheInLocalStorage(keys, expirationTimestamp, settings.sync.__splitFiltersValidation),
segments: new SegmentCacheInLocalStorage(keys),
impressions: new ImpressionsCacheInMemory,
metrics: new LatencyCacheInMemory,
count: new CountCacheInMemory,
events: new EventsCacheInMemory(context),
// When using shared instanciation with MEMORY we reuse everything but segments (they are customer per key).
shared(settings) {
const childKeysBuilder = new KeyBuilderLocalStorage(settings);
return {
splits: this.splits,
segments: new SegmentCacheInLocalStorage(childKeysBuilder),
impressions: this.impressions,
metrics: this.metrics,
count: this.count,
events: this.events,
destroy() {
this.splits = new SplitCacheInMemory;
this.segments = new SegmentCacheInMemory(childKeysBuilder);
}
};
},
destroy() {
this.splits = new SplitCacheInMemory;
this.segments = new SegmentCacheInMemory(new KeyBuilder(settings));
this.impressions.clear();
this.metrics.clear();
this.count.clear();
this.events.clear();
}
};
break;
}
default:
throw new Error('Unsupported storage type');
}
// load precached data into storage
if (storage.dataLoader) {
const key = settings.core.key;
storage.dataLoader(instance, key);
}
return instance;
};
export default BrowserStorageFactory;