react-native-onyx 1.0.41 → 1.0.43

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
@@ -48,12 +48,13 @@ const deferredInitTask = createDeferredTask();
48
48
  * Uses a selector string or function to return a simplified version of sourceData
49
49
  * @param {Mixed} sourceData
50
50
  * @param {String|Function} selector
51
+ * @param {Object} [withOnyxInstanceState]
51
52
  * If it's a string, the selector is passed to lodashGet on the sourceData
52
53
  * If it's a function, it is passed the sourceData and it should return the simplified data
53
54
  * @returns {Mixed}
54
55
  */
55
- const getSubsetOfData = (sourceData, selector) => (_.isFunction(selector)
56
- ? selector(sourceData)
56
+ const getSubsetOfData = (sourceData, selector, withOnyxInstanceState) => (_.isFunction(selector)
57
+ ? selector(sourceData, withOnyxInstanceState)
57
58
  : lodashGet(sourceData, selector));
58
59
 
59
60
  /**
@@ -62,11 +63,12 @@ const getSubsetOfData = (sourceData, selector) => (_.isFunction(selector)
62
63
  * The resulting collection will only contain items that are returned by the selector.
63
64
  * @param {Object} collection
64
65
  * @param {String|Function} selector (see method docs for getSubsetOfData() for full details)
66
+ * @param {Object} [withOnyxInstanceState]
65
67
  * @returns {Object}
66
68
  */
67
- const reduceCollectionWithSelector = (collection, selector) => _.reduce(collection, (finalCollection, item, key) => {
69
+ const reduceCollectionWithSelector = (collection, selector, withOnyxInstanceState) => _.reduce(collection, (finalCollection, item, key) => {
68
70
  // eslint-disable-next-line no-param-reassign
69
- finalCollection[key] = getSubsetOfData(item, selector);
71
+ finalCollection[key] = getSubsetOfData(item, selector, withOnyxInstanceState);
70
72
 
71
73
  return finalCollection;
72
74
  }, {});
@@ -362,8 +364,8 @@ function keysChanged(collectionKey, partialCollection) {
362
364
  // returned by the selector.
363
365
  if (subscriber.selector) {
364
366
  subscriber.withOnyxInstance.setState((prevState) => {
365
- const previousData = reduceCollectionWithSelector(prevState[subscriber.statePropertyName], subscriber.selector);
366
- const newData = reduceCollectionWithSelector(cachedCollection, subscriber.selector);
367
+ const previousData = reduceCollectionWithSelector(prevState[subscriber.statePropertyName], subscriber.selector, subscriber.withOnyxInstance.state);
368
+ const newData = reduceCollectionWithSelector(cachedCollection, subscriber.selector, subscriber.withOnyxInstance.state);
367
369
 
368
370
  if (!deepEqual(previousData, newData)) {
369
371
  return {
@@ -406,7 +408,7 @@ function keysChanged(collectionKey, partialCollection) {
406
408
  if (subscriber.selector) {
407
409
  subscriber.withOnyxInstance.setState((prevState) => {
408
410
  const prevData = prevState[subscriber.statePropertyName];
409
- const newData = getSubsetOfData(cachedCollection[subscriber.key], subscriber.selector);
411
+ const newData = getSubsetOfData(cachedCollection[subscriber.key], subscriber.selector, subscriber.withOnyxInstance.state);
410
412
  if (!deepEqual(prevData, newData)) {
411
413
  PerformanceUtils.logSetStateCall(subscriber, prevData, newData, 'keysChanged', collectionKey);
412
414
  return {
@@ -488,11 +490,11 @@ function keyChanged(key, data, canUpdateSubscriber) {
488
490
  subscriber.withOnyxInstance.setState((prevState) => {
489
491
  const prevData = prevState[subscriber.statePropertyName];
490
492
  const newData = {
491
- [key]: getSubsetOfData(data, subscriber.selector),
493
+ [key]: getSubsetOfData(data, subscriber.selector, subscriber.withOnyxInstance.state),
492
494
  };
493
495
  const prevDataWithNewData = {
494
496
  ...prevData,
495
- [key]: getSubsetOfData(data, subscriber.selector),
497
+ [key]: getSubsetOfData(data, subscriber.selector, subscriber.withOnyxInstance.state),
496
498
  };
497
499
  if (!deepEqual(prevData, prevDataWithNewData)) {
498
500
  PerformanceUtils.logSetStateCall(subscriber, prevData, newData, 'keyChanged', key);
@@ -523,8 +525,8 @@ function keyChanged(key, data, canUpdateSubscriber) {
523
525
  // returned by the selector and only if the selected data has changed.
524
526
  if (subscriber.selector) {
525
527
  subscriber.withOnyxInstance.setState((prevState) => {
526
- const previousValue = getSubsetOfData(prevState[subscriber.statePropertyName], subscriber.selector);
527
- const newValue = getSubsetOfData(data, subscriber.selector);
528
+ const previousValue = getSubsetOfData(prevState[subscriber.statePropertyName], subscriber.selector, subscriber.withOnyxInstance.state);
529
+ const newValue = getSubsetOfData(data, subscriber.selector, subscriber.withOnyxInstance.state);
528
530
  if (!deepEqual(previousValue, newValue)) {
529
531
  return {
530
532
  [subscriber.statePropertyName]: newValue,
@@ -582,9 +584,9 @@ function sendDataToConnection(mapping, val, matchedKey) {
582
584
  // returned by the selector.
583
585
  if (mapping.selector) {
584
586
  if (isCollectionKey(mapping.key)) {
585
- newData = reduceCollectionWithSelector(val, mapping.selector);
587
+ newData = reduceCollectionWithSelector(val, mapping.selector, mapping.withOnyxInstance.state);
586
588
  } else {
587
- newData = getSubsetOfData(val, mapping.selector);
589
+ newData = getSubsetOfData(val, mapping.selector, mapping.withOnyxInstance.state);
588
590
  }
589
591
  }
590
592
 
@@ -662,9 +664,10 @@ function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
662
664
  * component
663
665
  * @param {Boolean} [mapping.waitForCollectionCallback] If set to true, it will return the entire collection to the callback as a single object
664
666
  * @param {String|Function} [mapping.selector] THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data.
665
- * If the selector is a string, the selector is passed to lodashGet on the sourceData. If the selector is a function, the sourceData is passed to the selector and should return the
666
- * simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component will only re-render when the subset of data changes.
667
- * Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint).
667
+ * If the selector is a string, the selector is passed to lodashGet on the sourceData. If the selector is a function, the sourceData and withOnyx state are
668
+ * passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component
669
+ * will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can
670
+ * be expensive from a performance standpoint).
668
671
  * @returns {Number} an ID to use when calling disconnect
669
672
  */
670
673
  function connect(mapping) {
@@ -1189,27 +1192,28 @@ function update(data) {
1189
1192
  });
1190
1193
 
1191
1194
  const promises = [];
1195
+ let clearPromise = Promise.resolve();
1192
1196
 
1193
1197
  _.each(data, ({onyxMethod, key, value}) => {
1194
1198
  switch (onyxMethod) {
1195
1199
  case METHOD.SET:
1196
- promises.push(set(key, value));
1200
+ promises.push(() => set(key, value));
1197
1201
  break;
1198
1202
  case METHOD.MERGE:
1199
- promises.push(merge(key, value));
1203
+ promises.push(() => merge(key, value));
1200
1204
  break;
1201
1205
  case METHOD.MERGE_COLLECTION:
1202
- promises.push(mergeCollection(key, value));
1206
+ promises.push(() => mergeCollection(key, value));
1203
1207
  break;
1204
1208
  case METHOD.CLEAR:
1205
- promises.push(clear());
1209
+ clearPromise = clear();
1206
1210
  break;
1207
1211
  default:
1208
1212
  break;
1209
1213
  }
1210
1214
  });
1211
1215
 
1212
- return Promise.all(promises);
1216
+ return clearPromise.then(() => Promise.all(_.map(promises, p => p())));
1213
1217
  }
1214
1218
 
1215
1219
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",