react-native-onyx 1.0.96 → 1.0.98

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.
@@ -1124,13 +1124,7 @@ function set(key, value) {
1124
1124
  _Logger__WEBPACK_IMPORTED_MODULE_6__.logAlert(`Onyx.set() called after Onyx.merge() for key: ${key}. It is recommended to use set() or merge() not both.`);
1125
1125
  }
1126
1126
 
1127
- // We can remove all null values in an object by merging it with itself
1128
- // utils.fastMerge recursively goes through the object and removes all null values
1129
- // Passing two identical objects as source and target to fastMerge will not change it, but only remove the null values
1130
- let valueWithNullRemoved = value;
1131
- if (typeof value === 'object' && !underscore__WEBPACK_IMPORTED_MODULE_1___default().isArray(value)) {
1132
- valueWithNullRemoved = _utils__WEBPACK_IMPORTED_MODULE_9__["default"].fastMerge(value, value);
1133
- }
1127
+ const valueWithNullRemoved = _utils__WEBPACK_IMPORTED_MODULE_9__["default"].removeNullObjectValues(value);
1134
1128
 
1135
1129
  const hasChanged = _OnyxCache__WEBPACK_IMPORTED_MODULE_4__["default"].hasValueChanged(key, valueWithNullRemoved);
1136
1130
 
