Skip to content

Commit ea154a6

Browse files
committed
remove try/catch block from getCollectionKey, update references
1 parent 957634a commit ea154a6

4 files changed

Lines changed: 21 additions & 43 deletions

File tree

lib/Onyx.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
346346
if (newValue !== oldValue) {
347347
cache.set(key, newValue);
348348

349-
let collectionKey: string | undefined;
350-
try {
351-
collectionKey = OnyxUtils.getCollectionKey(key);
352-
} catch (e) {
353-
// If getCollectionKey() throws an error it means the key is not a collection key.
354-
collectionKey = undefined;
355-
}
349+
const collectionKey = OnyxUtils.getCollectionKey(key) ?? undefined;
356350

357351
if (collectionKey) {
358352
if (!keyValuesToResetAsCollection[collectionKey]) {

lib/OnyxSnapshotCache.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,10 @@ class OnyxSnapshotCache {
133133
// Always invalidate the exact key
134134
this.snapshotCache.delete(keyToInvalidate);
135135

136-
try {
137-
// Check if the key is a collection member and invalidate the collection base key
138-
const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate);
136+
// Check if the key is a collection member and invalidate the collection base key
137+
const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate);
138+
if (collectionBaseKey !== null) {
139139
this.snapshotCache.delete(collectionBaseKey);
140-
} catch (e) {
141-
// do nothing - this just means the key is not a collection member
142140
}
143141
}
144142

lib/OnyxUtils.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,9 @@ function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collect
475475
* @returns true if the key is a collection member, false otherwise
476476
*/
477477
function isCollectionMember(key: OnyxKey): boolean {
478-
try {
479-
const collectionKey = getCollectionKey(key);
480-
// If the key is longer than the collection key, it's a collection member
481-
return key.length > collectionKey.length;
482-
} catch (e) {
483-
// If getCollectionKey throws, the key is not a collection member
484-
return false;
485-
}
478+
const collectionKey = getCollectionKey(key);
479+
// If the key is longer than the collection key, it's a collection member
480+
return collectionKey !== null && key.length > collectionKey.length;
486481
}
487482

488483
/**
@@ -503,12 +498,9 @@ function isCollectionMember(key: OnyxKey): boolean {
503498
* @returns true if key is a RAM-only key, RAM-only collection key, or a RAM-only collection member
504499
*/
505500
function isRamOnlyKey(key: OnyxKey): boolean {
506-
try {
507-
const collectionKey = getCollectionKey(key);
508-
// If collectionKey exists for a given key, check if it's a RAM-only key
501+
const collectionKey = getCollectionKey(key);
502+
if (collectionKey !== null) {
509503
return cache.isRamOnlyKey(collectionKey);
510-
} catch {
511-
// If getCollectionKey throws, the key is not a collection member
512504
}
513505

514506
return cache.isRamOnlyKey(key);
@@ -530,8 +522,12 @@ function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType
530522
}
531523

532524
if (!collectionKey) {
525+
const resolvedKey = getCollectionKey(key);
526+
if (resolvedKey === null) {
527+
throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
528+
}
533529
// eslint-disable-next-line no-param-reassign
534-
collectionKey = getCollectionKey(key);
530+
collectionKey = resolvedKey;
535531
}
536532

537533
return [collectionKey as CollectionKeyType, key.slice(collectionKey.length)];
@@ -555,9 +551,9 @@ function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
555551
* - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
556552
*
557553
* @param key - The collection key to process.
558-
* @returns The plain collection key or throws an Error if the key is not a collection one.
554+
* @returns The plain collection key or null if the key is not a collection one.
559555
*/
560-
function getCollectionKey(key: CollectionKey): string {
556+
function getCollectionKey(key: CollectionKey): string | null {
561557
// Start by finding the position of the last underscore in the string
562558
let lastUnderscoreIndex = key.lastIndexOf('_');
563559

@@ -575,7 +571,7 @@ function getCollectionKey(key: CollectionKey): string {
575571
lastUnderscoreIndex = key.lastIndexOf('_', lastUnderscoreIndex - 1);
576572
}
577573

578-
throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
574+
return null;
579575
}
580576

581577
/**
@@ -754,13 +750,7 @@ function keyChanged<TKey extends OnyxKey>(
754750
// do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
755751
// 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.
756752
let stateMappingKeys = onyxKeyToSubscriptionIDs.get(key) ?? [];
757-
let collectionKey: string | undefined;
758-
try {
759-
collectionKey = getCollectionKey(key);
760-
} catch (e) {
761-
// If getCollectionKey() throws an error it means the key is not a collection key.
762-
collectionKey = undefined;
763-
}
753+
const collectionKey = getCollectionKey(key) ?? undefined;
764754

765755
if (collectionKey) {
766756
// Getting the collection key from the specific key because only collection keys were stored in the mapping.

tests/unit/onyxUtilsTest.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,9 @@ describe('OnyxUtils', () => {
342342
});
343343
});
344344

345-
it('should throw error if key does not contain underscore', () => {
346-
expect(() => {
347-
OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY);
348-
}).toThrowError("Invalid 'test' key provided, only collection keys are allowed.");
349-
expect(() => {
350-
OnyxUtils.getCollectionKey('');
351-
}).toThrowError("Invalid '' key provided, only collection keys are allowed.");
345+
it('should return null if key does not contain underscore', () => {
346+
expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeNull();
347+
expect(OnyxUtils.getCollectionKey('')).toBeNull();
352348
});
353349
});
354350

0 commit comments

Comments
 (0)