react-native-onyx 1.0.29 → 1.0.31

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
@@ -661,7 +661,10 @@ function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
661
661
  * @param {Boolean} [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the
662
662
  * component
663
663
  * @param {Boolean} [mapping.waitForCollectionCallback] If set to true, it will return the entire collection to the callback as a single object
664
- * @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. 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).
664
+ * @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).
665
668
  * @returns {Number} an ID to use when calling disconnect
666
669
  */
667
670
  function connect(mapping) {
@@ -775,6 +778,19 @@ function notifySubscribersOnNextTick(key, value, canUpdateSubscriber) {
775
778
  Promise.resolve().then(() => keyChanged(key, value, canUpdateSubscriber));
776
779
  }
777
780
 
781
+ /**
782
+ * This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections
783
+ * so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
784
+ * subscriber callbacks receive the data in a different format than they normally expect and it breaks code.
785
+ *
786
+ * @param {String} key
787
+ * @param {*} value
788
+ */
789
+ // eslint-disable-next-line rulesdir/no-negated-variables
790
+ function notifyCollectionSubscribersOnNextTick(key, value) {
791
+ Promise.resolve().then(() => keysChanged(key, value));
792
+ }
793
+
778
794
  /**
779
795
  * Remove a key from Onyx and update the subscribers
780
796
  *
@@ -1025,17 +1041,68 @@ function initializeWithDefaultKeyStates() {
1025
1041
  * Storage.setItem() from Onyx.clear() will have already finished and the merged
1026
1042
  * value will be saved to storage after the default value.
1027
1043
  *
1044
+ * @param {Array} keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data
1028
1045
  * @returns {Promise<void>}
1029
1046
  */
1030
- function clear() {
1047
+ function clear(keysToPreserve = []) {
1031
1048
  return getAllKeys()
1032
1049
  .then((keys) => {
1033
- _.each(keys, (key) => {
1034
- const resetValue = lodashGet(defaultKeyStates, key, null);
1035
- cache.set(key, resetValue);
1036
- notifySubscribersOnNextTick(key, resetValue);
1050
+ const keyValuesToReset = [];
1051
+ const defaultKeys = _.keys(defaultKeyStates);
1052
+
1053
+ // The only keys that should not be cleared are:
1054
+ // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline
1055
+ // status, or activeClients need to remain in Onyx even when signed out)
1056
+ // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them
1057
+ // to null would cause unknown behavior)
1058
+ const keysToClear = _.difference(keys, keysToPreserve, defaultKeys);
1059
+ keyValuesToReset.push(..._.map(keysToClear, key => [key, null]));
1060
+
1061
+ // Remove any keysToPreserve from the defaultKeyStates because if they are passed in it has been explicitly
1062
+ // called out to preserve those values instead of resetting them back
1063
+ // to the default.
1064
+ const defaultKeyValuePairs = _.pairs(_.omit(defaultKeyStates, ...keysToPreserve));
1065
+
1066
+ // Add the default key value pairs to the keyValuesToReset so that they get set back to their default values
1067
+ // when we clear Onyx
1068
+ keyValuesToReset.push(...defaultKeyValuePairs);
1069
+
1070
+ // We now have all the key/values that need to be reset, but we're not done yet!
1071
+ // There will be two groups of key/values and they each need to be updated a little bit differently.
1072
+ // Collection keys need to be notified differently than non collection keys
1073
+ const keyValuesToResetAsCollection = {};
1074
+ const keyValuesToResetIndividually = {};
1075
+
1076
+ // Make sure that we also reset the cache values before clearing the values from storage.
1077
+ // We do this before clearing Storage so that any call to clear() followed by merge() on a key with a
1078
+ // default state results in the merged value getting saved, since the update from the merge() call would
1079
+ // happen on the tick after the update from this clear()
1080
+ _.each(keyValuesToReset, (keyValue) => {
1081
+ const key = keyValue[0];
1082
+ const value = keyValue[1];
1083
+ cache.set(key, value);
1084
+
1085
+ const collectionKey = key.substring(0, key.indexOf('_') + 1);
1086
+ if (collectionKey) {
1087
+ if (!keyValuesToResetAsCollection[collectionKey]) {
1088
+ keyValuesToResetAsCollection[collectionKey] = {};
1089
+ }
1090
+ keyValuesToResetAsCollection[collectionKey][key] = value;
1091
+ return;
1092
+ }
1093
+
1094
+ keyValuesToResetIndividually[key] = value;
1037
1095
  });
1038
- return Storage.clear();
1096
+
1097
+ // Notify the subscribers for each key/value group so they can receive the new values
1098
+ _.each(keyValuesToResetIndividually, (value, key) => {
1099
+ notifySubscribersOnNextTick(key, value);
1100
+ });
1101
+ _.each(keyValuesToResetAsCollection, (value, key) => {
1102
+ notifyCollectionSubscribersOnNextTick(key, value);
1103
+ });
1104
+
1105
+ return Storage.multiSet(keyValuesToReset);
1039
1106
  });
1040
1107
  }
1041
1108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",