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.
@@ -1505,6 +1505,42 @@ function mergeCollection(collectionKey, collection) {
1505
1505
  });
1506
1506
  }
1507
1507
 
1508
+ /**
1509
+ * Internal recursive function to execute the functions in the correct order
1510
+ *
1511
+ * @param {Array} data An array of objects with shape {onyxMethod: oneOf('set', 'merge', 'mergeCollection', 'multiSet', 'clear'), key: string, value: *}
1512
+ * @returns {Promise<Void>}
1513
+ */
1514
+ function innerUpdate(data) {
1515
+ if (data.length === 0) {
1516
+ return Promise.resolve();
1517
+ }
1518
+
1519
+ const { onyxMethod, key, value } = data.shift();
1520
+ let promise = Promise.resolve();
1521
+ switch (onyxMethod) {
1522
+ case METHOD.SET:
1523
+ promise = set(key, value);
1524
+ break;
1525
+ case METHOD.MERGE:
1526
+ promise = merge(key, value);
1527
+ break;
1528
+ case METHOD.MERGE_COLLECTION:
1529
+ promise = mergeCollection(key, value);
1530
+ break;
1531
+ case METHOD.MULTI_SET:
1532
+ promise = multiSet(value);
1533
+ break;
1534
+ case METHOD.CLEAR:
1535
+ promise = clear();
1536
+ break;
1537
+ default:
1538
+ break;}
1539
+
1540
+
1541
+ return promise.then(() => innerUpdate(data));
1542
+ }
1543
+
1508
1544
  /**
1509
1545
  * Insert API responses and lifecycle data into Onyx
1510
1546
  *
@@ -1513,8 +1549,9 @@ function mergeCollection(collectionKey, collection) {
1513
1549
  */
1514
1550
  function update(data) {
1515
1551
  // First, validate the Onyx object is in the format we expect
1552
+ const validMethods = [METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET];
1516
1553
  underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref3) => {let { onyxMethod, key, value } = _ref3;
1517
- if (!underscore__WEBPACK_IMPORTED_MODULE_1___default().contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET], onyxMethod)) {
1554
+ if (!underscore__WEBPACK_IMPORTED_MODULE_1___default().contains(validMethods, onyxMethod)) {
1518
1555
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
1519
1556
  }
1520
1557
  if (onyxMethod === METHOD.MULTI_SET) {
@@ -1527,32 +1564,10 @@ function update(data) {
1527
1564
  }
1528
1565
  });
1529
1566
 
1530
- const promises = [];
1531
- let clearPromise = Promise.resolve();
1532
-
1533
- underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref4) => {let { onyxMethod, key, value } = _ref4;
1534
- switch (onyxMethod) {
1535
- case METHOD.SET:
1536
- promises.push(() => set(key, value));
1537
- break;
1538
- case METHOD.MERGE:
1539
- promises.push(() => merge(key, value));
1540
- break;
1541
- case METHOD.MERGE_COLLECTION:
1542
- promises.push(() => mergeCollection(key, value));
1543
- break;
1544
- case METHOD.MULTI_SET:
1545
- promises.push(() => multiSet(value));
1546
- break;
1547
- case METHOD.CLEAR:
1548
- clearPromise = clear();
1549
- break;
1550
- default:
1551
- break;}
1552
-
1553
- });
1567
+ // Put clear operation on top
1568
+ data.sort((a) => a.onyxMethod === METHOD.CLEAR ? -1 : 1);
1554
1569
 
1555
- return clearPromise.then(() => Promise.all(underscore__WEBPACK_IMPORTED_MODULE_1___default().map(promises, (p) => p())));
1570
+ return innerUpdate(data);
1556
1571
  }
1557
1572
 
1558
1573
  /**