react-native-onyx 1.0.101 → 1.0.102

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
@@ -1422,6 +1422,42 @@ function mergeCollection(collectionKey, collection) {
1422
1422
  });
1423
1423
  }
1424
1424
 
1425
+ /**
1426
+ * Internal recursive function to execute the functions in the correct order
1427
+ *
1428
+ * @param {Array} data An array of objects with shape {onyxMethod: oneOf('set', 'merge', 'mergeCollection', 'multiSet', 'clear'), key: string, value: *}
1429
+ * @returns {Promise<Void>}
1430
+ */
1431
+ function innerUpdate(data) {
1432
+ if (data.length === 0) {
1433
+ return Promise.resolve();
1434
+ }
1435
+
1436
+ const {onyxMethod, key, value} = data.shift();
1437
+ let promise = Promise.resolve();
1438
+ switch (onyxMethod) {
1439
+ case METHOD.SET:
1440
+ promise = set(key, value);
1441
+ break;
1442
+ case METHOD.MERGE:
1443
+ promise = merge(key, value);
1444
+ break;
1445
+ case METHOD.MERGE_COLLECTION:
1446
+ promise = mergeCollection(key, value);
1447
+ break;
1448
+ case METHOD.MULTI_SET:
1449
+ promise = multiSet(value);
1450
+ break;
1451
+ case METHOD.CLEAR:
1452
+ promise = clear();
1453
+ break;
1454
+ default:
1455
+ break;
1456
+ }
1457
+
1458
+ return promise.then(() => innerUpdate(data));
1459
+ }
1460
+
1425
1461
  /**
1426
1462
  * Insert API responses and lifecycle data into Onyx
1427
1463
  *
@@ -1430,8 +1466,9 @@ function mergeCollection(collectionKey, collection) {
1430
1466
  */
1431
1467
  function update(data) {
1432
1468
  // First, validate the Onyx object is in the format we expect
1469
+ const validMethods = [METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET];
1433
1470
  _.each(data, ({onyxMethod, key, value}) => {
1434
- if (!_.contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET], onyxMethod)) {
1471
+ if (!_.contains(validMethods, onyxMethod)) {
1435
1472
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
1436
1473
  }
1437
1474
  if (onyxMethod === METHOD.MULTI_SET) {
@@ -1444,32 +1481,10 @@ function update(data) {
1444
1481
  }
1445
1482
  });
1446
1483
 
1447
- const promises = [];
1448
- let clearPromise = Promise.resolve();
1449
-
1450
- _.each(data, ({onyxMethod, key, value}) => {
1451
- switch (onyxMethod) {
1452
- case METHOD.SET:
1453
- promises.push(() => set(key, value));
1454
- break;
1455
- case METHOD.MERGE:
1456
- promises.push(() => merge(key, value));
1457
- break;
1458
- case METHOD.MERGE_COLLECTION:
1459
- promises.push(() => mergeCollection(key, value));
1460
- break;
1461
- case METHOD.MULTI_SET:
1462
- promises.push(() => multiSet(value));
1463
- break;
1464
- case METHOD.CLEAR:
1465
- clearPromise = clear();
1466
- break;
1467
- default:
1468
- break;
1469
- }
1470
- });
1484
+ // Put clear operation on top
1485
+ data.sort(a => (a.onyxMethod === METHOD.CLEAR ? -1 : 1));
1471
1486
 
1472
- return clearPromise.then(() => Promise.all(_.map(promises, p => p())));
1487
+ return innerUpdate(data);
1473
1488
  }
1474
1489
 
1475
1490
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.101",
3
+ "version": "1.0.102",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",