react-native-onyx 1.0.18 → 1.0.19

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,5 @@
1
1
  import _ from 'underscore';
2
- import mergeWithCustomized from './mergeWithCustomized';
2
+ import fastMerge from './fastMerge';
3
3
 
4
4
  const isDefined = _.negate(_.isUndefined);
5
5
 
@@ -110,7 +110,9 @@ class OnyxCache {
110
110
  * @param {Record<string, *>} data - a map of (cache) key - values
111
111
  */
112
112
  merge(data) {
113
- this.storageMap = mergeWithCustomized({}, this.storageMap, data);
113
+ // lodash adds a small overhead so we don't use it here
114
+ // eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
115
+ this.storageMap = Object.assign({}, fastMerge(this.storageMap, data));
114
116
 
115
117
  const storageKeys = this.getAllKeys();
116
118
  const mergedKeys = _.keys(data);
@@ -0,0 +1,66 @@
1
+ // Mostly copied from https://medium.com/@lubaka.a/how-to-remove-lodash-performance-improvement-b306669ad0e1
2
+
3
+ /**
4
+ * @param {mixed} val
5
+ * @returns {boolean}
6
+ */
7
+ function isMergeableObject(val) {
8
+ const nonNullObject = val != null ? typeof val === 'object' : false;
9
+ return (nonNullObject
10
+ && Object.prototype.toString.call(val) !== '[object RegExp]'
11
+ && Object.prototype.toString.call(val) !== '[object Date]');
12
+ }
13
+
14
+ /**
15
+ * @param {Object} target
16
+ * @param {Object} source
17
+ * @returns {Object}
18
+ */
19
+ function mergeObject(target, source) {
20
+ const destination = {};
21
+ if (isMergeableObject(target)) {
22
+ // lodash adds a small overhead so we don't use it here
23
+ // eslint-disable-next-line rulesdir/prefer-underscore-method
24
+ const targetKeys = Object.keys(target);
25
+ for (let i = 0; i < targetKeys.length; ++i) {
26
+ const key = targetKeys[i];
27
+ destination[key] = target[key];
28
+ }
29
+ }
30
+
31
+ // lodash adds a small overhead so we don't use it here
32
+ // eslint-disable-next-line rulesdir/prefer-underscore-method
33
+ const sourceKeys = Object.keys(source);
34
+ for (let i = 0; i < sourceKeys.length; ++i) {
35
+ const key = sourceKeys[i];
36
+ if (source[key] === undefined) {
37
+ // eslint-disable-next-line no-continue
38
+ continue;
39
+ }
40
+ if (!isMergeableObject(source[key]) || !target[key]) {
41
+ destination[key] = source[key];
42
+ } else {
43
+ // eslint-disable-next-line no-use-before-define
44
+ destination[key] = fastMerge(target[key], source[key]);
45
+ }
46
+ }
47
+
48
+ return destination;
49
+ }
50
+
51
+ /**
52
+ * @param {Object|Array} target
53
+ * @param {Object|Array} source
54
+ * @returns {Object|Array}
55
+ */
56
+ function fastMerge(target, source) {
57
+ // lodash adds a small overhead so we don't use it here
58
+ // eslint-disable-next-line rulesdir/prefer-underscore-method
59
+ const array = Array.isArray(source);
60
+ if (array) {
61
+ return source;
62
+ }
63
+ return mergeObject(target, source);
64
+ }
65
+
66
+ export default fastMerge;
@@ -7,7 +7,7 @@
7
7
  import localforage from 'localforage';
8
8
  import _ from 'underscore';
9
9
  import SyncQueue from '../../SyncQueue';
10
- import mergeWithCustomized from '../../mergeWithCustomized';
10
+ import fastMerge from '../../fastMerge';
11
11
 
12
12
  localforage.config({
13
13
  name: 'OnyxDB',
@@ -24,7 +24,10 @@ const provider = {
24
24
  return localforage.getItem(key)
25
25
  .then((existingValue) => {
26
26
  const newValue = _.isObject(existingValue)
27
- ? mergeWithCustomized({}, existingValue, value)
27
+
28
+ // lodash adds a small overhead so we don't use it here
29
+ // eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
30
+ ? Object.assign({}, fastMerge(existingValue, value))
28
31
  : value;
29
32
  return localforage.setItem(key, newValue);
30
33
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",
@@ -1,26 +0,0 @@
1
- import lodashMergeWith from 'lodash/mergeWith';
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
- // eslint-disable-next-line rulesdir/prefer-underscore-method
17
- if (Array.isArray(objValue)) {
18
- return srcValue;
19
- }
20
- }
21
-
22
- function mergeWithCustomized(...args) {
23
- return lodashMergeWith(...args, customizerForMergeWith);
24
- }
25
-
26
- export default mergeWithCustomized;