react-native-onyx 1.0.30 → 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
@@ -778,6 +778,19 @@ function notifySubscribersOnNextTick(key, value, canUpdateSubscriber) {
778
778
  Promise.resolve().then(() => keyChanged(key, value, canUpdateSubscriber));
779
779
  }
780
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
+
781
794
  /**
782
795
  * Remove a key from Onyx and update the subscribers
783
796
  *
@@ -1038,24 +1051,55 @@ function clear(keysToPreserve = []) {
1038
1051
  const defaultKeys = _.keys(defaultKeyStates);
1039
1052
 
1040
1053
  // 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)
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)
1043
1058
  const keysToClear = _.difference(keys, keysToPreserve, defaultKeys);
1044
1059
  keyValuesToReset.push(..._.map(keysToClear, key => [key, null]));
1045
1060
 
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
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
1047
1063
  // to the default.
1048
1064
  const defaultKeyValuePairs = _.pairs(_.omit(defaultKeyStates, ...keysToPreserve));
1049
1065
 
1050
- // Add the default key value pairs to the keyValuesToReset so that they get set back to their default values when we clear Onyx
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
1051
1068
  keyValuesToReset.push(...defaultKeyValuePairs);
1052
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
+
1053
1076
  // 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()
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()
1056
1080
  _.each(keyValuesToReset, (keyValue) => {
1057
- cache.set(keyValue[0], keyValue[1]);
1058
- notifySubscribersOnNextTick(keyValue[0], keyValue[1]);
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;
1095
+ });
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);
1059
1103
  });
1060
1104
 
1061
1105
  return Storage.multiSet(keyValuesToReset);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.30",
3
+ "version": "1.0.31",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",