mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* fix: Recover from closed IndexedDB connections in store persistence StorePersistence cached a single IDBDatabase forever, so once the browser or another tab closed the connection every subsequent hydrate, flush and clear threw against a dead handle. - Close and forget the connection on versionchange, so another tab is never blocked from deleting or upgrading the database - Discard the cached connection on close, open error and blocked, and retry once against a fresh connection when one was closed underneath us - Close open connections before deleteAllDatabases removes them, which also stops logout recreating the databases it just deleted Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix: Cancel pending writes when store persistence is disabled A debounced flush scheduled before disabling would still fire, reopening and recreating the database that deleteAllDatabases had just removed.
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { flatten } from "es-toolkit/compat";
|
|
import stores from "~/stores";
|
|
import Store from "~/stores/base/Store";
|
|
import StorePersistence from "~/stores/base/StorePersistence";
|
|
import { flattenTree } from "@shared/utils/tree";
|
|
|
|
/**
|
|
* Delete all databases in the browser.
|
|
*
|
|
* @returns A promise that resolves when all databases have been deleted.
|
|
*/
|
|
export async function deleteAllDatabases() {
|
|
if (!window.indexedDB) {
|
|
return;
|
|
}
|
|
|
|
// Close any connections this tab holds open, otherwise the deletions below
|
|
// are blocked until they are, and in-flight reads and writes are aborted.
|
|
stores.disablePersistence();
|
|
|
|
const teamId = stores.auth.currentTeamId;
|
|
if (teamId) {
|
|
Object.values(stores).forEach((store) => {
|
|
if (store instanceof Store && store.persistable) {
|
|
window.indexedDB.deleteDatabase(
|
|
StorePersistence.databaseName(store.apiEndpoint, teamId)
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
if ("databases" in window.indexedDB) {
|
|
const databases = await window.indexedDB.databases();
|
|
|
|
for (const database of databases) {
|
|
if (database.name) {
|
|
window.indexedDB.deleteDatabase(database.name);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// If the browser does not support listing databases, we need to manually delete as best we can
|
|
// by iterating over all known collections and documents.
|
|
await Promise.all(
|
|
stores.collections.orderedData.map(async (collection) => {
|
|
const nodes = flatten(collection.documents?.map(flattenTree));
|
|
|
|
return nodes.map(async (node) => {
|
|
window.indexedDB.deleteDatabase(`document.${node.id}`);
|
|
});
|
|
})
|
|
);
|
|
}
|