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.
package/lib/Onyx.js CHANGED
@@ -1422,42 +1422,6 @@ 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
-
1461
1425
  /**
1462
1426
  * Insert API responses and lifecycle data into Onyx
1463
1427
  *
@@ -1466,9 +1430,8 @@ function innerUpdate(data) {
1466
1430
  */
1467
1431
  function update(data) {
1468
1432
  // 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];
1470
1433
  _.each(data, ({onyxMethod, key, value}) => {
1471
- if (!_.contains(validMethods, onyxMethod)) {
1434
+ if (!_.contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION, METHOD.MULTI_SET], onyxMethod)) {
1472
1435
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
1473
1436
  }
1474
1437
  if (onyxMethod === METHOD.MULTI_SET) {
@@ -1481,10 +1444,32 @@ function update(data) {
1481
1444
  }
1482
1445
  });
1483
1446
 
1484
- // Put clear operation on top
1485
- data.sort(a => (a.onyxMethod === METHOD.CLEAR ? -1 : 1));
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
+ });
1486
1471
 
1487
- return innerUpdate(data);
1472
+ return clearPromise.then(() => Promise.all(_.map(promises, p => p())));
1488
1473
  }
1489
1474
 
1490
1475
  /**
package/lib/withOnyx.js CHANGED
@@ -20,6 +20,15 @@ function getDisplayName(component) {
20
20
  return component.displayName || component.name || 'Component';
21
21
  }
22
22
 
23
+ /**
24
+ * Removes all the keys from state that are unrelated to the onyx data being mapped to the component.
25
+ *
26
+ * @param {Object} state of the component
27
+ * @param {Object} onyxToStateMapping the object holding all of the mapping configuration for the component
28
+ * @returns {Object}
29
+ */
30
+ const getOnyxDataFromState = (state, onyxToStateMapping) => _.pick(state, _.keys(onyxToStateMapping));
31
+
23
32
  export default function (mapOnyxToState, shouldDelayUpdates = false) {
24
33
  // A list of keys that must be present in tempState before we can render the WrappedComponent
25
34
  const requiredKeysForInit = _.chain(mapOnyxToState)
@@ -92,16 +101,20 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
92
101
  this.checkEvictableKeys();
93
102
  }
94
103
 
95
- componentDidUpdate(prevProps) {
104
+ componentDidUpdate() {
105
+ // When the state is passed to the key functions with Str.result(), omit anything
106
+ // from state that was not part of the mapped keys.
107
+ const onyxDataFromState = getOnyxDataFromState(this.state, mapOnyxToState);
108
+
96
109
  // If any of the mappings use data from the props, then when the props change, all the
97
110
  // connections need to be reconnected with the new props
98
- _.each(mapOnyxToState, (mapping, propertyName) => {
99
- const previousKey = Str.result(mapping.key, prevProps);
100
- const newKey = Str.result(mapping.key, this.props);
111
+ _.each(mapOnyxToState, (mapping, propName) => {
112
+ const previousKey = mapping.previousKey;
113
+ const newKey = Str.result(mapping.key, {...this.props, ...onyxDataFromState});
101
114
  if (previousKey !== newKey) {
102
115
  Onyx.disconnect(this.activeConnectionIDs[previousKey], previousKey);
103
116
  delete this.activeConnectionIDs[previousKey];
104
- this.connectMappingToOnyx(mapping, propertyName);
117
+ this.connectMappingToOnyx(mapping, propName);
105
118
  }
106
119
  });
107
120
  this.checkEvictableKeys();
@@ -110,9 +123,8 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
110
123
  componentWillUnmount() {
111
124
  // Disconnect everything from Onyx
112
125
  _.each(mapOnyxToState, (mapping) => {
113
- const key = Str.result(mapping.key, this.props);
114
- const connectionID = this.activeConnectionIDs[key];
115
- Onyx.disconnect(connectionID, key);
126
+ const key = Str.result(mapping.key, {...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState)});
127
+ Onyx.disconnect(this.activeConnectionIDs[key], key);
116
128
  });
117
129
  }
118
130
 
@@ -244,7 +256,14 @@ export default function (mapOnyxToState, shouldDelayUpdates = false) {
244
256
  * component
245
257
  */
246
258
  connectMappingToOnyx(mapping, statePropertyName) {
247
- const key = Str.result(mapping.key, this.props);
259
+ const key = Str.result(mapping.key, {...this.props, ...getOnyxDataFromState(this.state, mapOnyxToState)});
260
+
261
+ // Remember the previous key so that if it ever changes, the component will reconnect to Onyx
262
+ // in componentDidUpdate
263
+ if (statePropertyName !== 'initialValue' && mapOnyxToState[statePropertyName]) {
264
+ // eslint-disable-next-line no-param-reassign
265
+ mapOnyxToState[statePropertyName].previousKey = key;
266
+ }
248
267
 
249
268
  // eslint-disable-next-line rulesdir/prefer-onyx-connect-in-libs
250
269
  this.activeConnectionIDs[key] = Onyx.connect({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.103",
3
+ "version": "1.0.105",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",