react-native-onyx 1.0.40 → 1.0.42
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 +8 -6
- package/dist/web.development.js +20 -16
- package/dist/web.development.js.map +1 -1
- package/dist/web.min.js +1 -1
- package/dist/web.min.js.map +1 -1
- package/lib/Onyx.js +20 -16
- package/package.json +1 -1
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
|
|
666
|
-
* simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component
|
|
667
|
-
* Otherwise, any change of data on any property would normally cause the component to re-render (and that can
|
|
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) {
|
|
@@ -1100,6 +1103,7 @@ function clear(keysToPreserve = []) {
|
|
|
1100
1103
|
const defaultKeyValuePairs = _.pairs(_.omit(defaultKeyStates, keysToPreserve));
|
|
1101
1104
|
|
|
1102
1105
|
// Remove only the items that we want cleared from storage, and reset others to default
|
|
1106
|
+
_.each(keysToBeClearedFromStorage, key => cache.drop(key));
|
|
1103
1107
|
return Storage.removeItems(keysToBeClearedFromStorage).then(() => Storage.multiSet(defaultKeyValuePairs));
|
|
1104
1108
|
});
|
|
1105
1109
|
}
|