react-native-onyx 2.0.66 → 2.0.68

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/dist/Onyx.js CHANGED
@@ -416,8 +416,15 @@ function clear(keysToPreserve = []) {
416
416
  const newValue = (_a = defaultKeyStates[key]) !== null && _a !== void 0 ? _a : null;
417
417
  if (newValue !== oldValue) {
418
418
  OnyxCache_1.default.set(key, newValue);
419
- const collectionKey = OnyxUtils_1.default.getCollectionKey(key);
420
- if (OnyxUtils_1.default.isCollectionKey(collectionKey)) {
419
+ let collectionKey;
420
+ try {
421
+ collectionKey = OnyxUtils_1.default.getCollectionKey(key);
422
+ }
423
+ catch (e) {
424
+ // If getCollectionKey() throws an error it means the key is not a collection key.
425
+ collectionKey = undefined;
426
+ }
427
+ if (collectionKey) {
421
428
  if (!keyValuesToResetAsCollection[collectionKey]) {
422
429
  keyValuesToResetAsCollection[collectionKey] = {};
423
430
  }
@@ -162,8 +162,13 @@ class OnyxConnectionManager {
162
162
  * */
163
163
  addToEvictionBlockList(connection) {
164
164
  var _a;
165
- const connectionMetadata = this.connectionsMap.get(connection === null || connection === void 0 ? void 0 : connection.id);
165
+ if (!connection) {
166
+ Logger.logInfo(`[ConnectionManager] Attempted to add connection to eviction block list passing an undefined connection object.`);
167
+ return;
168
+ }
169
+ const connectionMetadata = this.connectionsMap.get(connection.id);
166
170
  if (!connectionMetadata) {
171
+ Logger.logInfo(`[ConnectionManager] Attempted to add connection to eviction block list but no connection was found.`);
167
172
  return;
168
173
  }
169
174
  const evictionBlocklist = OnyxUtils_1.default.getEvictionBlocklist();
@@ -178,8 +183,13 @@ class OnyxConnectionManager {
178
183
  */
179
184
  removeFromEvictionBlockList(connection) {
180
185
  var _a, _b, _c;
181
- const connectionMetadata = this.connectionsMap.get(connection === null || connection === void 0 ? void 0 : connection.id);
186
+ if (!connection) {
187
+ Logger.logInfo(`[ConnectionManager] Attempted to remove connection from eviction block list passing an undefined connection object.`);
188
+ return;
189
+ }
190
+ const connectionMetadata = this.connectionsMap.get(connection.id);
182
191
  if (!connectionMetadata) {
192
+ Logger.logInfo(`[ConnectionManager] Attempted to remove connection from eviction block list but no connection was found.`);
183
193
  return;
184
194
  }
185
195
  const evictionBlocklist = OnyxUtils_1.default.getEvictionBlocklist();
@@ -90,11 +90,12 @@ declare function isSafeEvictionKey(testKey: OnyxKey): boolean;
90
90
  *
91
91
  * For example:
92
92
  * - `getCollectionKey("report_123")` would return "report_"
93
- * - `getCollectionKey("report")` would return "report"
94
93
  * - `getCollectionKey("report_")` would return "report_"
94
+ * - `getCollectionKey("report_-1_something")` would return "report_"
95
+ * - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
95
96
  *
96
97
  * @param {OnyxKey} key - The key to process.
97
- * @return {string} The pure key without any numeric
98
+ * @return {string} The plain collection key.
98
99
  */
99
100
  declare function getCollectionKey(key: OnyxKey): string;
100
101
  /**
package/dist/OnyxUtils.js CHANGED
@@ -335,13 +335,8 @@ function isCollectionMemberKey(collectionKey, key, collectionKeyLength) {
335
335
  * @returns A tuple where the first element is the collection part and the second element is the ID part.
336
336
  */
337
337
  function splitCollectionMemberKey(key) {
338
- const underscoreIndex = key.lastIndexOf('_');
339
- if (underscoreIndex === -1) {
340
- throw new Error(`Invalid ${key} key provided, only collection keys are allowed.`);
341
- }
342
- const collectionKey = key.substring(0, underscoreIndex + 1);
343
- const memberKey = key.substring(underscoreIndex + 1);
344
- return [collectionKey, memberKey];
338
+ const collectionKey = getCollectionKey(key);
339
+ return [collectionKey, key.slice(collectionKey.length)];
345
340
  }
346
341
  /**
347
342
  * Checks to see if a provided key is the exact configured key of our connected subscriber
@@ -359,18 +354,28 @@ function isSafeEvictionKey(testKey) {
359
354
  *
360
355
  * For example:
361
356
  * - `getCollectionKey("report_123")` would return "report_"
362
- * - `getCollectionKey("report")` would return "report"
363
357
  * - `getCollectionKey("report_")` would return "report_"
358
+ * - `getCollectionKey("report_-1_something")` would return "report_"
359
+ * - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
364
360
  *
365
361
  * @param {OnyxKey} key - The key to process.
366
- * @return {string} The pure key without any numeric
362
+ * @return {string} The plain collection key.
367
363
  */
368
364
  function getCollectionKey(key) {
369
- const underscoreIndex = key.lastIndexOf('_');
370
- if (underscoreIndex === -1) {
371
- return key;
365
+ // Start by finding the position of the last underscore in the string
366
+ let lastUnderscoreIndex = key.lastIndexOf('_');
367
+ // Iterate backwards to find the longest key that ends with '_'
368
+ while (lastUnderscoreIndex > 0) {
369
+ const possibleKey = key.slice(0, lastUnderscoreIndex + 1);
370
+ // Check if the substring is a key in the Set
371
+ if (isCollectionKey(possibleKey)) {
372
+ // Return the matching key and the rest of the string
373
+ return possibleKey;
374
+ }
375
+ // Move to the next underscore to check smaller possible keys
376
+ lastUnderscoreIndex = key.lastIndexOf('_', lastUnderscoreIndex - 1);
372
377
  }
373
- return key.substring(0, underscoreIndex + 1);
378
+ throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
374
379
  }
375
380
  /**
376
381
  * Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
@@ -639,11 +644,17 @@ function keyChanged(key, value, previousValue, canUpdateSubscriber = () => true,
639
644
  // do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
640
645
  // For performance reason, we look for the given key and later if don't find it we look for the collection key, instead of checking if it is a collection key first.
641
646
  let stateMappingKeys = (_a = onyxKeyToSubscriptionIDs.get(key)) !== null && _a !== void 0 ? _a : [];
642
- const collectionKey = getCollectionKey(key);
643
- const plainCollectionKey = collectionKey.lastIndexOf('_') !== -1 ? collectionKey : undefined;
644
- if (plainCollectionKey) {
647
+ let collectionKey;
648
+ try {
649
+ collectionKey = getCollectionKey(key);
650
+ }
651
+ catch (e) {
652
+ // If getCollectionKey() throws an error it means the key is not a collection key.
653
+ collectionKey = undefined;
654
+ }
655
+ if (collectionKey) {
645
656
  // Getting the collection key from the specific key because only collection keys were stored in the mapping.
646
- stateMappingKeys = [...stateMappingKeys, ...((_b = onyxKeyToSubscriptionIDs.get(plainCollectionKey)) !== null && _b !== void 0 ? _b : [])];
657
+ stateMappingKeys = [...stateMappingKeys, ...((_b = onyxKeyToSubscriptionIDs.get(collectionKey)) !== null && _b !== void 0 ? _b : [])];
647
658
  if (stateMappingKeys.length === 0) {
648
659
  return;
649
660
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.66",
3
+ "version": "2.0.68",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",