-
Notifications
You must be signed in to change notification settings - Fork 73
/
Onyx.js
783 lines (682 loc) · 25.7 KB
/
Onyx.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import _ from 'underscore';
import AsyncStorage from '@react-native-community/async-storage';
import Str from 'expensify-common/lib/str';
import lodashMerge from 'lodash/merge';
import {registerLogger, logInfo, logAlert} from './Logger';
import cache from './OnyxCache';
// Keeps track of the last connectionID that was used so we can keep incrementing it
let lastConnectionID = 0;
// Holds a mapping of all the react components that want their state subscribed to a store key
const callbackToStateMapping = {};
// Stores all of the keys that Onyx can use. Must be defined in init().
let onyxKeys = {};
// Holds a list of keys that have been directly subscribed to or recently modified from least to most recent
let recentlyAccessedKeys = [];
// Holds a list of keys that are safe to remove when we reach max storage. If a key does not match with
// whatever appears in this list it will NEVER be a candidate for eviction.
let evictionAllowList = [];
// Holds a map of keys and connectionID arrays whose keys will never be automatically evicted as
// long as we have at least one subscriber that returns false for the canEvict property.
const evictionBlocklist = {};
// Optional user-provided key value states set when Onyx initializes or clears
let defaultKeyStates = {};
/**
* Get some data from the store
*
* @param {string} key
* @returns {Promise<*>}
*/
function get(key) {
// When we already have the value in cache - resolve right away
if (cache.hasCacheForKey(key)) {
return Promise.resolve(cache.getValue(key));
}
const taskName = `get:${key}`;
// When a value retrieving task for this key is still running hook to it
if (cache.hasPendingTask(taskName)) {
return cache.getTaskPromise(taskName);
}
// Otherwise retrieve the value from storage and capture a promise to aid concurrent usages
const promise = AsyncStorage.getItem(key)
.then((val) => {
const parsed = val && JSON.parse(val);
cache.set(key, parsed);
return parsed;
})
.catch(err => logInfo(`Unable to get item from persistent storage. Key: ${key} Error: ${err}`));
return cache.captureTask(taskName, promise);
}
/**
* Returns current key names stored in persisted storage
* @returns {Promise<string[]>}
*/
function getAllKeys() {
// When we've already read stored keys, resolve right away
const storedKeys = cache.getAllKeys();
if (storedKeys.length > 0) {
return Promise.resolve(storedKeys);
}
const taskName = 'getAllKeys';
// When a value retrieving task for all keys is still running hook to it
if (cache.hasPendingTask(taskName)) {
return cache.getTaskPromise(taskName);
}
// Otherwise retrieve the keys from storage and capture a promise to aid concurrent usages
const promise = AsyncStorage.getAllKeys()
.then((keys) => {
_.each(keys, key => cache.addKey(key));
return keys;
});
return cache.captureTask(taskName, promise);
}
/**
* Checks to see if the a subscriber's supplied key
* is associated with a collection of keys.
*
* @param {String} key
* @returns {Boolean}
*/
function isCollectionKey(key) {
return _.contains(_.values(onyxKeys.COLLECTION), key);
}
/**
* Find the collection a collection item belongs to
* or return null if them item is not a part of a collection
* @param {string} key
* @returns {string|null}
*/
function getCollectionKeyForItem(key) {
return _.chain(onyxKeys.COLLECTION)
.values()
.find(name => key.startsWith(name))
.value();
}
/**
* Checks to see if a given key matches with the
* configured key of our connected subscriber
*
* @param {String} configKey
* @param {String} key
* @return {Boolean}
*/
function isKeyMatch(configKey, key) {
return isCollectionKey(configKey)
? Str.startsWith(key, configKey)
: configKey === key;
}
/**
* Checks to see if this key has been flagged as
* safe for removal.
*
* @param {String} testKey
* @returns {Boolean}
*/
function isSafeEvictionKey(testKey) {
return _.some(evictionAllowList, key => isKeyMatch(key, testKey));
}
/**
* Remove a key from the recently accessed key list.
*
* @param {String} key
*/
function removeLastAccessedKey(key) {
recentlyAccessedKeys = _.without(recentlyAccessedKeys, key);
}
/**
* Add a key to the list of recently accessed keys. The least
* recently accessed key should be at the head and the most
* recently accessed key at the tail.
*
* @param {String} key
*/
function addLastAccessedKey(key) {
// Only specific keys belong in this list since we cannot remove an entire collection.
if (isCollectionKey(key) || !isSafeEvictionKey(key)) {
return;
}
removeLastAccessedKey(key);
recentlyAccessedKeys.push(key);
}
/**
* Removes a key previously added to this list
* which will enable it to be deleted again.
*
* @param {String} key
* @param {Number} connectionID
*/
function removeFromEvictionBlockList(key, connectionID) {
evictionBlocklist[key] = _.without(evictionBlocklist[key] || [], connectionID);
// Remove the key if there are no more subscribers
if (evictionBlocklist[key].length === 0) {
delete evictionBlocklist[key];
}
}
/**
* Keys added to this list can never be deleted.
*
* @param {String} key
* @param {Number} connectionID
*/
function addToEvictionBlockList(key, connectionID) {
removeFromEvictionBlockList(key, connectionID);
if (!evictionBlocklist[key]) {
evictionBlocklist[key] = [];
}
evictionBlocklist[key].push(connectionID);
}
/**
* Take all the keys that are safe to evict and add them to
* the recently accessed list when initializing the app. This
* enables keys that have not recently been accessed to be
* removed.
*/
function addAllSafeEvictionKeysToRecentlyAccessedList() {
getAllKeys()
.then((keys) => {
_.each(evictionAllowList, (safeEvictionKey) => {
_.each(keys, (key) => {
if (isKeyMatch(safeEvictionKey, key)) {
addLastAccessedKey(key);
}
});
});
});
}
/**
* When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks
*
* @param {String} collectionKey
* @param {Object} collection
*/
function keysChanged(collectionKey, collection) {
// Find all subscribers that were added with connect() and trigger the callback or setState() with the new data
_.each(callbackToStateMapping, (subscriber) => {
if (!subscriber) {
return;
}
const isSubscribedToCollectionKey = isKeyMatch(subscriber.key, collectionKey)
&& isCollectionKey(subscriber.key);
const isSubscribedToCollectionMemberKey = subscriber.key.startsWith(collectionKey);
if (isSubscribedToCollectionKey) {
if (_.isFunction(subscriber.callback)) {
_.each(collection, (data, dataKey) => {
subscriber.callback(data, dataKey);
});
} else if (subscriber.withOnyxInstance) {
subscriber.withOnyxInstance.setState((prevState) => {
const finalCollection = _.clone(prevState[subscriber.statePropertyName] || {});
_.each(collection, (data, dataKey) => {
if (finalCollection[dataKey]) {
lodashMerge(finalCollection[dataKey], data);
} else {
finalCollection[dataKey] = data;
}
});
return {
[subscriber.statePropertyName]: finalCollection,
};
});
}
} else if (isSubscribedToCollectionMemberKey) {
const dataFromCollection = collection[subscriber.key];
// If `dataFromCollection` happens to not exist, then return early so that there are no unnecessary
// re-renderings of the component
if (_.isUndefined(dataFromCollection)) {
return;
}
subscriber.withOnyxInstance.setState(prevState => ({
[subscriber.statePropertyName]: _.isObject(dataFromCollection)
? {
...prevState[subscriber.statePropertyName],
...dataFromCollection,
}
: dataFromCollection,
}));
}
});
}
/**
* When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks
*
* @param {string} key
* @param {mixed} data
*/
function keyChanged(key, data) {
// Add or remove this key from the recentlyAccessedKeys lists
if (!_.isNull(data)) {
addLastAccessedKey(key);
} else {
removeLastAccessedKey(key);
}
// Find all subscribers that were added with connect() and trigger the callback or setState() with the new data
_.each(callbackToStateMapping, (subscriber) => {
if (subscriber && isKeyMatch(subscriber.key, key)) {
if (_.isFunction(subscriber.callback)) {
subscriber.callback(data, key);
}
if (!subscriber.withOnyxInstance) {
return;
}
// Check if we are subscribing to a collection key and add this item as a collection
if (isCollectionKey(subscriber.key)) {
subscriber.withOnyxInstance.setState((prevState) => {
const collection = _.clone(prevState[subscriber.statePropertyName] || {});
collection[key] = data;
return {
[subscriber.statePropertyName]: collection,
};
});
} else {
subscriber.withOnyxInstance.setState({
[subscriber.statePropertyName]: data,
});
}
}
});
}
/**
* Sends the data obtained from the keys to the connection. It either:
* - sets state on the withOnyxInstances
* - triggers the callback function
*
* @param {object} config
* @param {object} [config.withOnyxInstance]
* @param {string} [config.statePropertyName]
* @param {function} [config.callback]
* @param {*|null} val
* @param {String} key
*/
function sendDataToConnection(config, val, key) {
if (config.withOnyxInstance) {
config.withOnyxInstance.setState({
[config.statePropertyName]: val,
});
} else if (_.isFunction(config.callback)) {
config.callback(val, key);
}
}
/**
* Subscribes a react component's state directly to a store key
*
* @param {object} mapping the mapping information to connect Onyx to the components state
* @param {string} mapping.key
* @param {string} mapping.statePropertyName the name of the property in the state to connect the data to
* @param {object} [mapping.withOnyxInstance] whose setState() method will be called with any changed data
* This is used by React components to connect to Onyx
* @param {object} [mapping.callback] a method that will be called with changed data
* This is used by any non-React code to connect to Onyx
* @param {boolean} [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the
* component
* @returns {number} an ID to use when calling disconnect
*/
function connect(mapping) {
const connectionID = lastConnectionID++;
callbackToStateMapping[connectionID] = mapping;
if (mapping.initWithStoredValues === false) {
return connectionID;
}
// Check to see if this key is flagged as a safe eviction key and add it to the recentlyAccessedKeys list
if (mapping.withOnyxInstance && !isCollectionKey(mapping.key) && isSafeEvictionKey(mapping.key)) {
// All React components subscribing to a key flagged as a safe eviction
// key must implement the canEvict property.
if (_.isUndefined(mapping.canEvict)) {
// eslint-disable-next-line max-len
throw new Error(`Cannot subscribe to safe eviction key '${mapping.key}' without providing a canEvict value.`);
}
addLastAccessedKey(mapping.key);
}
getAllKeys()
.then((keys) => {
// Find all the keys matched by the config key
const matchingKeys = _.filter(keys, key => isKeyMatch(mapping.key, key));
// If the key being connected to does not exist, initialize the value with null
if (matchingKeys.length === 0) {
sendDataToConnection(mapping, null);
return;
}
// When using a callback subscriber we will trigger the callback
// for each key we find. It's up to the subscriber to know whether
// to expect a single key or multiple keys in the case of a collection.
// React components are an exception since we'll want to send their
// initial data as a single object when using collection keys.
if (mapping.withOnyxInstance && isCollectionKey(mapping.key)) {
Promise.all(_.map(matchingKeys, key => get(key)))
.then(values => _.reduce(values, (finalObject, value, i) => ({
...finalObject,
[matchingKeys[i]]: value,
}), {}))
.then(val => sendDataToConnection(mapping, val));
} else {
_.each(matchingKeys, (key) => {
get(key).then(val => sendDataToConnection(mapping, val, key));
});
}
});
return connectionID;
}
/**
* Remove cache items that are no longer connected through Onyx
* @param {string} key
*/
function cleanCache(key) {
const hasRemainingConnections = _.some(callbackToStateMapping, {key});
// When the key is still used in other places don't remove it from cache
if (hasRemainingConnections) {
return;
}
// When this is a collection - also recursively remove any unused individual items
if (isCollectionKey(key)) {
cache.remove(key);
getAllKeys().then(cachedKeys => _.chain(cachedKeys)
.filter(name => name.startsWith(key))
.forEach(cleanCache));
return;
}
// When this is a collection item - check if the collection is still used
const collectionKey = getCollectionKeyForItem(key);
if (collectionKey) {
// When there's an active subscription for a collection don't remove the item
const hasRemainingConnectionsForCollection = _.some(callbackToStateMapping, {key: collectionKey});
if (hasRemainingConnectionsForCollection) {
return;
}
}
// Otherwise remove the item from cache
cache.remove(key);
}
/**
* Remove the listener for a react component
*
* @param {Number} connectionID
* @param {String} [keyToRemoveFromEvictionBlocklist]
*/
function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {
if (!callbackToStateMapping[connectionID]) {
return;
}
// Remove this key from the eviction block list as we are no longer
// subscribing to it and it should be safe to delete again
if (keyToRemoveFromEvictionBlocklist) {
removeFromEvictionBlockList(keyToRemoveFromEvictionBlocklist, connectionID);
}
const key = callbackToStateMapping[connectionID].key;
delete callbackToStateMapping[connectionID];
// When the last subscriber disconnects, drop cache as well
cleanCache(key);
}
/**
* Remove a key from Onyx and update the subscribers
*
* @param {String} key
* @return {Promise}
*/
function remove(key) {
// Remove from cache
cache.remove(key);
// Optimistically inform subscribers
keyChanged(key, null);
return AsyncStorage.removeItem(key);
}
/**
* If we fail to set or merge we must handle this by
* evicting some data from Onyx and then retrying to do
* whatever it is we attempted to do.
*
* @param {Error} error
* @param {Function} ionMethod
* @param {...any} args
* @return {Promise}
*/
function evictStorageAndRetry(error, ionMethod, ...args) {
// Find the first key that we can remove that has no subscribers in our blocklist
const keyForRemoval = _.find(recentlyAccessedKeys, key => !evictionBlocklist[key]);
if (!keyForRemoval) {
logAlert('Out of storage. But found no acceptable keys to remove.');
throw error;
}
// Remove the least recently viewed key that is not currently being accessed and retry.
logInfo(`Out of storage. Evicting least recently accessed key (${keyForRemoval}) and retrying.`);
return remove(keyForRemoval)
.then(() => ionMethod(...args));
}
/**
* Write a value to our store with the given key
*
* @param {string} key
* @param {mixed} val
* @returns {Promise}
*/
function set(key, val) {
// Adds the key to cache when it's not available
cache.set(key, val);
// Optimistically inform subscribers
keyChanged(key, val);
// Write the thing to persistent storage, which will trigger a storage event for any other tabs open on this domain
return AsyncStorage.setItem(key, JSON.stringify(val))
.catch(error => evictStorageAndRetry(error, set, key, val));
}
/**
* AsyncStorage expects array like: [["@MyApp_user", "value_1"], ["@MyApp_key", "value_2"]]
* This method transforms an object like {'@MyApp_user': 'myUserValue', '@MyApp_key': 'myKeyValue'}
* to an array of key-value pairs in the above format
* @param {Record<string, *>} data
* @return {Array<[string, string]>} an array of stringified key - value pairs
*/
function prepareKeyValuePairsForStorage(data) {
return _.keys(data).map(key => [key, JSON.stringify(data[key])]);
}
/**
* Sets multiple keys and values. Example
* Onyx.multiSet({'key1': 'a', 'key2': 'b'});
*
* @param {object} data
* @returns {Promise}
*/
function multiSet(data) {
const keyValuePairs = prepareKeyValuePairsForStorage(data);
_.each(data, (val, key) => {
// Update cache and optimistically inform subscribers
cache.set(key, val);
keyChanged(key, val);
});
return AsyncStorage.multiSet(keyValuePairs)
.catch(error => evictStorageAndRetry(error, multiSet, data));
}
// Key/value store of Onyx key and arrays of values to merge
const mergeQueue = {};
/**
* Given an Onyx key and value this method will combine all queued
* value updates and return a single value. Merge attempts are
* batched. They must occur after a single call to get() so we
* can avoid race conditions.
*
* @param {String} key
* @param {*} data
*
* @returns {*}
*/
function applyMerge(key, data) {
const mergeValues = mergeQueue[key];
if (_.isArray(data) || _.every(mergeValues, _.isArray)) {
// Array values will always just concatenate
// more items onto the end of the array
return _.reduce(mergeValues, (modifiedData, mergeValue) => [
...modifiedData,
...mergeValue,
], data || []);
}
if (_.isObject(data) || _.every(mergeValues, _.isObject)) {
// Object values are merged one after the other
return _.reduce(mergeValues, (modifiedData, mergeValue) => {
const newData = lodashMerge({}, modifiedData, mergeValue);
// We will also delete any object keys that are undefined or null.
// Deleting keys is not supported by AsyncStorage so we do it this way.
// Remove all first level keys that are explicitly set to null.
return _.omit(newData, (value, finalObjectKey) => _.isNull(mergeValue[finalObjectKey]));
}, data || {});
}
// If we have anything else we can't merge it so we'll
// simply return the last value that was queued
return _.last(mergeValues);
}
/**
* Merge a new value into an existing value at a key
*
* @param {string} key
* @param {*} val
* @returns {Promise}
*/
function merge(key, val) {
if (mergeQueue[key]) {
mergeQueue[key].push(val);
return Promise.resolve();
}
mergeQueue[key] = [val];
return get(key)
.then((data) => {
const modifiedData = applyMerge(key, data);
// Clean up the write queue so we
// don't apply these changes again
delete mergeQueue[key];
return set(key, modifiedData);
});
}
/**
* Merge user provided default key value pairs.
*/
function initializeWithDefaultKeyStates() {
_.each(defaultKeyStates, (state, key) => merge(key, state));
}
/**
* Clear out all the data in the store
*
* @returns {Promise<void>}
*/
function clear() {
return getAllKeys()
.then((keys) => {
_.each(keys, (key) => {
keyChanged(key, null);
cache.remove(key);
});
})
.then(AsyncStorage.clear)
.then(initializeWithDefaultKeyStates);
}
/**
* Merges a collection based on their keys
*
* @param {String} collectionKey
* @param {Object} collection
* @returns {Promise}
*/
function mergeCollection(collectionKey, collection) {
// Confirm all the collection keys belong to the same parent
_.each(collection, (data, dataKey) => {
if (!isKeyMatch(collectionKey, dataKey)) {
// eslint-disable-next-line max-len
throw new Error(`Provided collection does not have all its data belonging to the same parent. CollectionKey: ${collectionKey}, DataKey: ${dataKey}`);
}
});
return getAllKeys()
.then((persistedKeys) => {
// Split to keys that exist in storage and keys that don't
const [existingKeys, newKeys] = _.chain(collection)
.keys()
.partition(key => persistedKeys.includes(key))
.value();
const existingKeyCollection = _.pick(collection, existingKeys);
const newCollection = _.pick(collection, newKeys);
const keyValuePairsForExistingCollection = prepareKeyValuePairsForStorage(existingKeyCollection);
const keyValuePairsForNewCollection = prepareKeyValuePairsForStorage(newCollection);
// New keys will be added via multiSet while existing keys will be updated using multiMerge
// This is because setting a key that doesn't exist yet with multiMerge will throw errors
const existingCollectionPromise = AsyncStorage.multiMerge(keyValuePairsForExistingCollection);
const newCollectionPromise = AsyncStorage.multiSet(keyValuePairsForNewCollection);
// Merge original data to cache
cache.merge(collection);
// Optimistically inform collection subscribers
keysChanged(collectionKey, collection);
return Promise.all([existingCollectionPromise, newCollectionPromise])
.catch(error => evictStorageAndRetry(error, mergeCollection, collection));
});
}
/**
* Initialize the store with actions and listening for storage events
*
* @param {Object} [options]
* @param {String[]} [options.safeEvictionKeys] This is an array of keys
* (individual or collection patterns) that when provided to Onyx are flagged
* as "safe" for removal. Any components subscribing to these keys must also
* implement a canEvict option. See the README for more info.
* @param {function} registerStorageEventListener a callback when a storage event happens.
* This applies to web platforms where the local storage emits storage events
* across all open tabs and allows Onyx to stay in sync across all open tabs.
* @param {Boolean} [options.captureMetrics]
*/
function init({
keys,
initialKeyStates,
safeEvictionKeys,
registerStorageEventListener,
captureMetrics = false,
}) {
if (captureMetrics) {
// The code here is only bundled and applied when the captureMetrics is set
// eslint-disable-next-line no-use-before-define
applyDecorators();
}
// Let Onyx know about all of our keys
onyxKeys = keys;
// Set our default key states to use when initializing and clearing Onyx data
defaultKeyStates = initialKeyStates;
// Let Onyx know about which keys are safe to evict
evictionAllowList = safeEvictionKeys;
addAllSafeEvictionKeysToRecentlyAccessedList();
// Initialize all of our keys with data provided
initializeWithDefaultKeyStates();
// Update any key whose value changes in storage
registerStorageEventListener((key, newValue) => keyChanged(key, newValue));
}
const Onyx = {
connect,
disconnect,
set,
multiSet,
merge,
mergeCollection,
clear,
init,
registerLogger,
addToEvictionBlockList,
removeFromEvictionBlockList,
isSafeEvictionKey,
};
/**
* Apply calls statistic decorators to benchmark Onyx
*/
function applyDecorators() {
// We're requiring the script dynamically here so that it's only evaluated when decorators are used
const decorate = require('./decorateWithMetrics');
// Re-assign with decorated functions
/* eslint-disable no-func-assign */
get = decorate.decorateWithMetrics(get, 'Onyx:get');
set = decorate.decorateWithMetrics(set, 'Onyx:set');
multiSet = decorate.decorateWithMetrics(multiSet, 'Onyx:multiSet');
clear = decorate.decorateWithMetrics(clear, 'Onyx:clear');
merge = decorate.decorateWithMetrics(merge, 'Onyx:merge');
mergeCollection = decorate.decorateWithMetrics(mergeCollection, 'Onyx:mergeCollection');
getAllKeys = decorate.decorateWithMetrics(getAllKeys, 'Onyx:getAllKeys');
/* eslint-enable */
// Re-expose decorated methods
Onyx.set = set;
Onyx.multiSet = multiSet;
Onyx.clear = clear;
Onyx.merge = merge;
Onyx.mergeCollection = mergeCollection;
// Expose stats methods on Onyx
Onyx.getMetrics = decorate.getMetrics;
Onyx.resetMetrics = decorate.resetMetrics;
Onyx.printMetrics = decorate.printMetrics;
}
export default Onyx;