react-native-onyx 1.0.80 → 1.0.81
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 +55 -29
- 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 +4 -21
- package/lib/fastMerge.js +3 -4
- package/lib/storage/providers/IDBKeyVal.js +2 -1
- package/lib/utils.js +23 -0
- package/package.json +1 -1
package/lib/Onyx.js
CHANGED
|
@@ -8,6 +8,7 @@ import createDeferredTask from './createDeferredTask';
|
|
|
8
8
|
import fastMerge from './fastMerge';
|
|
9
9
|
import * as PerformanceUtils from './metrics/PerformanceUtils';
|
|
10
10
|
import Storage from './storage';
|
|
11
|
+
import Utils from './utils';
|
|
11
12
|
import unstable_batchedUpdates from './batch';
|
|
12
13
|
|
|
13
14
|
// Method constants
|
|
@@ -990,24 +991,6 @@ function hasPendingMergeForKey(key) {
|
|
|
990
991
|
return Boolean(mergeQueue[key]);
|
|
991
992
|
}
|
|
992
993
|
|
|
993
|
-
/**
|
|
994
|
-
* We generally want to remove top-level nullish values from objects written to disk and cache, because it decreases the amount of data stored in memory and on disk.
|
|
995
|
-
* On native, when merging an existing value with new changes, SQLite will use JSON_PATCH, which removes top-level nullish values.
|
|
996
|
-
* To be consistent with the behaviour for merge, we'll also want to remove nullish values for "set" operations.
|
|
997
|
-
* On web, IndexedDB will keep the top-level keys along with a null value and this uses up storage and memory.
|
|
998
|
-
* This method will ensure that keys for null values are removed before an object is written to disk and cache so that all platforms are storing the data in the same efficient way.
|
|
999
|
-
* @private
|
|
1000
|
-
* @param {*} value
|
|
1001
|
-
* @returns {*}
|
|
1002
|
-
*/
|
|
1003
|
-
function removeNullObjectValues(value) {
|
|
1004
|
-
if (_.isArray(value) || !_.isObject(value)) {
|
|
1005
|
-
return value;
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
return _.omit(value, objectValue => _.isNull(objectValue));
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
994
|
/**
|
|
1012
995
|
* Write a value to our store with the given key
|
|
1013
996
|
*
|
|
@@ -1025,7 +1008,7 @@ function set(key, value) {
|
|
|
1025
1008
|
Logger.logAlert(`Onyx.set() called after Onyx.merge() for key: ${key}. It is recommended to use set() or merge() not both.`);
|
|
1026
1009
|
}
|
|
1027
1010
|
|
|
1028
|
-
const valueWithNullRemoved = removeNullObjectValues(value);
|
|
1011
|
+
const valueWithNullRemoved = Utils.removeNullObjectValues(value);
|
|
1029
1012
|
|
|
1030
1013
|
const hasChanged = cache.hasValueChanged(key, valueWithNullRemoved);
|
|
1031
1014
|
|
|
@@ -1144,14 +1127,14 @@ function merge(key, changes) {
|
|
|
1144
1127
|
delete mergeQueuePromise[key];
|
|
1145
1128
|
|
|
1146
1129
|
// After that we merge the batched changes with the existing value
|
|
1147
|
-
const modifiedData = removeNullObjectValues(applyMerge(existingValue, [batchedChanges]));
|
|
1130
|
+
const modifiedData = Utils.removeNullObjectValues(applyMerge(existingValue, [batchedChanges]));
|
|
1148
1131
|
|
|
1149
1132
|
// On native platforms we use SQLite which utilises JSON_PATCH to merge changes.
|
|
1150
1133
|
// JSON_PATCH generally removes top-level nullish values from the stored object.
|
|
1151
1134
|
// When there is no existing value though, SQLite will just insert the changes as a new value and thus the top-level nullish values won't be removed.
|
|
1152
1135
|
// Therefore we need to remove nullish values from the `batchedChanges` which are sent to the SQLite, if no existing value is present.
|
|
1153
1136
|
if (!existingValue) {
|
|
1154
|
-
batchedChanges = removeNullObjectValues(batchedChanges);
|
|
1137
|
+
batchedChanges = Utils.removeNullObjectValues(batchedChanges);
|
|
1155
1138
|
}
|
|
1156
1139
|
|
|
1157
1140
|
const hasChanged = cache.hasValueChanged(key, modifiedData);
|
package/lib/fastMerge.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import _ from 'underscore';
|
|
2
|
+
|
|
1
3
|
// Mostly copied from https://medium.com/@lubaka.a/how-to-remove-lodash-performance-improvement-b306669ad0e1
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -54,10 +56,7 @@ function mergeObject(target, source) {
|
|
|
54
56
|
* @returns {Object|Array}
|
|
55
57
|
*/
|
|
56
58
|
function fastMerge(target, source) {
|
|
57
|
-
|
|
58
|
-
// eslint-disable-next-line rulesdir/prefer-underscore-method
|
|
59
|
-
const array = Array.isArray(source);
|
|
60
|
-
if (array) {
|
|
59
|
+
if (_.isArray(source) || _.isNull(source) || _.isUndefined(source)) {
|
|
61
60
|
return source;
|
|
62
61
|
}
|
|
63
62
|
return mergeObject(target, source);
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
} from 'idb-keyval';
|
|
13
13
|
import _ from 'underscore';
|
|
14
14
|
import fastMerge from '../../fastMerge';
|
|
15
|
+
import Utils from '../../utils';
|
|
15
16
|
|
|
16
17
|
// We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
|
|
17
18
|
// which might not be available in certain environments that load the bundle (e.g. electron main process).
|
|
@@ -56,7 +57,7 @@ const provider = {
|
|
|
56
57
|
const upsertMany = _.map(pairs, ([key, value], index) => {
|
|
57
58
|
const prev = values[index];
|
|
58
59
|
const newValue = _.isObject(prev) ? fastMerge(prev, value) : value;
|
|
59
|
-
return promisifyRequest(store.put(newValue, key));
|
|
60
|
+
return promisifyRequest(store.put(Utils.removeNullObjectValues(newValue), key));
|
|
60
61
|
});
|
|
61
62
|
return Promise.all(upsertMany);
|
|
62
63
|
});
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import _ from 'underscore';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* We generally want to remove top-level nullish values from objects written to disk and cache, because it decreases the amount of data stored in memory and on disk.
|
|
5
|
+
* On native, when merging an existing value with new changes, SQLite will use JSON_PATCH, which removes top-level nullish values.
|
|
6
|
+
* To be consistent with the behaviour for merge, we'll also want to remove nullish values for "set" operations.
|
|
7
|
+
* On web, IndexedDB will keep the top-level keys along with a null value and this uses up storage and memory.
|
|
8
|
+
* This method will ensure that keys for null values are removed before an object is written to disk and cache so that all platforms are storing the data in the same efficient way.
|
|
9
|
+
* @private
|
|
10
|
+
* @param {*} value
|
|
11
|
+
* @returns {*}
|
|
12
|
+
*/
|
|
13
|
+
function removeNullObjectValues(value) {
|
|
14
|
+
if (_.isArray(value) || !_.isObject(value)) {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const objectWithoutNullObjectValues = _.omit(value, objectValue => _.isNull(objectValue));
|
|
19
|
+
|
|
20
|
+
return objectWithoutNullObjectValues;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default {removeNullObjectValues};
|