@@ -2,29 +2,12 @@ import bindAll from 'lodash/bindAll';
22import type { OnyxKey } from '../types' ;
33import Storage from '../storage' ;
44import * as Logger from '../Logger' ;
5- import type { StorageUsageConfig , StorageKeyInfo , StorageMetadata } from './types' ;
5+ import type { StorageUsageConfig , StorageKeyInfo , StorageMetadata , StorageCleanupResult , CleanupExecutionResult } from './types' ;
66import { DEFAULT_STORAGE_CONFIG } from './types' ;
77
88const MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24 ;
99const METADATA_KEY_PREFIX = '__onyx_meta_' ;
1010
11- /**
12- * Represents the result of a storage cleanup operation.
13- * @property cleanedKeys - An array of Onyx keys that were successfully cleaned/evicted.
14- * @property timeElapsed - The time taken (in milliseconds) to perform the cleanup.
15- * @property errors - An array of error messages encountered during the cleanup process.
16- */
17- type StorageCleanupResult = {
18- cleanedKeys : OnyxKey [ ] ;
19- timeElapsed : number ;
20- errors : string [ ] ;
21- } ;
22-
23- type CleanupExecutionResult = {
24- successfulKeys : OnyxKey [ ] ;
25- failedKeys : OnyxKey [ ] ;
26- } ;
27-
2811class OnyxStorageManager {
2912 /**
3013 * Stores metadata and tracking information for each Onyx key in storage.
@@ -42,7 +25,7 @@ class OnyxStorageManager {
4225 * List of keys that are eligible for eviction based on the current config.
4326 * Only these keys will be considered for automatic cleanup.
4427 */
45- private evictableKeys : string [ ] = [ ] ;
28+ private evictableKeys : OnyxKey [ ] = [ ] ;
4629
4730 /**
4831 * Indicates whether the storage manager has been initialized.
@@ -111,35 +94,36 @@ class OnyxStorageManager {
11194 } ) ;
11295 }
11396
114- private initializeTracking ( ) : Promise < void | void [ ] > {
97+ private initializeTracking ( ) : Promise < void > {
11598 Logger . logInfo ( 'Initializing storage usage tracking...' ) ;
11699
117100 return Storage . getAllKeys ( )
118101 . then ( ( allKeys ) => {
119102 Logger . logInfo ( `Found ${ allKeys . length } keys in storage` ) ;
120103 return this . trackExistingKeys ( allKeys ) ;
121104 } )
105+ . then ( ( ) => undefined )
122106 . catch ( ( error ) => {
123107 Logger . logAlert ( `Failed to initialize storage tracking: ${ error } ` ) ;
124108 } ) ;
125109 }
126110
127- private trackExistingKeys ( keys : OnyxKey [ ] ) : Promise < void [ ] > {
128- const dataKeys = keys . filter ( ( key ) => ! key . startsWith ( '__onyx_meta_' ) ) ;
111+ private trackExistingKeys ( keys : OnyxKey [ ] ) : Promise < void > {
112+ const dataKeys = keys . filter ( ( key ) => ! key . startsWith ( METADATA_KEY_PREFIX ) ) ;
129113 const evictableDataKeys = dataKeys . filter ( ( key ) => this . isKeyEvictable ( key ) ) ;
130114 Logger . logInfo ( `[StorageManager] Filtering keys: ${ dataKeys . length } total, ${ evictableDataKeys . length } evictable` ) ;
131115
132116 const trackingPromises = evictableDataKeys . map ( ( key ) => this . initializeKeyTracking ( key ) ) ;
133117
134- return Promise . all ( trackingPromises ) ;
118+ return Promise . all ( trackingPromises ) . then ( ( ) => undefined ) ;
135119 }
136120
137121 private initializeKeyTracking ( key : OnyxKey ) : Promise < void > {
138122 const now = Date . now ( ) ;
139123
140124 return this . loadMetadata ( key )
141125 . then ( ( metadata ) => {
142- const keyInfo = {
126+ const keyInfo : StorageKeyInfo = {
143127 key,
144128 lastAccessed : metadata ?. lastAccessed ?? now ,
145129 createdAt : metadata ?. createdAt ?? now ,
@@ -181,7 +165,7 @@ class OnyxStorageManager {
181165
182166 private updateKeyInfo ( key : OnyxKey , now : number , isEvictable : boolean , isNewKey = false ) : void {
183167 const existing = this . keyInfoMap . get ( key ) ;
184- const keyInfo = {
168+ const keyInfo : StorageKeyInfo = {
185169 key,
186170 lastAccessed : now ,
187171 createdAt : isNewKey ? now : existing ?. createdAt || now ,
@@ -190,7 +174,7 @@ class OnyxStorageManager {
190174 this . keyInfoMap . set ( key , keyInfo ) ;
191175
192176 if ( isEvictable ) {
193- const metadata = {
177+ const metadata : StorageMetadata = {
194178 lastAccessed : now ,
195179 createdAt : keyInfo . createdAt ,
196180 } ;
@@ -225,17 +209,15 @@ class OnyxStorageManager {
225209 * Updates the list of keys that are eligible for eviction.
226210 * @param keys - Array of Onyx keys or collection keys
227211 */
228- setEvictableKeys ( keys : string [ ] ) : void {
212+ setEvictableKeys ( keys : OnyxKey [ ] ) : void {
229213 this . evictableKeys = keys ;
230214 }
231215
232216 private isKeyEvictable ( key : OnyxKey ) : boolean {
233217 return this . evictableKeys . some ( ( pattern ) => {
234- // If pattern ends with underscore, treat it as a prefix pattern
235218 if ( pattern . endsWith ( '_' ) ) {
236219 return key . startsWith ( pattern ) ;
237220 }
238- // Otherwise, exact match
239221 return key === pattern ;
240222 } ) ;
241223 }
0 commit comments