-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnibelung.js
283 lines (230 loc) · 6.66 KB
/
nibelung.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(function(global) {
'use strict';
var nibelung = {
Hoard: Hoard
};
var __eventSinks = {};
function Hoard(options) {
var namespace = options.namespace;
var ttlMilliseconds = options.ttlMilliseconds;
var maxRecords = options.maxRecords;
var _cache = _createStorageInstance(options.persistent);
var _clock = options.clock || new DefaultClock();
var _reentrancyProtector = options.reentrancyProtector || new DefaultReentrancyProtector();
var _logger = options.logger;
__eventSinks[namespace] = __eventSinks[namespace] || new EventSink([
'PUT', 'REMOVE', 'CLEAR'
]);
var _getRecordsByLastUpdateTime = R.pipe(
R.keys,
R.filter(_isKeyInNamespace),
R.map(_getRecord),
R.reject(R.eq(undefined)),
R.sortBy(R.prop('lastUpdateTimeMs')),
R.reverse);
var _getRecordsByKey = R.pipe(
R.map(_wrapKey),
R.map(_getRecord),
R.reject(R.eq(undefined)));
this.getOne = function getOne(key) {
return this.get([key])[0];
};
this.putOne = function putOne(key, value) {
_put(key, value);
};
this.removeOne = function deleteOne(key) {
this.remove([key]);
};
this.get = function get(keys) {
return R.map(
_unwrapValue,
_getRecordsByKey(keys));
};
this.put = function put(values, keyName) {
R.forEach(function (value) {
_put(value[keyName], value);
}, values);
_enforceMaxRecords();
};
this.remove = function remove(keys) {
R.forEach(function(key) {
var record = _getRecord(_wrapKey(key));
if (record) {
_dropRecord(record);
}
}, keys);
}
/** Filters the keys by what's not already cached. */
this.excludes = function excludes(keys) {
var findRecords = R.pipe(
_getRecordsByKey,
R.pluck('key'),
R.map(_unwrapKey));
return R.difference(keys, findRecords(keys));
};
/**
* Gets the newest records in the cache, up to limit, in descending
* order by last update time.
*/
this.getLatest = function getLatest(limit) {
var computeLatest = R.pipe(
_getRecordsByLastUpdateTime,
R.slice(0, limit),
R.map(R.prop('value')));
return computeLatest(_cache);
};
this.clear = function clear() {
_cache.clear();
_cache = _createStorageInstance(options.persistent);
__eventSinks[namespace].emit('CLEAR', undefined, _reentrancyProtector);
};
this.on = function on(event, handler) {
__eventSinks[namespace].on(event, handler);
};
this.off = function off(event, handler) {
__eventSinks[namespace].off(event, handler);
};
function _wrapKey(key) {
return [namespace, key].join('-');
}
function _unwrapKey(key) {
return key.replace(namespace + '-', '');
}
function _wrapValue(key, value) {
return JSON.stringify({
key: key,
value: value,
lastUpdateTimeMs: _clock.now()
});
}
function _unwrapValue(record) {
if (record) {
return record.value;
}
return undefined;
}
function _isKeyInNamespace(key) {
return key && key.indexOf(namespace) === 0;
}
function _getRecord(key) {
var json = _cache[key];
if (!json) {
return undefined;
}
var record = JSON.parse(json);
if (record && _isRecordExpired(record)) {
_dropRecord(record);
return undefined;
}
return record;
}
function _enforceMaxRecords() {
if (!maxRecords) {
return;
}
var allRecords = _getRecordsByLastUpdateTime(_cache);
var recordsOverCap = R.slice(maxRecords, allRecords.length)(
allRecords);
_dropRecords(recordsOverCap);
}
function _dropRecords(records) {
records.forEach(_dropRecord);
}
function _isRecordExpired(record) {
if (!ttlMilliseconds) {
return false;
}
return _clock.now() - record.lastUpdateTimeMs >
ttlMilliseconds;
}
function _dropRecord(record) {
_cache.removeItem(record.key);
__eventSinks[namespace].emit(
'REMOVE',
_unwrapValue(record),
_reentrancyProtector);
}
function _put(key, value) {
var cacheKey = _wrapKey(key);
_cache[cacheKey] = _wrapValue(cacheKey, value);
__eventSinks[namespace].emit('PUT', value, _reentrancyProtector);
}
function _createStorageInstance(persistent) {
try {
var storage = persistent ? window.localStorage : window.sessionStorage;
// Test that we can actually use the storage:
storage.setItem('test', true);
return storage;
}
catch (e) {
_log('Unable to create storage, falling back to in-memory data: ' + e);
return {
clear: function () {},
removeItem: function (key) {
this[key] = undefined;
}
};
}
}
function _log(message) {
if (!_logger) {
return;
}
_logger(
['[' + options.namespace + ']',
(options.persistent ? '[persistent]' : ''),
message].join(' '));
}
}
function DefaultClock() {
this.now = function() {
return Date.now();
}
}
// Protects emit calls against re-entrancy and exception-prone handlers.
function DefaultReentrancyProtector() {
this.protect = function(fn) {
return window.setTimeout(fn, 0);
}
}
function EventSink(legalEvents) {
var _handlers = {};
var _legalEvents = [].concat(legalEvents); // Defensive copy.
this.on = function on(event, handler) {
_assertLegalEvent(event);
if (!_handlers[event]) {
_handlers[event] = [];
}
_handlers[event].push(handler);
}
this.off = function off(event, handler) {
_assertLegalEvent(event);
if (_handlers[event]) {
_handlers[event] = R.reject(R.eq(handler), _handlers[event]);
}
}
this.emit = function emit(event, data, reentrancyProtector) {
_assertLegalEvent(event);
if (!_handlers[event] || !_handlers[event].length) {
return;
}
R.forEach(function _handle(handler) {
reentrancyProtector.protect(function () {
handler(event, data);
}, 0);
}, _handlers[event]);
}
function _assertLegalEvent(event) {
if (!R.contains(event, _legalEvents)) {
throw new Error('Invalid event: ' + event);
}
}
}
if (typeof exports === 'object') {
module.exports = nibelung;
} else if (typeof define === 'function' && define.amd) {
define(function() { return nibelung; });
} else {
global.nibelung = nibelung;
}
})(this);