Skip to content

Commit 3752ef9

Browse files
resolve comments
1 parent 7805973 commit 3752ef9

3 files changed

Lines changed: 34 additions & 49 deletions

File tree

lib/Onyx.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,7 @@ function set<TKey extends OnyxKey>(key: TKey, value: OnyxSetInput<TKey>, options
229229
return Storage.setItem(key, valueWithoutNestedNullValues)
230230
.catch((error) => OnyxUtils.evictStorageAndRetry(error, set, key, valueWithoutNestedNullValues))
231231
.then(() => {
232-
try {
233-
storageManager.trackKeySet(key);
234-
} catch (error) {
235-
Logger.logInfo(`Failed to track key set: ${error}`);
236-
}
237-
232+
storageManager.trackKeySet(key);
238233
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.SET, key, valueWithoutNestedNullValues);
239234
return updatePromise;
240235
});
@@ -286,14 +281,9 @@ function multiSet(data: OnyxMultiSetInput): Promise<void> {
286281
return Storage.multiSet(keyValuePairsToSet)
287282
.catch((error) => OnyxUtils.evictStorageAndRetry(error, multiSet, newData))
288283
.then(() => {
289-
try {
290-
keyValuePairsToSet.forEach(([key]) => {
291-
storageManager.trackKeySet(key);
292-
});
293-
} catch (error) {
294-
Logger.logInfo(`Failed to track multiset keys: ${error}`);
295-
}
296-
284+
keyValuePairsToSet.forEach(([key]) => {
285+
storageManager.trackKeySet(key);
286+
});
297287
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MULTI_SET, undefined, newData);
298288
return Promise.all(updatePromises);
299289
})
@@ -380,11 +370,7 @@ function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxMergeInput<TKey>):
380370
}
381371

382372
return OnyxMerge.applyMerge(key, existingValue, validChanges).then(({mergedValue, updatePromise}) => {
383-
try {
384-
storageManager.trackKeySet(key);
385-
} catch (error) {
386-
Logger.logInfo(`Failed to track merge key: ${error}`);
387-
}
373+
storageManager.trackKeySet(key);
388374
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, mergedValue);
389375
return updatePromise;
390376
});

lib/OnyxStorageManager/index.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,12 @@ import bindAll from 'lodash/bindAll';
22
import type {OnyxKey} from '../types';
33
import Storage from '../storage';
44
import * as Logger from '../Logger';
5-
import type {StorageUsageConfig, StorageKeyInfo, StorageMetadata} from './types';
5+
import type {StorageUsageConfig, StorageKeyInfo, StorageMetadata, StorageCleanupResult, CleanupExecutionResult} from './types';
66
import {DEFAULT_STORAGE_CONFIG} from './types';
77

88
const MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
99
const 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-
2811
class 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
}

lib/OnyxStorageManager/types.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ const DEFAULT_STORAGE_CONFIG: StorageUsageConfig = {
6969
evictableKeys: [],
7070
};
7171

72-
export type {StorageUsageConfig, StorageKeyInfo, StorageMetadata};
72+
/**
73+
* Represents the result of a storage cleanup operation.
74+
* @property cleanedKeys - An array of Onyx keys that were successfully cleaned/evicted.
75+
* @property timeElapsed - The time taken (in milliseconds) to perform the cleanup.
76+
* @property errors - An array of error messages encountered during the cleanup process.
77+
*/
78+
type StorageCleanupResult = {
79+
cleanedKeys: OnyxKey[];
80+
timeElapsed: number;
81+
errors: string[];
82+
};
83+
84+
type CleanupExecutionResult = {
85+
successfulKeys: OnyxKey[];
86+
failedKeys: OnyxKey[];
87+
};
88+
89+
export type {StorageUsageConfig, StorageKeyInfo, StorageMetadata, StorageCleanupResult, CleanupExecutionResult};
7390

7491
export {DEFAULT_STORAGE_CONFIG};

0 commit comments

Comments
 (0)