-
Notifications
You must be signed in to change notification settings - Fork 73
/
MemoryOnlyProvider.ts
144 lines (120 loc) · 3.76 KB
/
MemoryOnlyProvider.ts
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
import _ from 'underscore';
import utils from '../../utils';
import type StorageProvider from './types';
import type {KeyValuePair} from './types';
import type {OnyxKey, OnyxValue} from '../../types';
type Store = Record<OnyxKey, OnyxValue<OnyxKey>>;
// eslint-disable-next-line import/no-mutable-exports
let store: Store = {};
const setInternal = (key: OnyxKey, value: OnyxValue<OnyxKey>) => {
store[key] = value;
return Promise.resolve(value);
};
const isJestRunning = typeof jest !== 'undefined';
const set = isJestRunning ? jest.fn(setInternal) : setInternal;
const provider: StorageProvider = {
/**
* The name of the provider that can be printed to the logs
*/
name: 'MemoryOnlyProvider',
/**
* Initializes the storage provider
*/
init() {
// do nothing
},
/**
* Get the value of a given key or return `null` if it's not available in memory
*/
getItem(key) {
const value = store[key] as OnyxValue<typeof key>;
return Promise.resolve(value === undefined ? (null as OnyxValue<typeof key>) : value);
},
/**
* Get multiple key-value pairs for the give array of keys in a batch.
*/
multiGet(keys) {
const getPromises = _.map(
keys,
(key) =>
new Promise((resolve) => {
this.getItem(key).then((value) => resolve([key, value]));
}),
) as Array<Promise<KeyValuePair>>;
return Promise.all(getPromises);
},
/**
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
*/
setItem(key, value) {
set(key, value);
return Promise.resolve();
},
/**
* Stores multiple key-value pairs in a batch
*/
multiSet(pairs) {
const setPromises = _.map(pairs, ([key, value]) => this.setItem(key, value));
return Promise.all(setPromises).then(() => undefined);
},
/**
* Merging an existing value with a new one
*/
mergeItem(key, _deltaChanges, preMergedValue) {
// Since Onyx already merged the existing value with the changes, we can just set the value directly
return this.setItem(key, preMergedValue);
},
/**
* Multiple merging of existing and new values in a batch
* This function also removes all nested null values from an object.
*/
multiMerge(pairs) {
_.forEach(pairs, ([key, value]) => {
const existingValue = store[key] as Record<string, unknown>;
const newValue = utils.fastMerge(existingValue, value as Record<string, unknown>) as OnyxValue<OnyxKey>;
set(key, newValue);
});
return Promise.resolve([]);
},
/**
* Remove given key and it's value from memory
*/
removeItem(key) {
delete store[key];
return Promise.resolve();
},
/**
* Remove given keys and their values from memory
*/
removeItems(keys) {
_.each(keys, (key) => {
delete store[key];
});
return Promise.resolve();
},
/**
* Clear everything from memory
*/
clear() {
store = {};
return Promise.resolve();
},
/**
* Returns all keys available in memory
*/
getAllKeys() {
return Promise.resolve(_.keys(store));
},
/**
* Gets the total bytes of the store.
* `bytesRemaining` will always be `Number.POSITIVE_INFINITY` since we don't have a hard limit on memory.
*/
getDatabaseSize() {
return Promise.resolve({bytesRemaining: Number.POSITIVE_INFINITY, bytesUsed: 0});
},
};
const setMockStore = (data: Store) => {
store = data;
};
export default provider;
export {store as mockStore, set as mockSet, setMockStore};