feat(y-indexeddb): remove id (#2810)

This commit is contained in:
Alex Yang
2023-06-17 13:58:48 +08:00
committed by GitHub
parent deeafb3a12
commit c68220166a
6 changed files with 68 additions and 68 deletions

View File

@@ -148,10 +148,16 @@ type SubDocsEvent = {
loaded: Set<Doc>;
};
/**
* We use `doc.guid` as the unique key, please make sure it not changes.
*/
export const createIndexedDBProvider = (
id: string,
doc: Doc,
dbName: string = DEFAULT_DB_NAME
dbName: string = DEFAULT_DB_NAME,
/**
* In the future, migrate will be removed and there will be a separate function
*/
migrate = true
): IndexedDBProvider => {
let resolve: () => void;
let reject: (reason?: unknown) => void;
@@ -336,15 +342,21 @@ export const createIndexedDBProvider = (
reject = _reject;
});
connected = true;
trackDoc(id, doc);
trackDoc(doc.guid, doc);
// only the runs `await` below, otherwise the logic is incorrect
const db = await dbPromise;
await tryMigrate(db, id, dbName);
if (migrate) {
// Tips:
// this is only backward compatible with the yjs official version of y-indexeddb
await tryMigrate(db, doc.guid, dbName);
}
if (!connected) {
return;
}
// recursively save all docs into indexeddb
const docs: [string, Doc][] = [];
docs.push([id, doc]);
docs.push([doc.guid, doc]);
while (docs.length > 0) {
const [id, doc] = docs.pop() as [string, Doc];
await saveDocOperation(id, doc);
@@ -361,13 +373,13 @@ export const createIndexedDBProvider = (
if (early) {
reject(new EarlyDisconnectError());
}
unTrackDoc(id, doc);
unTrackDoc(doc.guid, doc);
},
async cleanup() {
if (connected) {
throw new CleanupWhenConnectingError();
}
await (await dbPromise).delete('workspace', id);
await (await dbPromise).delete('workspace', doc.guid);
},
whenSynced: Promise.resolve(),
get connected() {