Skip to content

Commit 88c11d2

Browse files
authored
Merge pull request Expensify#737 from callstack-internal/JKobrynski/feat/81830-remove-try-catch-from-getcollectionkey
[Onyx Audit] Remove try/catch block from getCollectionKey
2 parents 4390705 + ca7a4d5 commit 88c11d2

6 files changed

Lines changed: 24 additions & 45 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);
356350

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

lib/OnyxCache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,13 @@ class OnyxCache {
458458
/**
459459
* Get the collection key for a given member key
460460
*/
461-
getCollectionKey(key: OnyxKey): OnyxKey | null {
461+
getCollectionKey(key: OnyxKey): OnyxKey | undefined {
462462
for (const collectionKey of this.collectionKeys) {
463463
if (key.startsWith(collectionKey) && key.length > collectionKey.length) {
464464
return collectionKey;
465465
}
466466
}
467-
return null;
467+
return undefined;
468468
}
469469

470470
/**

lib/OnyxSnapshotCache.ts

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

134-
try {
135-
// Check if the key is a collection member and invalidate the collection base key
136-
const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate);
134+
// Check if the key is a collection member and invalidate the collection base key
135+
const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate);
136+
if (collectionBaseKey) {
137137
this.snapshotCache.delete(collectionBaseKey);
138-
} catch (e) {
139-
// do nothing - this just means the key is not a collection member
140138
}
141139
}
142140

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 && 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) {
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) {
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 undefined if the key is not a collection one.
559555
*/
560-
function getCollectionKey(key: CollectionKey): string {
556+
function getCollectionKey(key: CollectionKey): string | undefined {
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 undefined;
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);
764754

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

tests/unit/OnyxSnapshotCacheTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ describe('OnyxSnapshotCache', () => {
156156

157157
it('should invalidate non-collection keys without affecting others', () => {
158158
mockedOnyxUtils.isCollectionKey.mockReturnValue(false);
159+
mockedOnyxUtils.getCollectionKey.mockReturnValue(undefined);
159160

160161
cache.invalidateForKey('nonCollectionKey');
161162

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 undefined if key does not contain underscore', () => {
346+
expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeUndefined();
347+
expect(OnyxUtils.getCollectionKey('')).toBeUndefined();
352348
});
353349
});
354350

0 commit comments

Comments
 (0)