react-native-onyx 1.0.103 → 1.0.105

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,42 +1505,6 @@ 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
-
1544
1508
  /**
1545
1509
  * Insert API responses and lifecycle data into Onyx
1546
1510
  *
@@ -1549,9 +1513,8 @@ function innerUpdate(data) {
1549
1513
  */
1550
1514
  function update(data) {
1551
1515
  // 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];
1553
1516
  underscore__WEBPACK_IMPORTED_MODULE_1___default().each(data, (_ref3) => {let { onyxMethod, key, value } = _ref3;
1554
- if (!underscore__WEBPACK_IMPORTED_MODULE_1___default().contains(validMethods, onyxMethod)) {
1517
+ if (!underscore__WEBPACK_IMPORTED_MODULE_1___default().contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET], onyxMethod)) {
1555
1518
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
1556
1519
  }
1557
1520
  if (onyxMethod === METHOD.MULTI_SET) {
@@ -1564,10 +1527,32 @@ function update(data) {
1564
1527
  }
1565
1528
  });
1566
1529
 
1567
- // Put clear operation on top
1568
- data.sort((a) => a.onyxMethod === METHOD.CLEAR ? -1 : 1);
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
+ });
1569
1554
 
1570
- return innerUpdate(data);
1555
+ return clearPromise.then(() => Promise.all(underscore__WEBPACK_IMPORTED_MODULE_1___default().map(promises, (p) => p())));
1571
1556
  }
1572
1557
 
1573
1558
  /**
@@ -2601,6 +2586,15 @@ function getDisplayName(component) {
2601
2586
  return component.displayName || component.name || 'Component';
2602
2587
  }
2603
2588
 
2589
+ /**
2590
+ * Removes all the keys from state that are unrelated to the onyx data being mapped to the component.
2591
+ *
2592
+ * @param {Object} state of the component
2593
+ * @param {Object} onyxToStateMapping the object holding all of the mapping configuration for the component
2594
+ * @returns {Object}
2595
+ */
2596
+ const getOnyxDataFromState = (state, onyxToStateMapping) => underscore__WEBPACK_IMPORTED_MODULE_2___default().pick(state, underscore__WEBPACK_IMPORTED_MODULE_2___default().keys(onyxToStateMapping));
2597
+
2604
2598
  /* harmony default export */ function __WEBPACK_DEFAULT_EXPORT__(mapOnyxToState) {let shouldDelayUpdates = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
2605
2599
  // A list of keys that must be present in tempState before we can render the WrappedComponent
2606
2600
  const requiredKeysForInit = underscore__WEBPACK_IMPORTED_MODULE_2___default().chain(mapOnyxToState).
@@ -2673,16 +2667,20 @@ function getDisplayName(component) {
2673
2667
  this.checkEvictableKeys();
2674
2668
  }
2675
2669
 
2676
- componentDidUpdate(prevProps) {
2670
+ componentDidUpdate() {
2671
+ // When the state is passed to the key functions with Str.result(), omit anything
2672
+ // from state that was not part of the mapped keys.
2673
+ const onyxDataFromState = getOnyxDataFromState(this.state, mapOnyxToState);
2674
+
2677
2675
  // If any of the mappings use data from the props, then when the props change, all the
2678
2676
  // connections need to be reconnected with the new props
2679
- underscore__WEBPACK_IMPORTED_MODULE_2___default().each(mapOnyxToState, (mapping, propertyName) => {
2680
- const previousKey = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, prevProps);
2681
- const newKey = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, this.props);
2677
+ underscore__WEBPACK_IMPORTED_MODULE_2___default().each(mapOnyxToState, (mapping, propName) => {
2678
+ const previousKey = mapping.previousKey;
2679
+ const newKey = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, { ...this.props, ...onyxDataFromState });
2682
2680
  if (previousKey !== newKey) {
2683
2681
  _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].disconnect(this.activeConnectionIDs[previousKey], previousKey);
2684
2682
  delete this.activeConnectionIDs[previousKey];
2685
- this.connectMappingToOnyx(mapping, propertyName);
2683
+ this.connectMappingToOnyx(mapping, propName);
2686
2684
  }
2687
2685
  });
2688
2686
  this.checkEvictableKeys();
@@ -2691,9 +2689,8 @@ function getDisplayName(component) {
2691
2689
  componentWillUnmount() {
2692
2690
  // Disconnect everything from Onyx
2693
2691
  underscore__WEBPACK_IMPORTED_MODULE_2___default().each(mapOnyxToState, (mapping) => {
2694
- const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, this.props);
2695
- const connectionID = this.activeConnectionIDs[key];
2696
- _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].disconnect(connectionID, key);
2692
+ const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, { ...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState) });
2693
+ _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].disconnect(this.activeConnectionIDs[key], key);
2697
2694
  });
2698
2695
  }
2699
2696
 
@@ -2825,7 +2822,14 @@ function getDisplayName(component) {
2825
2822
  * component
2826
2823
  */
2827
2824
  connectMappingToOnyx(mapping, statePropertyName) {
2828
- const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, this.props);
2825
+ const key = _Str__WEBPACK_IMPORTED_MODULE_3__.result(mapping.key, { ...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState) });
2826
+
2827
+ // Remember the previous key so that if it ever changes, the component will reconnect to Onyx
2828
+ // in componentDidUpdate
2829
+ if (statePropertyName !== 'initialValue' && mapOnyxToState[statePropertyName]) {
2830
+ // eslint-disable-next-line no-param-reassign
2831
+ mapOnyxToState[statePropertyName].previousKey = key;
2832
+ }
2829
2833
 
2830
2834
  // eslint-disable-next-line rulesdir/prefer-onyx-connect-in-libs
2831
2835
  this.activeConnectionIDs[key] = _Onyx__WEBPACK_IMPORTED_MODULE_4__["default"].connect({