Skip to content

Commit 475b1a9

Browse files
author
Justice Arthur
authored
Merge pull request #175 from Expensify/ionatan_changesmergewith
Address comments from review regarding mergeWith changes
2 parents 873e978 + d0c07c7 commit 475b1a9

7 files changed

Lines changed: 112 additions & 110 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The data will then be cached and stored via [`AsyncStorage`](https://github.com/
6363

6464
We can also use `Onyx.merge()` to merge new `Object` or `Array` data in with existing data.
6565

66-
For `Array` the default behavior is to concatenate replace it fully, effectively making it equivalent to set:
66+
For `Array` the default behavior is to replace it fully, effectively making it equivalent to set:
6767

6868
```javascript
6969
Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Joe']); // -> ['Joe']
@@ -77,7 +77,7 @@ Onyx.merge(ONYXKEYS.POLICY, {id: 1}); // -> {id: 1}
7777
Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'}
7878
```
7979

80-
Arrays inside objects will NOT be concatenated and instead will be replaced fully, same as arrays not inside objects:
80+
Arrays inside objects will be replaced fully, same as arrays not inside objects:
8181

8282
```javascript
8383
Onyx.merge(ONYXKEYS.POLICY, {employeeList: ['Joe', 'Jack']}); // -> {employeeList: ['Joe', 'Jack']}

lib/Onyx.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import _ from 'underscore';
22
import Str from 'expensify-common/lib/str';
33
import lodashMerge from 'lodash/merge';
4-
import lodashMergeWith from 'lodash/mergeWith';
54
import lodashGet from 'lodash/get';
65
import Storage from './storage';
76
import * as Logger from './Logger';
87
import cache from './OnyxCache';
98
import createDeferredTask from './createDeferredTask';
10-
import customizerForMergeWith from './customizerForMergeWith';
9+
import mergeWithCustomized from './mergeWithCustomized';
1110

1211
// Keeps track of the last connectionID that was used so we can keep incrementing it
1312
let lastConnectionID = 0;
@@ -663,7 +662,7 @@ function applyMerge(key, data) {
663662
if (_.isObject(data) || _.every(mergeValues, _.isObject)) {
664663
// Object values are merged one after the other
665664
return _.reduce(mergeValues, (modifiedData, mergeValue) => {
666-
const newData = lodashMergeWith({}, modifiedData, mergeValue, customizerForMergeWith);
665+
const newData = mergeWithCustomized({}, modifiedData, mergeValue);
667666

668667
// We will also delete any object keys that are undefined or null.
669668
// Deleting keys is not supported by AsyncStorage so we do it this way.
@@ -734,7 +733,7 @@ function initializeWithDefaultKeyStates() {
734733
.then((pairs) => {
735734
const asObject = _.object(pairs);
736735

737-
const merged = lodashMergeWith(asObject, defaultKeyStates, customizerForMergeWith);
736+
const merged = mergeWithCustomized(asObject, defaultKeyStates);
738737
cache.merge(merged);
739738
_.each(merged, (val, key) => keyChanged(key, val));
740739
});

lib/OnyxCache.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import _ from 'underscore';
2-
import lodashMergeWith from 'lodash/mergeWith';
3-
import customizerForMergeWith from './customizerForMergeWith';
2+
import mergeWithCustomized from './mergeWithCustomized';
43

54
const isDefined = _.negate(_.isUndefined);
65

@@ -111,7 +110,7 @@ class OnyxCache {
111110
* @param {Record<string, *>} data - a map of (cache) key - values
112111
*/
113112
merge(data) {
114-
this.storageMap = lodashMergeWith({}, this.storageMap, data, customizerForMergeWith);
113+
this.storageMap = mergeWithCustomized({}, this.storageMap, data);
115114

116115
const storageKeys = this.getAllKeys();
117116
const mergedKeys = _.keys(data);
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from 'underscore';
1+
import lodashMergeWith from 'lodash/mergeWith';
22

33
/**
44
* When merging 2 objects into onyx that contain an array, we want to completely replace the array instead of the default
@@ -13,9 +13,14 @@ import _ from 'underscore';
1313
*/
1414
// eslint-disable-next-line rulesdir/prefer-early-return
1515
function customizerForMergeWith(objValue, srcValue) {
16-
if (_.isArray(objValue)) {
16+
// eslint-disable-next-line rulesdir/prefer-underscore-method
17+
if (Array.isArray(objValue)) {
1718
return srcValue;
1819
}
1920
}
2021

21-
export default customizerForMergeWith;
22+
function mergeWithCustomized(...args) {
23+
return lodashMergeWith(...args, customizerForMergeWith);
24+
}
25+
26+
export default mergeWithCustomized;

lib/storage/providers/LocalForage.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
import localforage from 'localforage';
88
import _ from 'underscore';
9-
import lodashMergeWith from 'lodash/mergeWith';
109
import SyncQueue from '../../SyncQueue';
11-
import customizerForMergeWith from '../../customizerForMergeWith';
10+
import mergeWithCustomized from '../../mergeWithCustomized';
1211

1312
localforage.config({
1413
name: 'OnyxDB',
@@ -25,7 +24,7 @@ const provider = {
2524
return localforage.getItem(key)
2625
.then((existingValue) => {
2726
const newValue = _.isObject(existingValue)
28-
? lodashMergeWith({}, existingValue, value, customizerForMergeWith)
27+
? mergeWithCustomized({}, existingValue, value)
2928
: value;
3029
return localforage.setItem(key, newValue);
3130
});

0 commit comments

Comments
 (0)