-
Notifications
You must be signed in to change notification settings - Fork 74
/
LocalForage.js
114 lines (98 loc) · 3.48 KB
/
LocalForage.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
/**
* @file
* The storage provider based on localforage allows us to store most anything in its
* natural form in the underlying DB without having to stringify or de-stringify it
*/
import localforage from 'localforage';
import _ from 'underscore';
import SyncQueue from '../../SyncQueue';
import fastMerge from '../../fastMerge';
localforage.config({
name: 'OnyxDB',
});
const provider = {
/**
* Writing very quickly to IndexedDB causes performance issues and can lock up the page and lead to jank.
* So, we are slowing this process down by waiting until one write is complete before moving on
* to the next.
*/
setItemQueue: new SyncQueue(({key, value, shouldMerge}) => {
if (shouldMerge) {
return localforage.getItem(key)
.then((existingValue) => {
const newValue = _.isObject(existingValue)
// lodash adds a small overhead so we don't use it here
// eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
? Object.assign({}, fastMerge(existingValue, value))
: value;
return localforage.setItem(key, newValue);
});
}
return localforage.setItem(key, value);
}),
/**
* Get multiple key-value pairs for the give array of keys in a batch
* @param {String[]} keys
* @return {Promise<Array<[key, value]>>}
*/
multiGet(keys) {
const pairs = _.map(
keys,
key => localforage.getItem(key)
.then(value => [key, value]),
);
return Promise.all(pairs);
},
/**
* Multiple merging of existing and new values in a batch
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiMerge(pairs) {
const tasks = _.map(pairs, ([key, value]) => this.setItemQueue.push({key, value, shouldMerge: true}));
// We're returning Promise.resolve, otherwise the array of task results will be returned to the caller
return Promise.all(tasks).then(() => Promise.resolve());
},
/**
* Stores multiple key-value pairs in a batch
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiSet(pairs) {
// We're returning Promise.resolve, otherwise the array of task results will be returned to the caller
const tasks = _.map(pairs, ([key, value]) => this.setItem(key, value));
return Promise.all(tasks).then(() => Promise.resolve());
},
/**
* Clear absolutely everything from storage
* @returns {Promise<void>}
*/
clear: localforage.clear,
/**
* Returns all keys available in storage
* @returns {Promise<String[]>}
*/
getAllKeys: localforage.keys,
/**
* Get the value of a given key or return `null` if it's not available in storage
* @param {String} key
* @return {Promise<*>}
*/
getItem: localforage.getItem,
/**
* Remove given key and it's value from storage
* @param {String} key
* @returns {Promise<void>}
*/
removeItem: localforage.removeItem,
/**
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
* @param {String} key
* @param {*} value
* @return {Promise<void>}
*/
setItem(key, value) {
return this.setItemQueue.push({key, value});
},
};
export default provider;