DBlobs: try to persist with the browser

This commit is contained in:
Enrico Ros
2024-06-13 16:24:06 -07:00
parent 6fc4dbe9d1
commit b9bff4abc0
3 changed files with 39 additions and 7 deletions
@@ -6,6 +6,7 @@ import { markNewsAsSeen, shallRedirectToNews } from '../../apps/news/news.versio
import { autoConfInitiateConfiguration } from '~/common/logic/autoconf';
import { navigateToNews, ROUTE_APP_CHAT } from '~/common/app.routes';
import { requestPersistentStorage } from '~/common/util/storageUtils';
import { useNextLoadProgress } from '~/common/components/useNextLoadProgress';
@@ -30,10 +31,13 @@ export function ProviderBootstrapLogic(props: { children: React.ReactNode }) {
// [gc] garbage collection(s)
React.useEffect(() => {
// Remove old attachment drafts (not persisted in chats)
// void gcAttachmentDBlobs(); // fire/forget
// Remove chat dblobs (not persisted in chat fragments)
void gcChatImageAssets(); // fire/forget
// Request persistent storage for the current origin, so that indexedDB's content is not evicted.
requestPersistentStorage().finally(() => {
// GC: Remove chat dblobs (not persisted in chat fragments)
void gcChatImageAssets(); // fire/forget
// GC: Remove old attachment drafts (not persisted in chats)
// void gcAttachmentDBlobs(); // fire/forget
});
}, []);
+26
View File
@@ -0,0 +1,26 @@
import { isBrowser } from '~/common/util/pwaUtils';
// enable debugging of the persistent storage
const DEBUG_PERSISTENCE = false;
/**
* Request persistent storage for the current origin, so that indexedDB's content is not evicted.
*/
export async function requestPersistentStorage(): Promise<boolean> {
if (isBrowser && navigator.storage && navigator.storage.persist) {
const isPersisted = await navigator.storage.persisted();
if (isPersisted) {
if (DEBUG_PERSISTENCE)
console.log('Storage is already persisted');
return true;
}
const isGranted = await navigator.storage.persist();
if (DEBUG_PERSISTENCE)
console.log(`Persistent storage granted: ${isGranted}`);
return isGranted;
}
console.warn('Persistent storage is not supported by this browser');
return false;
}
+5 -3
View File
@@ -9,6 +9,10 @@ import { DBlobAsset, DBlobAssetId, DBlobAssetType, DBlobDBAsset } from './dblobs
*
* [DEV NOTE] To delete the full DB (don't do it!):
* - indexedDB.deleteDatabase('NAME').onsuccess = console.log;
*
* [DEV NOTE] This is related to storageUtils.ts's requestPersistentStorage,
* aswe need to request persistent storage for the current origin, sot that
* indexedDB's content is not evicted.
*/
class BigAgiDB extends Dexie {
largeAssets!: Dexie.Table<DBlobDBAsset, string>;
@@ -23,9 +27,7 @@ class BigAgiDB extends Dexie {
}
}
/**
* In development mode, reuse the same instance of the DB to avoid re-creating it on every hot reload
*/
// In development mode, reuse the same instance of the DB to avoid re-creating it on every hot reload
const globalForDexie = globalThis as unknown as {
bigAgiDB: BigAgiDB | undefined;
};