Skip to content

Commit 0717c75

Browse files
Revert "move collection keys methods to seperate module to not create dependecy cycle"
1 parent 1e9452a commit 0717c75

4 files changed

Lines changed: 25 additions & 91 deletions

File tree

lib/CollectionKeyUtils.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

lib/OnyxStorageManager/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import bindAll from 'lodash/bindAll';
22
import type {OnyxKey} from '../types';
33
import Storage from '../storage';
44
import * as Logger from '../Logger';
5-
import {isCollectionKey, isKeyMatch} from '../CollectionKeyUtils';
65
import type {StorageUsageConfig, StorageKeyInfo, StorageMetadata, StorageCleanupResult, CleanupExecutionResult} from './types';
76
import {DEFAULT_STORAGE_CONFIG} from './types';
87

@@ -216,7 +215,10 @@ class OnyxStorageManager {
216215

217216
private isKeyEvictable(key: OnyxKey): boolean {
218217
return this.evictableKeys.some((pattern) => {
219-
return isKeyMatch(pattern, key);
218+
if (pattern.endsWith('_')) {
219+
return key.startsWith(pattern);
220+
}
221+
return key === pattern;
220222
});
221223
}
222224

lib/OnyxUtils.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import * as PerformanceUtils from './PerformanceUtils';
1212
import * as Str from './Str';
1313
import unstable_batchedUpdates from './batch';
1414
import Storage from './storage';
15-
import * as CollectionKeyUtils from './CollectionKeyUtils';
1615
import type {
1716
CollectionKey,
1817
CollectionKeyBase,
@@ -154,9 +153,6 @@ function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Pa
154153
return acc;
155154
}, new Set<OnyxKey>());
156155

157-
// Initialize the shared collection key utility
158-
CollectionKeyUtils.setCollectionKeys(onyxCollectionKeySet);
159-
160156
// Set our default key states to use when initializing and clearing Onyx data
161157
defaultKeyStates = initialKeyStates;
162158

@@ -462,11 +458,11 @@ function getCollectionKeys(): Set<OnyxKey> {
462458
* is associated with a collection of keys.
463459
*/
464460
function isCollectionKey(key: OnyxKey): key is CollectionKeyBase {
465-
return CollectionKeyUtils.isCollectionKey(key);
461+
return onyxCollectionKeySet.has(key);
466462
}
467463

468464
function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}` {
469-
return CollectionKeyUtils.isCollectionMemberKey(collectionKey, key);
465+
return key.startsWith(collectionKey) && key.length > collectionKey.length;
470466
}
471467

472468
/**
@@ -497,7 +493,7 @@ function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType
497493
* or if the provided key is a collection member key (in case our configured key is a "collection key")
498494
*/
499495
function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
500-
return CollectionKeyUtils.isKeyMatch(configKey, key);
496+
return isCollectionKey(configKey) ? Str.startsWith(key, configKey) : configKey === key;
501497
}
502498

503499
/**
@@ -513,7 +509,24 @@ function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
513509
* @returns The plain collection key or throws an Error if the key is not a collection one.
514510
*/
515511
function getCollectionKey(key: CollectionKey): string {
516-
return CollectionKeyUtils.getCollectionKey(key);
512+
// Start by finding the position of the last underscore in the string
513+
let lastUnderscoreIndex = key.lastIndexOf('_');
514+
515+
// Iterate backwards to find the longest key that ends with '_'
516+
while (lastUnderscoreIndex > 0) {
517+
const possibleKey = key.slice(0, lastUnderscoreIndex + 1);
518+
519+
// Check if the substring is a key in the Set
520+
if (isCollectionKey(possibleKey)) {
521+
// Return the matching key and the rest of the string
522+
return possibleKey;
523+
}
524+
525+
// Move to the next underscore to check smaller possible keys
526+
lastUnderscoreIndex = key.lastIndexOf('_', lastUnderscoreIndex - 1);
527+
}
528+
529+
throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
517530
}
518531

519532
/**

tests/unit/storageEvictionTest.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {jest} from '@jest/globals';
22
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
33
import Storage from '../../lib/storage';
44
import {OnyxStorageManager} from '../../lib/OnyxStorageManager';
5-
import * as CollectionKeyUtils from '../../lib/CollectionKeyUtils';
65
import type {StorageUsageConfig, StorageMetadata} from '../../lib/OnyxStorageManager/types';
76

87
// Mock the Storage module
@@ -300,8 +299,6 @@ describe('Storage Eviction Tests', () => {
300299
});
301300

302301
it('should support key patterns with underscore suffix', async () => {
303-
CollectionKeyUtils.setCollectionKeys(new Set(['temp_']));
304-
305302
const config: StorageUsageConfig = {
306303
enabled: true,
307304
evictableKeys: ['temp_'], // Pattern: keys starting with 'temp_'

0 commit comments

Comments
 (0)