@@ -1187,10 +1181,9 @@ function multiSet(data) {
1187
1181
  * @private
1188
1182
  * @param {*} existingValue
1189
1183
  * @param {Array<*>} changes Array of changes that should be applied to the existing value
1190
- * @param {Boolean} shouldRemoveNullObjectValues
1191
1184
  * @returns {*}
1192
1185
  */
1193
- function applyMerge(existingValue, changes, shouldRemoveNullObjectValues) {
1186
+ function applyMerge(existingValue, changes) {
1194
1187
  const lastChange = underscore__WEBPACK_IMPORTED_MODULE_1___default().last(changes);
1195
1188
 
1196
1189
  if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isArray(lastChange)) {
@@ -1199,7 +1192,7 @@ function applyMerge(existingValue, changes, shouldRemoveNullObjectValues) {
1199
1192
 
1200
1193
  if (underscore__WEBPACK_IMPORTED_MODULE_1___default().some(changes, (underscore__WEBPACK_IMPORTED_MODULE_1___default().isObject))) {
1201
1194
  // Object values are then merged one after the other
1202
- return underscore__WEBPACK_IMPORTED_MODULE_1___default().reduce(changes, (modifiedData, change) => _utils__WEBPACK_IMPORTED_MODULE_9__["default"].fastMerge(modifiedData, change, shouldRemoveNullObjectValues),
1195
+ return underscore__WEBPACK_IMPORTED_MODULE_1___default().reduce(changes, (modifiedData, change) => _utils__WEBPACK_IMPORTED_MODULE_9__["default"].fastMerge(modifiedData, change),
1203
1196
  existingValue || {});
1204
1197
  }
1205
1198
 
@@ -1247,8 +1240,7 @@ function merge(key, changes) {
1247
1240
  then((existingValue) => {
1248
1241
  try {
1249
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)
1250
- // We don't want to remove null values from the "batchedChanges", because SQLite uses them to remove keys from storage natively.
1251
- let batchedChanges = applyMerge(undefined, mergeQueue[key], false);
1243
+ let batchedChanges = applyMerge(undefined, mergeQueue[key]);
1252
1244
 
1253
1245
  if (underscore__WEBPACK_IMPORTED_MODULE_1___default().isNull(batchedChanges)) {
1254
1246
  return remove(key);
@@ -1263,16 +1255,15 @@ function merge(key, changes) {
1263
1255
  delete mergeQueuePromise[key];
1264
1256
 
1265
1257
  // After that we merge the batched changes with the existing value
1266
- // We can remove null values from the "modifiedData", because "null" implicates that the user wants to remove a value from storage.
1267
- // The "modifiedData" will be directly "set" in storage instead of being merged
1268
- const modifiedData = shouldOverwriteExistingValue ? batchedChanges : applyMerge(existingValue, [batchedChanges], true);
1258
+ const updatedValue = shouldOverwriteExistingValue ? batchedChanges : applyMerge(existingValue, [batchedChanges]);
1259
+ const modifiedData = _utils__WEBPACK_IMPORTED_MODULE_9__["default"].removeNullObjectValues(updatedValue);
1269
1260
 
1270
1261
  // On native platforms we use SQLite which utilises JSON_PATCH to merge changes.
1271
1262
  // JSON_PATCH generally removes top-level nullish values from the stored object.
1272
- // When there is no existing value though, SQLite will just insert the changes as a new value and thus the null values won't be removed.
1273
- // Therefore we need to remove null values from the `batchedChanges` which are sent to the SQLite, if no existing value is present.
1263
+ // When there is no existing value though, SQLite will just insert the changes as a new value and thus the top-level nullish values won't be removed.
1264
+ // Therefore we need to remove nullish values from the `batchedChanges` which are sent to the SQLite, if no existing value is present.
1274
1265
  if (!existingValue) {
1275
- batchedChanges = applyMerge(undefined, mergeQueue[key], true);
1266
+ batchedChanges = _utils__WEBPACK_IMPORTED_MODULE_9__["default"].removeNullObjectValues(batchedChanges);
1276
1267
  }
1277
1268
 
1278
1269
  const hasChanged = _OnyxCache__WEBPACK_IMPORTED_MODULE_4__["default"].hasValueChanged(key, modifiedData);
@@ -2300,7 +2291,7 @@ const provider = {
2300
2291
  const upsertMany = underscore__WEBPACK_IMPORTED_MODULE_1___default().map(pairs, (_ref2, index) => {let [key, value] = _ref2;
2301
2292
  const prev = values[index];
2302
2293
  const newValue = underscore__WEBPACK_IMPORTED_MODULE_1___default().isObject(prev) ? _utils__WEBPACK_IMPORTED_MODULE_2__["default"].fastMerge(prev, value) : value;
2303
- return (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.promisifyRequest)(store.put(newValue, key));
2294
+ return (0,idb_keyval__WEBPACK_IMPORTED_MODULE_0__.promisifyRequest)(store.put(_utils__WEBPACK_IMPORTED_MODULE_2__["default"].removeNullObjectValues(newValue), key));
2304
2295
  });
2305
2296
  return Promise.all(upsertMany);
2306
2297
  });
@@ -2405,8 +2396,8 @@ function areObjectsEmpty(a, b) {
2405
2396
  return (
2406
2397
  typeof a === 'object' &&
2407
2398
  typeof b === 'object' &&
2408
- underscore__WEBPACK_IMPORTED_MODULE_0___default().isEmpty(a) &&
2409
- underscore__WEBPACK_IMPORTED_MODULE_0___default().isEmpty(b));
2399
+ underscore__WEBPACK_IMPORTED_MODULE_0__.isEmpty(a) &&
2400
+ underscore__WEBPACK_IMPORTED_MODULE_0__.isEmpty(b));
2410
2401
 
2411
2402
  }
2412
2403
 
@@ -2426,12 +2417,9 @@ function isMergeableObject(val) {
2426
2417
  /**
2427
2418
  * @param {Object} target
2428
2419
  * @param {Object} source
2429
- * @param {Boolean} shouldRemoveNullObjectValues
2430
2420
  * @returns {Object}
2431
2421
  */
2432
- function mergeObject(target, source) {let shouldRemoveNullObjectValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
2433
- const targetAndSourceIdentical = target === source;
2434
-
2422
+ function mergeObject(target, source) {
2435
2423
  const destination = {};
2436
2424
  if (isMergeableObject(target)) {
2437
2425
  // lodash adds a small overhead so we don't use it here
@@ -2439,13 +2427,6 @@ function mergeObject(target, source) {let shouldRemoveNullObjectValues = argumen
2439
2427
  const targetKeys = Object.keys(target);
2440
2428
  for (let i = 0; i < targetKeys.length; ++i) {
2441
2429
  const key = targetKeys[i];
2442
-
2443
- // If shouldRemoveNullObjectValues is true, we want to remove null values from the merged object
2444
- if (shouldRemoveNullObjectValues && (target[key] === null || source[key] === null)) {
2445
- // eslint-disable-next-line no-continue
2446
- continue;
2447
- }
2448
-
2449
2430
  destination[key] = target[key];
2450
2431
  }
2451
2432
  }
@@ -2455,22 +2436,15 @@ function mergeObject(target, source) {let shouldRemoveNullObjectValues = argumen
2455
2436
  const sourceKeys = Object.keys(source);
2456
2437
  for (let i = 0; i < sourceKeys.length; ++i) {
2457
2438
  const key = sourceKeys[i];
2458
-
2459
- // If shouldRemoveNullObjectValues is true, we want to remove null values from the merged object
2460
- if (shouldRemoveNullObjectValues && source[key] === null) {
2439
+ if (source[key] === undefined) {
2461
2440
  // eslint-disable-next-line no-continue
2462
2441
  continue;
2463
2442
  }
2464
-
2465
2443
  if (!isMergeableObject(source[key]) || !target[key]) {
2466
- if (targetAndSourceIdentical) {
2467
- // eslint-disable-next-line no-continue
2468
- continue;
2469
- }
2470
2444
  destination[key] = source[key];
2471
2445
  } else {
2472
2446
  // eslint-disable-next-line no-use-before-define
2473
- destination[key] = fastMerge(target[key], source[key], shouldRemoveNullObjectValues);
2447
+ destination[key] = fastMerge(target[key], source[key]);
2474
2448
  }
2475
2449
  }
2476
2450
 
@@ -2478,28 +2452,41 @@ function mergeObject(target, source) {let shouldRemoveNullObjectValues = argumen
2478
2452
  }
2479
2453
 
2480
2454
  /**
2481
- * Merges two objects and removes null values if "shouldRemoveNullObjectValues" is set to true
2482
- *
2483
- * We generally want to remove null values from objects written to disk and cache, because it decreases the amount of data stored in memory and on disk.
2484
- * On native, when merging an existing value with new changes, SQLite will use JSON_PATCH, which removes top-level nullish values.
2485
- * To be consistent with the behaviour for merge, we'll also want to remove null values for "set" operations.
2486
- *
2487
2455
  * @param {Object|Array} target
2488
2456
  * @param {Object|Array} source
2489
- * @param {Boolean} shouldRemoveNullObjectValues
2490
2457
  * @returns {Object|Array}
2491
2458
  */
2492
- function fastMerge(target, source) {let shouldRemoveNullObjectValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
2459
+ function fastMerge(target, source) {
2493
2460
  // We have to ignore arrays and nullish values here,
2494
2461
  // otherwise "mergeObject" will throw an error,
2495
2462
  // because it expects an object as "source"
2496
- if (underscore__WEBPACK_IMPORTED_MODULE_0___default().isArray(source) || source === null || source === undefined) {
2463
+ if (underscore__WEBPACK_IMPORTED_MODULE_0__.isArray(source) || underscore__WEBPACK_IMPORTED_MODULE_0__.isNull(source) || underscore__WEBPACK_IMPORTED_MODULE_0__.isUndefined(source)) {
2497
2464
  return source;
2498
2465
  }
2499
- return mergeObject(target, source, shouldRemoveNullObjectValues);
2466
+ return mergeObject(target, source);
2467
+ }
2468
+
2469
+ /**
2470
+ * We generally want to remove top-level nullish values from objects written to disk and cache, because it decreases the amount of data stored in memory and on disk.
2471
+ * On native, when merging an existing value with new changes, SQLite will use JSON_PATCH, which removes top-level nullish values.
2472
+ * To be consistent with the behaviour for merge, we'll also want to remove nullish values for "set" operations.
2473
+ * On web, IndexedDB will keep the top-level keys along with a null value and this uses up storage and memory.
2474
+ * This method will ensure that keys for null values are removed before an object is written to disk and cache so that all platforms are storing the data in the same efficient way.
2475
+ * @private
2476
+ * @param {*} value
2477
+ * @returns {*}
2478
+ */
2479
+ function removeNullObjectValues(value) {
2480
+ if (underscore__WEBPACK_IMPORTED_MODULE_0__.isArray(value) || !underscore__WEBPACK_IMPORTED_MODULE_0__.isObject(value)) {
2481
+ return value;
2482
+ }
2483
+
2484
+ const objectWithoutNullObjectValues = underscore__WEBPACK_IMPORTED_MODULE_0__.omit(value, (objectValue) => underscore__WEBPACK_IMPORTED_MODULE_0__.isNull(objectValue));
2485
+
2486
+ return objectWithoutNullObjectValues;
2500
2487
  }
2501
2488
 
2502
- /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({ areObjectsEmpty, fastMerge });
2489
+ /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({ removeNullObjectValues, areObjectsEmpty, fastMerge });
2503
2490
 
2504
2491
  /***/ }),
2505
2492