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/API.md CHANGED
@@ -5,10 +5,10 @@
5
5
  ## Functions
6
6
 
7
7
  <dl>
8
- <dt><a href="#getSubsetOfData">getSubsetOfData(sourceData, selector)</a> ⇒ <code>Mixed</code></dt>
8
+ <dt><a href="#getSubsetOfData">getSubsetOfData(sourceData, selector, [withOnyxInstanceState])</a> ⇒ <code>Mixed</code></dt>
9
9
  <dd><p>Uses a selector string or function to return a simplified version of sourceData</p>
10
10
  </dd>
11
- <dt><a href="#reduceCollectionWithSelector">reduceCollectionWithSelector(collection, selector)</a> ⇒ <code>Object</code></dt>
11
+ <dt><a href="#reduceCollectionWithSelector">reduceCollectionWithSelector(collection, selector, [withOnyxInstanceState])</a> ⇒ <code>Object</code></dt>
12
12
  <dd><p>Takes a collection of items (eg. {testKey_1:{a:&#39;a&#39;}, testKey_2:{b:&#39;b&#39;}})
13
13
  and runs it through a reducer function to return a subset of the data according to a selector.
14
14
  The resulting collection will only contain items that are returned by the selector.</p>
@@ -77,7 +77,7 @@ value will be saved to storage after the default value.</p>
77
77
 
78
78
  <a name="getSubsetOfData"></a>
79
79
 
80
- ## getSubsetOfData(sourceData, selector) ⇒ <code>Mixed</code>
80
+ ## getSubsetOfData(sourceData, selector, [withOnyxInstanceState]) ⇒ <code>Mixed</code>
81
81
  Uses a selector string or function to return a simplified version of sourceData
82
82
 
83
83
  **Kind**: global function
@@ -85,11 +85,12 @@ Uses a selector string or function to return a simplified version of sourceData
85
85
  | Param | Type | Description |
86
86
  | --- | --- | --- |
87
87
  | sourceData | <code>Mixed</code> | |
88
- | selector | <code>String</code> \| <code>function</code> | If it's a string, the selector is passed to lodashGet on the sourceData If it's a function, it is passed the sourceData and it should return the simplified data |
88
+ | selector | <code>String</code> \| <code>function</code> | |
89
+ | [withOnyxInstanceState] | <code>Object</code> | If it's a string, the selector is passed to lodashGet on the sourceData If it's a function, it is passed the sourceData and it should return the simplified data |
89
90
 
90
91
  <a name="reduceCollectionWithSelector"></a>
91
92
 
92
- ## reduceCollectionWithSelector(collection, selector) ⇒ <code>Object</code>
93
+ ## reduceCollectionWithSelector(collection, selector, [withOnyxInstanceState]) ⇒ <code>Object</code>
93
94
  Takes a collection of items (eg. {testKey_1:{a:'a'}, testKey_2:{b:'b'}})
94
95
  and runs it through a reducer function to return a subset of the data according to a selector.
95
96
  The resulting collection will only contain items that are returned by the selector.
@@ -100,6 +101,7 @@ The resulting collection will only contain items that are returned by the select
100
101
  | --- | --- | --- |
101
102
  | collection | <code>Object</code> | |
102
103
  | selector | <code>String</code> \| <code>function</code> | (see method docs for getSubsetOfData() for full details) |
104
+ | [withOnyxInstanceState] | <code>Object</code> | |
103
105
 
104
106
  <a name="isCollectionMemberKey"></a>
105
107
 
@@ -128,7 +130,7 @@ Subscribes a react component's state directly to a store key
128
130
  | [mapping.callback] | <code>function</code> | a method that will be called with changed data This is used by any non-React code to connect to Onyx |
129
131
  | [mapping.initWithStoredValues] | <code>Boolean</code> | If set to false, then no data will be prefilled into the component |
130
132
  | [mapping.waitForCollectionCallback] | <code>Boolean</code> | If set to true, it will return the entire collection to the callback as a single object |
131
- | [mapping.selector] | <code>String</code> \| <code>function</code> | THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data. 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 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. 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). |
133
+ | [mapping.selector] | <code>String</code> \| <code>function</code> | THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data. 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 passed to the selector and should return the 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. 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). |
132
134
 
133
135
  **Example**
134
136
  ```js
@@ -133,12 +133,13 @@ const deferredInitTask = (0,_createDeferredTask__WEBPACK_IMPORTED_MODULE_4__["de
133
133
  * Uses a selector string or function to return a simplified version of sourceData
134
134
  * @param {Mixed} sourceData
135
135
  * @param {String|Function} selector
136
+ * @param {Object} [withOnyxInstanceState]
136
137
  * If it's a string, the selector is passed to lodashGet on the sourceData
137
138
  * If it's a function, it is passed the sourceData and it should return the simplified data
138
139
  * @returns {Mixed}
139
140
  */
140
- const getSubsetOfData = (sourceData, selector) => underscore__WEBPACK_IMPORTED_MODULE_0___default().isFunction(selector) ?
141
- selector(sourceData) :
141
+ const getSubsetOfData = (sourceData, selector, withOnyxInstanceState) => underscore__WEBPACK_IMPORTED_MODULE_0___default().isFunction(selector) ?
142
+ selector(sourceData, withOnyxInstanceState) :
142
143
  lodash_get__WEBPACK_IMPORTED_MODULE_3___default()(sourceData, selector);
143
144
 
144
145
  /**
@@ -147,11 +148,12 @@ lodash_get__WEBPACK_IMPORTED_MODULE_3___default()(sourceData, selector);
147
148
  * The resulting collection will only contain items that are returned by the selector.
148
149
  * @param {Object} collection
149
150
  * @param {String|Function} selector (see method docs for getSubsetOfData() for full details)
151
+ * @param {Object} [withOnyxInstanceState]
150
152
  * @returns {Object}
151
153
  */
152
- const reduceCollectionWithSelector = (collection, selector) => underscore__WEBPACK_IMPORTED_MODULE_0___default().reduce(collection, (finalCollection, item, key) => {
154
+ const reduceCollectionWithSelector = (collection, selector, withOnyxInstanceState) => underscore__WEBPACK_IMPORTED_MODULE_0___default().reduce(collection, (finalCollection, item, key) => {
153
155
  // eslint-disable-next-line no-param-reassign
154
- finalCollection[key] = getSubsetOfData(item, selector);
156
+ finalCollection[key] = getSubsetOfData(item, selector, withOnyxInstanceState);
155
157
 
156
158
  return finalCollection;
157
159
  }, {});
@@ -447,8 +449,8 @@ function keysChanged(collectionKey, partialCollection) {
447
449
  // returned by the selector.
448
450
  if (subscriber.selector) {
449
451
  subscriber.withOnyxInstance.setState((prevState) => {
450
- const previousData = reduceCollectionWithSelector(prevState[subscriber.statePropertyName], subscriber.selector);
451
- const newData = reduceCollectionWithSelector(cachedCollection, subscriber.selector);
452
+ const previousData = reduceCollectionWithSelector(prevState[subscriber.statePropertyName], subscriber.selector, subscriber.withOnyxInstance.state);
453
+ const newData = reduceCollectionWithSelector(cachedCollection, subscriber.selector, subscriber.withOnyxInstance.state);
452
454
 
453
455
  if (!(0,fast_equals__WEBPACK_IMPORTED_MODULE_2__.deepEqual)(previousData, newData)) {
454
456
  return {
@@ -491,7 +493,7 @@ function keysChanged(collectionKey, partialCollection) {
491
493
  if (subscriber.selector) {
492
494
  subscriber.withOnyxInstance.setState((prevState) => {
493
495
  const prevData = prevState[subscriber.statePropertyName];
494
- const newData = getSubsetOfData(cachedCollection[subscriber.key], subscriber.selector);
496
+ const newData = getSubsetOfData(cachedCollection[subscriber.key], subscriber.selector, subscriber.withOnyxInstance.state);
495
497
  if (!(0,fast_equals__WEBPACK_IMPORTED_MODULE_2__.deepEqual)(prevData, newData)) {
496
498
  _metrics_PerformanceUtils__WEBPACK_IMPORTED_MODULE_8__.logSetStateCall(subscriber, prevData, newData, 'keysChanged', collectionKey);
497
499
  return {
@@ -573,11 +575,11 @@ function keyChanged(key, data, canUpdateSubscriber) {
573
575
  subscriber.withOnyxInstance.setState((prevState) => {
574
576
  const prevData = prevState[subscriber.statePropertyName];
575
577
  const newData = {
576
- [key]: getSubsetOfData(data, subscriber.selector)
578
+ [key]: getSubsetOfData(data, subscriber.selector, subscriber.withOnyxInstance.state)
577
579
  };
578
580
  const prevDataWithNewData = {
579
581
  ...prevData,
580
- [key]: getSubsetOfData(data, subscriber.selector)
582
+ [key]: getSubsetOfData(data, subscriber.selector, subscriber.withOnyxInstance.state)
581
583
  };
582
584
  if (!(0,fast_equals__WEBPACK_IMPORTED_MODULE_2__.deepEqual)(prevData, prevDataWithNewData)) {
583
585
  _metrics_PerformanceUtils__WEBPACK_IMPORTED_MODULE_8__.logSetStateCall(subscriber, prevData, newData, 'keyChanged', key);
@@ -608,8 +610,8 @@ function keyChanged(key, data, canUpdateSubscriber) {
608
610
  // returned by the selector and only if the selected data has changed.
609
611
  if (subscriber.selector) {
610
612
  subscriber.withOnyxInstance.setState((prevState) => {
611
- const previousValue = getSubsetOfData(prevState[subscriber.statePropertyName], subscriber.selector);
612
- const newValue = getSubsetOfData(data, subscriber.selector);
613
+ const previousValue = getSubsetOfData(prevState[subscriber.statePropertyName], subscriber.selector, subscriber.withOnyxInstance.state);
614
+ const newValue = getSubsetOfData(data, subscriber.selector, subscriber.withOnyxInstance.state);
613
615
  if (!(0,fast_equals__WEBPACK_IMPORTED_MODULE_2__.deepEqual)(previousValue, newValue)) {
614
616
  return {
615
617
  [subscriber.statePropertyName]: newValue
@@ -667,9 +669,9 @@ function sendDataToConnection(mapping, val, matchedKey) {
667
669
  // returned by the selector.
668
670
  if (mapping.selector) {
669
671
  if (isCollectionKey(mapping.key)) {
670
- newData = reduceCollectionWithSelector(val, mapping.selector);
672
+ newData = reduceCollectionWithSelector(val, mapping.selector, mapping.withOnyxInstance.state);
671
673
  } else {
672
- newData = getSubsetOfData(val, mapping.selector);
674
+ newData = getSubsetOfData(val, mapping.selector, mapping.withOnyxInstance.state);
673
675
  }
674
676
  }
675
677
 
@@ -747,9 +749,10 @@ function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
747
749
  * component
748
750
  * @param {Boolean} [mapping.waitForCollectionCallback] If set to true, it will return the entire collection to the callback as a single object
749
751
  * @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.
750
- * 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
751
- * 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.
752
- * 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).
752
+ * 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
753
+ * passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component
754
+ * 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
755
+ * be expensive from a performance standpoint).
753
756
  * @returns {Number} an ID to use when calling disconnect
754
757
  */
755
758
  function connect(mapping) {
@@ -1274,27 +1277,28 @@ function update(data) {
1274
1277
  });
1275
1278
 
1276
1279
  const promises = [];
1280
+ let clearPromise = Promise.resolve();
1277
1281
 
1278
1282
  underscore__WEBPACK_IMPORTED_MODULE_0___default().each(data, (_ref2) => {let { onyxMethod, key, value } = _ref2;
1279
1283
  switch (onyxMethod) {
1280
1284
  case METHOD.SET:
1281
- promises.push(set(key, value));
1285
+ promises.push(() => set(key, value));
1282
1286
  break;
1283
1287
  case METHOD.MERGE:
1284
- promises.push(merge(key, value));
1288
+ promises.push(() => merge(key, value));
1285
1289
  break;
1286
1290
  case METHOD.MERGE_COLLECTION:
1287
- promises.push(mergeCollection(key, value));
1291
+ promises.push(() => mergeCollection(key, value));
1288
1292
  break;
1289
1293
  case METHOD.CLEAR:
1290
- promises.push(clear());
1294
+ clearPromise = clear();
1291
1295
  break;
1292
1296
  default:
1293
1297
  break;}
1294
1298
 
1295
1299
  });
1296
1300
 
1297
- return Promise.all(promises);
1301
+ return clearPromise.then(() => Promise.all(underscore__WEBPACK_IMPORTED_MODULE_0___default().map(promises, (p) => p())));
1298
1302
  }
1299
1303
 
1300
1304
  /**