react-native-onyx 1.0.13 → 1.0.16

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/lib/OnyxCache.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import _ from 'underscore';
2
- import lodashMerge from 'lodash/merge';
2
+ import lodashMergeWith from 'lodash/mergeWith';
3
+ import customizerForMergeWith from './customizerForMergeWith';
3
4
 
4
5
  const isDefined = _.negate(_.isUndefined);
5
6
 
@@ -110,7 +111,7 @@ class OnyxCache {
110
111
  * @param {Record<string, *>} data - a map of (cache) key - values
111
112
  */
112
113
  merge(data) {
113
- this.storageMap = lodashMerge({}, this.storageMap, data);
114
+ this.storageMap = lodashMergeWith({}, this.storageMap, data, customizerForMergeWith);
114
115
 
115
116
  const storageKeys = this.getAllKeys();
116
117
  const mergedKeys = _.keys(data);
@@ -0,0 +1,21 @@
1
+ import _ from 'underscore';
2
+
3
+ /**
4
+ * When merging 2 objects into onyx that contain an array, we want to completely replace the array instead of the default
5
+ * behavior which is to merge each item by its index.
6
+ * ie:
7
+ * merge({a: [1]}, {a: [2,3]}):
8
+ * - In the default implementation would produce {a:[1,3]}
9
+ * - With this function would produce: {a: [2,3]}
10
+ * @param {*} objValue
11
+ * @param {*} srcValue
12
+ * @return {*}
13
+ */
14
+ // eslint-disable-next-line rulesdir/prefer-early-return
15
+ function customizerForMergeWith(objValue, srcValue) {
16
+ if (_.isArray(objValue)) {
17
+ return srcValue;
18
+ }
19
+ }
20
+
21
+ export default customizerForMergeWith;
@@ -6,8 +6,9 @@
6
6
 
7
7
  import localforage from 'localforage';
8
8
  import _ from 'underscore';
9
- import lodashMerge from 'lodash/merge';
9
+ import lodashMergeWith from 'lodash/mergeWith';
10
10
  import SyncQueue from '../../SyncQueue';
11
+ import customizerForMergeWith from '../../customizerForMergeWith';
11
12
 
12
13
  localforage.config({
13
14
  name: 'OnyxDB',
@@ -24,7 +25,7 @@ const provider = {
24
25
  return localforage.getItem(key)
25
26
  .then((existingValue) => {
26
27
  const newValue = _.isObject(existingValue)
27
- ? lodashMerge({}, existingValue, value)
28
+ ? lodashMergeWith({}, existingValue, value, customizerForMergeWith)
28
29
  : value;
29
30
  return localforage.setItem(key, newValue);
30
31
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.13",
3
+ "version": "1.0.16",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",