react-native-onyx 1.0.28 → 1.0.30

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) {
@@ -1025,17 +1028,37 @@ function initializeWithDefaultKeyStates() {
1025
1028
  * Storage.setItem() from Onyx.clear() will have already finished and the merged
1026
1029
  * value will be saved to storage after the default value.
1027
1030
  *
1031
+ * @param {Array} keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data
1028
1032
  * @returns {Promise<void>}
1029
1033
  */
1030
- function clear() {
1034
+ function clear(keysToPreserve = []) {
1031
1035
  return getAllKeys()
1032
1036
  .then((keys) => {
1033
- _.each(keys, (key) => {
1034
- const resetValue = lodashGet(defaultKeyStates, key, null);
1035
- cache.set(key, resetValue);
1036
- notifySubscribersOnNextTick(key, resetValue);
1037
+ const keyValuesToReset = [];
1038
+ const defaultKeys = _.keys(defaultKeyStates);
1039
+
1040
+ // The only keys that should not be cleared are:
1041
+ // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline status, or activeClients need to remain in Onyx even when signed out)
1042
+ // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them to null would cause unknown behavior)
1043
+ const keysToClear = _.difference(keys, keysToPreserve, defaultKeys);
1044
+ keyValuesToReset.push(..._.map(keysToClear, key => [key, null]));
1045
+
1046
+ // Remove any keysToPreserve from the defaultKeyStates because if they are passed in it has been explicitly called out to preserve those values instead of resetting them back
1047
+ // to the default.
1048
+ const defaultKeyValuePairs = _.pairs(_.omit(defaultKeyStates, ...keysToPreserve));
1049
+
1050
+ // Add the default key value pairs to the keyValuesToReset so that they get set back to their default values when we clear Onyx
1051
+ keyValuesToReset.push(...defaultKeyValuePairs);
1052
+
1053
+ // Make sure that we also reset the cache values before clearing the values from storage.
1054
+ // We do this before clearing Storage so that any call to clear() followed by merge() on a key with a default state results in the merged value getting saved, since the update
1055
+ // from the merge() call would happen on the tick after the update from this clear()
1056
+ _.each(keyValuesToReset, (keyValue) => {
1057
+ cache.set(keyValue[0], keyValue[1]);
1058
+ notifySubscribersOnNextTick(keyValue[0], keyValue[1]);
1037
1059
  });
1038
- return Storage.clear();
1060
+
1061
+ return Storage.multiSet(keyValuesToReset);
1039
1062
  });
1040
1063
  }
1041
1064
 
@@ -1,6 +1,6 @@
1
1
  // For web-only implementations of Onyx, this module will just be a no-op
2
2
 
3
- function decorateWithMetrics() {}
3
+ function decorateWithMetrics(func) { return func; }
4
4
  function getMetrics() {}
5
5
  function printMetrics() {}
6
6
  function resetMetrics() {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.28",
3
+ "version": "1.0.30",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",