react-native-onyx 3.0.38 → 3.0.40

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
@@ -317,14 +317,7 @@ function clear(keysToPreserve = []) {
317
317
  const newValue = (_a = defaultKeyStates[key]) !== null && _a !== void 0 ? _a : null;
318
318
  if (newValue !== oldValue) {
319
319
  OnyxCache_1.default.set(key, newValue);
320
- let collectionKey;
321
- try {
322
- collectionKey = OnyxUtils_1.default.getCollectionKey(key);
323
- }
324
- catch (e) {
325
- // If getCollectionKey() throws an error it means the key is not a collection key.
326
- collectionKey = undefined;
327
- }
320
+ const collectionKey = OnyxUtils_1.default.getCollectionKey(key);
328
321
  if (collectionKey) {
329
322
  if (!keyValuesToResetAsCollection[collectionKey]) {
330
323
  keyValuesToResetAsCollection[collectionKey] = { oldValues: {}, newValues: {} };
@@ -162,7 +162,7 @@ declare class OnyxCache {
162
162
  /**
163
163
  * Get the collection key for a given member key
164
164
  */
165
- getCollectionKey(key: OnyxKey): OnyxKey | null;
165
+ getCollectionKey(key: OnyxKey): OnyxKey | undefined;
166
166
  /**
167
167
  * Get all data for a collection key
168
168
  */
package/dist/OnyxCache.js CHANGED
@@ -383,7 +383,7 @@ class OnyxCache {
383
383
  return collectionKey;
384
384
  }
385
385
  }
386
- return null;
386
+ return undefined;
387
387
  }
388
388
  /**
389
389
  * Get all data for a collection key
@@ -100,14 +100,11 @@ class OnyxSnapshotCache {
100
100
  invalidateForKey(keyToInvalidate) {
101
101
  // Always invalidate the exact key
102
102
  this.snapshotCache.delete(keyToInvalidate);
103
- try {
104
- // Check if the key is a collection member and invalidate the collection base key
105
- const collectionBaseKey = OnyxUtils_1.default.getCollectionKey(keyToInvalidate);
103
+ // Check if the key is a collection member and invalidate the collection base key
104
+ const collectionBaseKey = OnyxUtils_1.default.getCollectionKey(keyToInvalidate);
105
+ if (collectionBaseKey) {
106
106
  this.snapshotCache.delete(collectionBaseKey);
107
107
  }
108
- catch (e) {
109
- // do nothing - this just means the key is not a collection member
110
- }
111
108
  }
112
109
  /**
113
110
  * Clear all snapshot cache
@@ -163,9 +163,9 @@ declare function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean;
163
163
  * - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
164
164
  *
165
165
  * @param key - The collection key to process.
166
- * @returns The plain collection key or throws an Error if the key is not a collection one.
166
+ * @returns The plain collection key or undefined if the key is not a collection one.
167
167
  */
168
- declare function getCollectionKey(key: CollectionKey): string;
168
+ declare function getCollectionKey(key: CollectionKey): string | undefined;
169
169
  /**
170
170
  * Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
171
171
  * If the requested key is a collection, it will return an object with all the collection members.
package/dist/OnyxUtils.js CHANGED
@@ -389,15 +389,9 @@ function isCollectionMemberKey(collectionKey, key) {
389
389
  * @returns true if the key is a collection member, false otherwise
390
390
  */
391
391
  function isCollectionMember(key) {
392
- try {
393
- const collectionKey = getCollectionKey(key);
394
- // If the key is longer than the collection key, it's a collection member
395
- return key.length > collectionKey.length;
396
- }
397
- catch (e) {
398
- // If getCollectionKey throws, the key is not a collection member
399
- return false;
400
- }
392
+ const collectionKey = getCollectionKey(key);
393
+ // If the key is longer than the collection key, it's a collection member
394
+ return !!collectionKey && key.length > collectionKey.length;
401
395
  }
402
396
  /**
403
397
  * Checks if a given key is a RAM-only key, RAM-only collection key, or a RAM-only collection member
@@ -417,14 +411,10 @@ function isCollectionMember(key) {
417
411
  * @returns true if key is a RAM-only key, RAM-only collection key, or a RAM-only collection member
418
412
  */
419
413
  function isRamOnlyKey(key) {
420
- try {
421
- const collectionKey = getCollectionKey(key);
422
- // If collectionKey exists for a given key, check if it's a RAM-only key
414
+ const collectionKey = getCollectionKey(key);
415
+ if (collectionKey) {
423
416
  return OnyxCache_1.default.isRamOnlyKey(collectionKey);
424
417
  }
425
- catch (_a) {
426
- // If getCollectionKey throws, the key is not a collection member
427
- }
428
418
  return OnyxCache_1.default.isRamOnlyKey(key);
429
419
  }
430
420
  /**
@@ -439,8 +429,12 @@ function splitCollectionMemberKey(key, collectionKey) {
439
429
  throw new Error(`Invalid '${collectionKey}' collection key provided, it isn't compatible with '${key}' key.`);
440
430
  }
441
431
  if (!collectionKey) {
432
+ const resolvedKey = getCollectionKey(key);
433
+ if (!resolvedKey) {
434
+ throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
435
+ }
442
436
  // eslint-disable-next-line no-param-reassign
443
- collectionKey = getCollectionKey(key);
437
+ collectionKey = resolvedKey;
444
438
  }
445
439
  return [collectionKey, key.slice(collectionKey.length)];
446
440
  }
@@ -461,7 +455,7 @@ function isKeyMatch(configKey, key) {
461
455
  * - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
462
456
  *
463
457
  * @param key - The collection key to process.
464
- * @returns The plain collection key or throws an Error if the key is not a collection one.
458
+ * @returns The plain collection key or undefined if the key is not a collection one.
465
459
  */
466
460
  function getCollectionKey(key) {
467
461
  // Start by finding the position of the last underscore in the string
@@ -477,7 +471,7 @@ function getCollectionKey(key) {
477
471
  // Move to the next underscore to check smaller possible keys
478
472
  lastUnderscoreIndex = key.lastIndexOf('_', lastUnderscoreIndex - 1);
479
473
  }
480
- throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
474
+ return undefined;
481
475
  }
482
476
  /**
483
477
  * Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
@@ -625,14 +619,7 @@ function keyChanged(key, value, canUpdateSubscriber = () => true, isProcessingCo
625
619
  // do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
626
620
  // 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.
627
621
  let stateMappingKeys = (_a = onyxKeyToSubscriptionIDs.get(key)) !== null && _a !== void 0 ? _a : [];
628
- let collectionKey;
629
- try {
630
- collectionKey = getCollectionKey(key);
631
- }
632
- catch (e) {
633
- // If getCollectionKey() throws an error it means the key is not a collection key.
634
- collectionKey = undefined;
635
- }
622
+ const collectionKey = getCollectionKey(key);
636
623
  if (collectionKey) {
637
624
  // Getting the collection key from the specific key because only collection keys were stored in the mapping.
638
625
  stateMappingKeys = [...stateMappingKeys, ...((_b = onyxKeyToSubscriptionIDs.get(collectionKey)) !== null && _b !== void 0 ? _b : [])];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "3.0.38",
3
+ "version": "3.0.40",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",