react-native-onyx 1.0.25 → 1.0.27

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
@@ -10,6 +10,14 @@ import createDeferredTask from './createDeferredTask';
10
10
  import fastMerge from './fastMerge';
11
11
  import * as PerformanceUtils from './metrics/PerformanceUtils';
12
12
 
13
+ // Method constants
14
+ const METHOD = {
15
+ SET: 'set',
16
+ MERGE: 'merge',
17
+ MERGE_COLLECTION: 'mergecollection',
18
+ CLEAR: 'clear',
19
+ };
20
+
13
21
  // Keeps track of the last connectionID that was used so we can keep incrementing it
14
22
  let lastConnectionID = 0;
15
23
 
@@ -1046,6 +1054,11 @@ function clear() {
1046
1054
  * @returns {Promise}
1047
1055
  */
1048
1056
  function mergeCollection(collectionKey, collection) {
1057
+ if (!_.isObject(collection) || _.isArray(collection) || _.isEmpty(collection)) {
1058
+ Logger.logInfo('mergeCollection() called with invalid or empty value. Skipping this update.');
1059
+ return Promise.resolve();
1060
+ }
1061
+
1049
1062
  // Confirm all the collection keys belong to the same parent
1050
1063
  _.each(collection, (_data, dataKey) => {
1051
1064
  if (isKeyMatch(collectionKey, dataKey)) {
@@ -1096,36 +1109,41 @@ function mergeCollection(collectionKey, collection) {
1096
1109
  * Insert API responses and lifecycle data into Onyx
1097
1110
  *
1098
1111
  * @param {Array} data An array of objects with shape {onyxMethod: oneOf('set', 'merge', 'mergeCollection'), key: string, value: *}
1112
+ * @returns {Promise} resolves when all operations are complete
1099
1113
  */
1100
1114
  function update(data) {
1101
1115
  // First, validate the Onyx object is in the format we expect
1102
1116
  _.each(data, ({onyxMethod, key}) => {
1103
- if (!_.contains(['clear', 'set', 'merge', 'mergecollection'], onyxMethod)) {
1117
+ if (!_.contains([METHOD.CLEAR, METHOD.SET, METHOD.MERGE, METHOD.MERGE_COLLECTION], onyxMethod)) {
1104
1118
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
1105
1119
  }
1106
- if (onyxMethod !== 'clear' && !_.isString(key)) {
1120
+ if (onyxMethod !== METHOD.CLEAR && !_.isString(key)) {
1107
1121
  throw new Error(`Invalid ${typeof key} key provided in Onyx update. Onyx key must be of type string.`);
1108
1122
  }
1109
1123
  });
1110
1124
 
1125
+ const promises = [];
1126
+
1111
1127
  _.each(data, ({onyxMethod, key, value}) => {
1112
1128
  switch (onyxMethod) {
1113
- case 'set':
1114
- set(key, value);
1129
+ case METHOD.SET:
1130
+ promises.push(set(key, value));
1115
1131
  break;
1116
- case 'merge':
1117
- merge(key, value);
1132
+ case METHOD.MERGE:
1133
+ promises.push(merge(key, value));
1118
1134
  break;
1119
- case 'mergecollection':
1120
- mergeCollection(key, value);
1135
+ case METHOD.MERGE_COLLECTION:
1136
+ promises.push(mergeCollection(key, value));
1121
1137
  break;
1122
- case 'clear':
1123
- clear();
1138
+ case METHOD.CLEAR:
1139
+ promises.push(clear());
1124
1140
  break;
1125
1141
  default:
1126
1142
  break;
1127
1143
  }
1128
1144
  });
1145
+
1146
+ return Promise.all(promises);
1129
1147
  }
1130
1148
 
1131
1149
  /**
@@ -1214,6 +1232,7 @@ const Onyx = {
1214
1232
  addToEvictionBlockList,
1215
1233
  removeFromEvictionBlockList,
1216
1234
  isSafeEvictionKey,
1235
+ METHOD,
1217
1236
  };
1218
1237
 
1219
1238
  /**
package/lib/OnyxCache.js CHANGED
@@ -111,6 +111,10 @@ class OnyxCache {
111
111
  * @param {Record<string, *>} data - a map of (cache) key - values
112
112
  */
113
113
  merge(data) {
114
+ if (!_.isObject(data) || _.isArray(data)) {
115
+ throw new Error('data passed to cache.merge() must be an Object of onyx key/value pairs');
116
+ }
117
+
114
118
  // lodash adds a small overhead so we don't use it here
115
119
  // eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
116
120
  this.storageMap = Object.assign({}, fastMerge(this.storageMap, data));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",