mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
Replace edit history in large collaborative state with fresh state once a document is idle with no open sessions, and when state is over the maximum length on save. Each replacement increments a state generation that is part of the collaboration room name, so old state never merges into the new state.
63 lines
1.9 KiB
TypeScript
63 lines
1.9 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 { buildDocumentName } from "@shared/collaboration/documentName";
|
|
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) => {
|
|
const stateGeneration =
|
|
stores.documents.get(node.id)?.stateGeneration ?? 0;
|
|
|
|
for (let generation = 0; generation <= stateGeneration; generation++) {
|
|
window.indexedDB.deleteDatabase(
|
|
buildDocumentName(node.id, generation)
|
|
);
|
|
}
|
|
});
|
|
})
|
|
);
|
|
}
|