react-native-onyx 1.0.39 → 1.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/web.development.js +1 -0
- package/dist/web.development.js.map +1 -1
- package/dist/web.min.js +1 -1
- package/dist/web.min.js.map +1 -1
- package/lib/Onyx.js +1 -0
- package/lib/storage/__mocks__/index.js +78 -0
- package/package.json +1 -6
- package/lib/storage/__mocks__/index.native.js +0 -8
- package/lib/storage/providers/AsyncStorage.js +0 -90
- package/lib/storage/providers/__mocks__/SQLiteStorage.js +0 -3
package/lib/Onyx.js
CHANGED
|
@@ -1100,6 +1100,7 @@ function clear(keysToPreserve = []) {
|
|
|
1100
1100
|
const defaultKeyValuePairs = _.pairs(_.omit(defaultKeyStates, keysToPreserve));
|
|
1101
1101
|
|
|
1102
1102
|
// Remove only the items that we want cleared from storage, and reset others to default
|
|
1103
|
+
_.each(keysToBeClearedFromStorage, key => cache.drop(key));
|
|
1103
1104
|
return Storage.removeItems(keysToBeClearedFromStorage).then(() => Storage.multiSet(defaultKeyValuePairs));
|
|
1104
1105
|
});
|
|
1105
1106
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import _ from 'underscore';
|
|
2
|
+
import fastMerge from '../../fastMerge';
|
|
3
|
+
|
|
4
|
+
let storageMapInternal = {};
|
|
5
|
+
|
|
6
|
+
const set = jest.fn((key, value) => {
|
|
7
|
+
// eslint-disable-next-line no-param-reassign
|
|
8
|
+
storageMapInternal[key] = value;
|
|
9
|
+
return Promise.resolve(storageMapInternal[key]);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const localForageMock = {
|
|
13
|
+
setItem(key, value) {
|
|
14
|
+
set(key, value);
|
|
15
|
+
return Promise.resolve();
|
|
16
|
+
},
|
|
17
|
+
multiSet(pairs) {
|
|
18
|
+
const setPromises = _.map(pairs, ([key, value]) => this.setItem(key, value));
|
|
19
|
+
return new Promise(resolve => Promise.all(setPromises).then(() => resolve(storageMapInternal)));
|
|
20
|
+
},
|
|
21
|
+
getItem(key) {
|
|
22
|
+
return Promise.resolve(storageMapInternal[key]);
|
|
23
|
+
},
|
|
24
|
+
multiGet(keys) {
|
|
25
|
+
const getPromises = _.map(keys, key => new Promise(resolve => this.getItem(key).then(value => resolve([key, value]))));
|
|
26
|
+
return Promise.all(getPromises);
|
|
27
|
+
},
|
|
28
|
+
multiMerge(pairs) {
|
|
29
|
+
_.forEach(pairs, ([key, value]) => {
|
|
30
|
+
const existingValue = storageMapInternal[key];
|
|
31
|
+
const newValue = _.isObject(existingValue)
|
|
32
|
+
? fastMerge(existingValue, value) : value;
|
|
33
|
+
|
|
34
|
+
set(key, newValue);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return Promise.resolve(storageMapInternal);
|
|
38
|
+
},
|
|
39
|
+
removeItem(key) {
|
|
40
|
+
delete storageMapInternal[key];
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
},
|
|
43
|
+
removeItems(keys) {
|
|
44
|
+
_.each(keys, (key) => {
|
|
45
|
+
delete storageMapInternal[key];
|
|
46
|
+
});
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
},
|
|
49
|
+
clear() {
|
|
50
|
+
storageMapInternal = {};
|
|
51
|
+
return Promise.resolve();
|
|
52
|
+
},
|
|
53
|
+
getAllKeys() {
|
|
54
|
+
return Promise.resolve(_.keys(storageMapInternal));
|
|
55
|
+
},
|
|
56
|
+
config() {},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const localForageMockSpy = {
|
|
60
|
+
localForageSet: set,
|
|
61
|
+
setItem: jest.fn(localForageMock.setItem),
|
|
62
|
+
getItem: jest.fn(localForageMock.getItem),
|
|
63
|
+
removeItem: jest.fn(localForageMock.removeItem),
|
|
64
|
+
removeItems: jest.fn(localForageMock.removeItems),
|
|
65
|
+
clear: jest.fn(localForageMock.clear),
|
|
66
|
+
getAllKeys: jest.fn(localForageMock.getAllKeys),
|
|
67
|
+
config: jest.fn(localForageMock.config),
|
|
68
|
+
multiGet: jest.fn(localForageMock.multiGet),
|
|
69
|
+
multiSet: jest.fn(localForageMock.multiSet),
|
|
70
|
+
multiMerge: jest.fn(localForageMock.multiMerge),
|
|
71
|
+
|
|
72
|
+
getStorageMap: jest.fn(() => storageMapInternal),
|
|
73
|
+
setInitialMockData: jest.fn((data) => {
|
|
74
|
+
storageMapInternal = data;
|
|
75
|
+
}),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default localForageMockSpy;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-onyx",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.41",
|
|
4
4
|
"author": "Expensify, Inc.",
|
|
5
5
|
"homepage": "https://expensify.com",
|
|
6
6
|
"description": "State management for React Native",
|
|
@@ -48,7 +48,6 @@
|
|
|
48
48
|
"@babel/core": "7.20.12",
|
|
49
49
|
"@babel/preset-env": "^7.20.2",
|
|
50
50
|
"@babel/preset-react": "^7.18.6",
|
|
51
|
-
"@react-native-async-storage/async-storage": "^1.17.11",
|
|
52
51
|
"@react-native-community/eslint-config": "^2.0.0",
|
|
53
52
|
"@testing-library/jest-native": "^3.4.2",
|
|
54
53
|
"@testing-library/react-native": "^7.0.2",
|
|
@@ -76,7 +75,6 @@
|
|
|
76
75
|
"webpack-merge": "^5.8.0"
|
|
77
76
|
},
|
|
78
77
|
"peerDependencies": {
|
|
79
|
-
"@react-native-async-storage/async-storage": "^1.17.11",
|
|
80
78
|
"expensify-common": ">=1",
|
|
81
79
|
"localforage": "^1.10.0",
|
|
82
80
|
"localforage-removeitems": "^1.4.0",
|
|
@@ -88,9 +86,6 @@
|
|
|
88
86
|
"react-native-performance": {
|
|
89
87
|
"optional": true
|
|
90
88
|
},
|
|
91
|
-
"@react-native-async-storage/async-storage": {
|
|
92
|
-
"optional": true
|
|
93
|
-
},
|
|
94
89
|
"react-native-quick-sqlite": {
|
|
95
90
|
"optional": true
|
|
96
91
|
},
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Because we're using the `react-native` preset of jest this file extension
|
|
3
|
-
* is .native.js. Otherwise, since jest prefers index.native.js over index.js
|
|
4
|
-
* it'll skip loading the mock
|
|
5
|
-
*/
|
|
6
|
-
import WebStorage from '../WebStorage';
|
|
7
|
-
|
|
8
|
-
export default WebStorage;
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The AsyncStorage provider stores everything in a key/value store by
|
|
3
|
-
* converting the value to a JSON string
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import _ from 'underscore';
|
|
7
|
-
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
8
|
-
|
|
9
|
-
const provider = {
|
|
10
|
-
/**
|
|
11
|
-
* Get the value of a given key or return `null` if it's not available in storage
|
|
12
|
-
* @param {String} key
|
|
13
|
-
* @return {Promise<*>}
|
|
14
|
-
*/
|
|
15
|
-
getItem(key) {
|
|
16
|
-
return AsyncStorage.getItem(key)
|
|
17
|
-
.then((value) => {
|
|
18
|
-
const parsed = value && JSON.parse(value);
|
|
19
|
-
return parsed;
|
|
20
|
-
});
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Get multiple key-value pairs for the give array of keys in a batch
|
|
25
|
-
* @param {String[]} keys
|
|
26
|
-
* @return {Promise<Array<[key, value]>>}
|
|
27
|
-
*/
|
|
28
|
-
multiGet(keys) {
|
|
29
|
-
return AsyncStorage.multiGet(keys)
|
|
30
|
-
.then(pairs => _.map(pairs, ([key, value]) => [key, value && JSON.parse(value)]));
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
|
|
35
|
-
* @param {String} key
|
|
36
|
-
* @param {*} value
|
|
37
|
-
* @return {Promise<void>}
|
|
38
|
-
*/
|
|
39
|
-
setItem(key, value) {
|
|
40
|
-
return AsyncStorage.setItem(key, JSON.stringify(value));
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Stores multiple key-value pairs in a batch
|
|
45
|
-
* @param {Array<[key, value]>} pairs
|
|
46
|
-
* @return {Promise<void>}
|
|
47
|
-
*/
|
|
48
|
-
multiSet(pairs) {
|
|
49
|
-
const stringPairs = _.map(pairs, ([key, value]) => [key, JSON.stringify(value)]);
|
|
50
|
-
return AsyncStorage.multiSet(stringPairs);
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Multiple merging of existing and new values in a batch
|
|
55
|
-
* @param {Array<[key, value]>} pairs
|
|
56
|
-
* @return {Promise<void>}
|
|
57
|
-
*/
|
|
58
|
-
multiMerge(pairs) {
|
|
59
|
-
const stringPairs = _.map(pairs, ([key, value]) => [key, JSON.stringify(value)]);
|
|
60
|
-
return AsyncStorage.multiMerge(stringPairs);
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Returns all keys available in storage
|
|
65
|
-
* @returns {Promise<String[]>}
|
|
66
|
-
*/
|
|
67
|
-
getAllKeys: AsyncStorage.getAllKeys,
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Remove given key and it's value from storage
|
|
71
|
-
* @param {String} key
|
|
72
|
-
* @returns {Promise<void>}
|
|
73
|
-
*/
|
|
74
|
-
removeItem: AsyncStorage.removeItem,
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Remove given keys and their values from storage
|
|
78
|
-
* @param {Array} keys
|
|
79
|
-
* @returns {Promise}
|
|
80
|
-
*/
|
|
81
|
-
removeItems: keys => Promise.all(_.map(keys, key => AsyncStorage.removeItem(key))),
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Clear everything from storage
|
|
85
|
-
* @returns {Promise<void>}
|
|
86
|
-
*/
|
|
87
|
-
clear: AsyncStorage.clear,
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export default provider;
|