Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,12 @@ function remove<TKey extends OnyxKey>(key: TKey, isProcessingCollectionUpdate?:

function reportStorageQuota(error?: Error): Promise<void> {
return Storage.getDatabaseSize()
.then(({bytesUsed, bytesRemaining}) => {
Logger.logInfo(`Storage Quota Check -- bytesUsed: ${bytesUsed} bytesRemaining: ${bytesRemaining}. Original error: ${error}`);
.then(({bytesUsed, bytesRemaining, usageDetails}) => {
Logger.logInfo(
`Storage Quota Check -- bytesUsed: ${bytesUsed} bytesRemaining: ${bytesRemaining}${
usageDetails ? ` usageDetails: ${JSON.stringify(usageDetails)}` : ''
}. Original error: ${error}`,
);
})
.catch((dbSizeError) => {
Logger.logAlert(`Unable to get database size. getDatabaseSize error: ${dbSizeError}. Original error: ${error}`);
Expand Down
1 change: 1 addition & 0 deletions lib/storage/providers/IDBKeyValProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const provider: StorageProvider<UseStore | undefined> = {
.then((value) => ({
bytesUsed: value.usage ?? 0,
bytesRemaining: (value.quota ?? 0) - (value.usage ?? 0),
usageDetails: (value as StorageEstimate & {usageDetails?: Record<string, number>}).usageDetails,
}))
.catch((error) => {
throw new Error(`Unable to estimate web storage quota. Original error: ${error}`);
Expand Down
1 change: 1 addition & 0 deletions lib/storage/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type StorageKeyList = OnyxKey[];
type DatabaseSize = {
bytesUsed: number;
bytesRemaining: number;
usageDetails?: Record<string, number>;
};

type OnStorageKeyChanged = <TKey extends OnyxKey>(key: TKey, value: OnyxValue<TKey>) => void;
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/onyxUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,19 @@ describe('OnyxUtils', () => {
expect(logInfoSpy).toHaveBeenCalledWith(`Storage Quota Check -- bytesUsed: 0 bytesRemaining: Infinity. Original error: ${diskFullError}`);
});

it('should include usageDetails in the storage quota log when available', async () => {
const logInfoSpy = jest.spyOn(Logger, 'logInfo');
const usageDetails = {caches: 1500160, fileSystem: 1369398, indexedDB: 10419711};
StorageMock.setItem = jest.fn().mockRejectedValue(diskFullError);
StorageMock.getDatabaseSize = jest.fn().mockResolvedValue({bytesUsed: 13289269, bytesRemaining: 5000000, usageDetails});

await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});

expect(logInfoSpy).toHaveBeenCalledWith(
`Storage Quota Check -- bytesUsed: 13289269 bytesRemaining: 5000000 usageDetails: ${JSON.stringify(usageDetails)}. Original error: ${diskFullError}`,
);
});

it('should include the error in logAlert when out of storage and getDatabaseSize fails', async () => {
const dbSizeError = new Error('Failed to estimate storage');
const logAlertSpy = jest.spyOn(Logger, 'logAlert');
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/storage/providers/IDBKeyvalProviderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe('IDBKeyValProvider', () => {
beforeEach(() => {
Object.defineProperty(window.navigator, 'storage', {
value: {
estimate: jest.fn().mockResolvedValue({quota: 750000, usage: 250000}),
estimate: jest.fn().mockResolvedValue({quota: 750000, usage: 250000, usageDetails: {caches: 100000, fileSystem: 50000, indexedDB: 100000}}),
},
configurable: true,
});
Expand All @@ -267,6 +267,7 @@ describe('IDBKeyValProvider', () => {
expect(await IDBKeyValProvider.getDatabaseSize()).toEqual({
bytesUsed: 250000,
bytesRemaining: 500000,
usageDetails: {caches: 100000, fileSystem: 50000, indexedDB: 100000},
});
});
});
Expand Down
Loading