react-native-onyx 1.0.92 → 1.0.94

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/Onyx.js CHANGED
@@ -1103,14 +1103,12 @@ function multiSet(data) {
1103
1103
  function applyMerge(existingValue, changes) {
1104
1104
  const lastChange = _.last(changes);
1105
1105
 
1106
- if (_.isArray(existingValue) || _.isArray(lastChange)) {
1106
+ if (_.isArray(lastChange)) {
1107
1107
  return lastChange;
1108
1108
  }
1109
1109
 
1110
- if (_.isObject(existingValue) || _.every(changes, _.isObject)) {
1111
- // Object values are merged one after the other
1112
- // lodash adds a small overhead so we don't use it here
1113
- // eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
1110
+ if (_.some(changes, _.isObject)) {
1111
+ // Object values are then merged one after the other
1114
1112
  return _.reduce(changes, (modifiedData, change) => utils.fastMerge(modifiedData, change),
1115
1113
  existingValue || {});
1116
1114
  }
@@ -1141,6 +1139,12 @@ function applyMerge(existingValue, changes) {
1141
1139
  * @returns {Promise}
1142
1140
  */
1143
1141
  function merge(key, changes) {
1142
+ // Top-level undefined values are ignored
1143
+ // Therefore we need to prevent adding them to the merge queue
1144
+ if (_.isUndefined(changes)) {
1145
+ return mergeQueue[key] ? mergeQueuePromise[key] : Promise.resolve();
1146
+ }
1147
+
1144
1148
  // Merge attempts are batched together. The delta should be applied after a single call to get() to prevent a race condition.
1145
1149
  // Using the initial value from storage in subsequent merge attempts will lead to an incorrect final merged value.
1146
1150
  if (mergeQueue[key]) {
@@ -1155,12 +1159,21 @@ function merge(key, changes) {
1155
1159
  // We first only merge the changes, so we can provide these to the native implementation (SQLite uses only delta changes in "JSON_PATCH" to merge)
1156
1160
  let batchedChanges = applyMerge(undefined, mergeQueue[key]);
1157
1161
 
1162
+ if (_.isNull(batchedChanges)) {
1163
+ return remove(key);
1164
+ }
1165
+
1166
+ // The presence of a `null` in the merge queue instructs us to drop the existing value.
1167
+ // In this case, we can't simply merge the batched changes with the existing value, because then the null in the merge queue would have no effect
1168
+ const shouldOverwriteExistingValue = _.includes(mergeQueue[key], null);
1169
+
1158
1170
  // Clean up the write queue, so we don't apply these changes again
1159
1171
  delete mergeQueue[key];
1160
1172
  delete mergeQueuePromise[key];
1161
1173
 
1162
1174
  // After that we merge the batched changes with the existing value
1163
- const modifiedData = utils.removeNullObjectValues(applyMerge(existingValue, [batchedChanges]));
1175
+ const updatedValue = shouldOverwriteExistingValue ? batchedChanges : applyMerge(existingValue, [batchedChanges]);
1176
+ const modifiedData = utils.removeNullObjectValues(updatedValue);
1164
1177
 
1165
1178
  // On native platforms we use SQLite which utilises JSON_PATCH to merge changes.
1166
1179
  // JSON_PATCH generally removes top-level nullish values from the stored object.
package/lib/OnyxCache.js CHANGED
@@ -34,9 +34,11 @@ class OnyxCache {
34
34
  /**
35
35
  * @private
36
36
  * Captured pending tasks for already running storage methods
37
- * @type {Record<string, Promise>}
37
+ * Using a map yields better performance on operations such a delete
38
+ * https://www.zhenghao.io/posts/object-vs-map
39
+ * @type {Map<string, Promise>}
38
40
  */
39
- this.pendingPromises = {};
41
+ this.pendingPromises = new Map();
40
42
 
41
43
  // bind all public methods to prevent problems with `this`
42
44
  _.bindAll(
@@ -133,7 +135,7 @@ class OnyxCache {
133
135
  * @returns {*}
134
136
  */
135
137
  hasPendingTask(taskName) {
136
- return isDefined(this.pendingPromises[taskName]);
138
+ return isDefined(this.pendingPromises.get(taskName));
137
139
  }
138
140
 
139
141
  /**
@@ -145,7 +147,7 @@ class OnyxCache {
145
147
  * @returns {Promise<T>}
146
148
  */
147
149
  getTaskPromise(taskName) {
148
- return this.pendingPromises[taskName];
150
+ return this.pendingPromises.get(taskName);
149
151
  }
150
152
 
151
153
  /**
@@ -157,11 +159,13 @@ class OnyxCache {
157
159
  * @returns {Promise<T>}
158
160
  */
159
161
  captureTask(taskName, promise) {
160
- this.pendingPromises[taskName] = promise.finally(() => {
161
- delete this.pendingPromises[taskName];
162
+ const returnPromise = promise.finally(() => {
163
+ this.pendingPromises.delete(taskName);
162
164
  });
163
165
 
164
- return this.pendingPromises[taskName];
166
+ this.pendingPromises.set(taskName, returnPromise);
167
+
168
+ return returnPromise;
165
169
  }
166
170
 
167
171
  /**
package/lib/utils.js CHANGED
@@ -65,8 +65,9 @@ function mergeObject(target, source) {
65
65
  * @returns {Object|Array}
66
66
  */
67
67
  function fastMerge(target, source) {
68
- // lodash adds a small overhead so we don't use it here
69
- // eslint-disable-next-line rulesdir/prefer-underscore-method
68
+ // We have to ignore arrays and nullish values here,
69
+ // otherwise "mergeObject" will throw an error,
70
+ // because it expects an object as "source"
70
71
  if (_.isArray(source) || _.isNull(source) || _.isUndefined(source)) {
71
72
  return source;
72
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.92",
3
+ "version": "1.0.94",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",
package/lib/fastMerge.js DELETED
@@ -1,65 +0,0 @@
1
- import _ from 'underscore';
2
-
3
- // Mostly copied from https://medium.com/@lubaka.a/how-to-remove-lodash-performance-improvement-b306669ad0e1
4
-
5
- /**
6
- * @param {mixed} val
7
- * @returns {boolean}
8
- */
9
- function isMergeableObject(val) {
10
- const nonNullObject = val != null ? typeof val === 'object' : false;
11
- return (nonNullObject
12
- && Object.prototype.toString.call(val) !== '[object RegExp]'
13
- && Object.prototype.toString.call(val) !== '[object Date]');
14
- }
15
-
16
- /**
17
- * @param {Object} target
18
- * @param {Object} source
19
- * @returns {Object}
20
- */
21
- function mergeObject(target, source) {
22
- const destination = {};
23
- if (isMergeableObject(target)) {
24
- // lodash adds a small overhead so we don't use it here
25
- // eslint-disable-next-line rulesdir/prefer-underscore-method
26
- const targetKeys = Object.keys(target);
27
- for (let i = 0; i < targetKeys.length; ++i) {
28
- const key = targetKeys[i];
29
- destination[key] = target[key];
30
- }
31
- }
32
-
33
- // lodash adds a small overhead so we don't use it here
34
- // eslint-disable-next-line rulesdir/prefer-underscore-method
35
- const sourceKeys = Object.keys(source);
36
- for (let i = 0; i < sourceKeys.length; ++i) {
37
- const key = sourceKeys[i];
38
- if (source[key] === undefined) {
39
- // eslint-disable-next-line no-continue
40
- continue;
41
- }
42
- if (!isMergeableObject(source[key]) || !target[key]) {
43
- destination[key] = source[key];
44
- } else {
45
- // eslint-disable-next-line no-use-before-define
46
- destination[key] = fastMerge(target[key], source[key]);
47
- }
48
- }
49
-
50
- return destination;
51
- }
52
-
53
- /**
54
- * @param {Object|Array} target
55
- * @param {Object|Array} source
56
- * @returns {Object|Array}
57
- */
58
- function fastMerge(target, source) {
59
- if (_.isArray(source) || _.isNull(source) || _.isUndefined(source)) {
60
- return source;
61
- }
62
- return mergeObject(target, source);
63
- }
64
-
65
- export default fastMerge;