react-native-onyx 1.0.91 → 1.0.93

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.
@@ -1186,14 +1186,12 @@ function multiSet(data) {
1186
1186
  function applyMerge(existingValue, changes) {
1187
1187
  const lastChange = underscore__WEBPACK_IMPORTED_MODULE_1___default().last(changes);
1188
1188
 
1189
- if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isArray(existingValue) || underscore__WEBPACK_IMPORTED_MODULE_1___default().isArray(lastChange)) {
1189
+ if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isArray(lastChange)) {
1190
1190
  return lastChange;
1191
1191
  }
1192
1192
 
1193
- if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isObject(existingValue) || underscore__WEBPACK_IMPORTED_MODULE_1___default().every(changes, (underscore__WEBPACK_IMPORTED_MODULE_1___default().isObject))) {
1194
- // Object values are merged one after the other
1195
- // lodash adds a small overhead so we don't use it here
1196
- // eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
1193
+ if (underscore__WEBPACK_IMPORTED_MODULE_1___default().some(changes, (underscore__WEBPACK_IMPORTED_MODULE_1___default().isObject))) {
1194
+ // Object values are then merged one after the other
1197
1195
  return underscore__WEBPACK_IMPORTED_MODULE_1___default().reduce(changes, (modifiedData, change) => _utils__WEBPACK_IMPORTED_MODULE_9__["default"].fastMerge(modifiedData, change),
1198
1196
  existingValue || {});
1199
1197
  }
@@ -1224,6 +1222,12 @@ function applyMerge(existingValue, changes) {
1224
1222
  * @returns {Promise}
1225
1223
  */
1226
1224
  function merge(key, changes) {
1225
+ // Top-level undefined values are ignored
1226
+ // Therefore we need to prevent adding them to the merge queue
1227
+ if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isUndefined(changes)) {
1228
+ return mergeQueue[key] ? mergeQueuePromise[key] : Promise.resolve();
1229
+ }
1230
+
1227
1231
  // Merge attempts are batched together. The delta should be applied after a single call to get() to prevent a race condition.
1228
1232
  // Using the initial value from storage in subsequent merge attempts will lead to an incorrect final merged value.
1229
1233
  if (mergeQueue[key]) {
@@ -1238,12 +1242,21 @@ function merge(key, changes) {
1238
1242
  // 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)
1239
1243
  let batchedChanges = applyMerge(undefined, mergeQueue[key]);
1240
1244
 
1245
+ if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isNull(batchedChanges)) {
1246
+ return remove(key);
1247
+ }
1248
+
1249
+ // The presence of a `null` in the merge queue instructs us to drop the existing value.
1250
+ // 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
1251
+ const shouldOverwriteExistingValue = underscore__WEBPACK_IMPORTED_MODULE_1___default().includes(mergeQueue[key], null);
1252
+
1241
1253
  // Clean up the write queue, so we don't apply these changes again
1242
1254
  delete mergeQueue[key];
1243
1255
  delete mergeQueuePromise[key];
1244
1256
 
1245
1257
  // After that we merge the batched changes with the existing value
1246
- const modifiedData = _utils__WEBPACK_IMPORTED_MODULE_9__["default"].removeNullObjectValues(applyMerge(existingValue, [batchedChanges]));
1258
+ const updatedValue = shouldOverwriteExistingValue ? batchedChanges : applyMerge(existingValue, [batchedChanges]);
1259
+ const modifiedData = _utils__WEBPACK_IMPORTED_MODULE_9__["default"].removeNullObjectValues(updatedValue);
1247
1260
 
1248
1261
  // On native platforms we use SQLite which utilises JSON_PATCH to merge changes.
1249
1262
  // JSON_PATCH generally removes top-level nullish values from the stored object.
@@ -1851,12 +1864,21 @@ class OnyxCache {
1851
1864
  * Remove keys that don't fall into the range of recently used keys
1852
1865
  */
1853
1866
  removeLeastRecentlyUsedKeys() {
1854
- while (this.recentKeys.size > this.maxRecentKeysSize) {
1855
- const iterator = this.recentKeys.values();
1867
+ let numKeysToRemove = this.recentKeys.size - this.maxRecentKeysSize;
1868
+ if (numKeysToRemove <= 0) {
1869
+ return;
1870
+ }
1871
+ const iterator = this.recentKeys.values();
1872
+ const temp = [];
1873
+ while (numKeysToRemove > 0) {
1856
1874
  const value = iterator.next().value;
1857
- if (value !== undefined) {
1858
- this.drop(value);
1859
- }
1875
+ temp.push(value);
1876
+ numKeysToRemove--;
1877
+ }
1878
+
1879
+ for (let i = 0; i < temp.length; ++i) {
1880
+ delete this.storageMap[temp[i]];
1881
+ this.recentKeys.delete(temp[i]);
1860
1882
  }
1861
1883
  }
1862
1884
 
@@ -2431,8 +2453,9 @@ function mergeObject(target, source) {
2431
2453
  * @returns {Object|Array}
2432
2454
  */
2433
2455
  function fastMerge(target, source) {
2434
- // lodash adds a small overhead so we don't use it here
2435
- // eslint-disable-next-line rulesdir/prefer-underscore-method
2456
+ // We have to ignore arrays and nullish values here,
2457
+ // otherwise "mergeObject" will throw an error,
2458
+ // because it expects an object as "source"
2436
2459
  if (underscore__WEBPACK_IMPORTED_MODULE_0__.isArray(source) || underscore__WEBPACK_IMPORTED_MODULE_0__.isNull(source) || underscore__WEBPACK_IMPORTED_MODULE_0__.isUndefined(source)) {
2437
2460
  return source;
2438
2461
  }