fix: remove usage of mergeUpdates (#3687)

This commit is contained in:
Alex Yang
2023-08-11 02:09:33 -04:00
committed by GitHub
parent 52f01a71d2
commit 42b90e1d8d
8 changed files with 62 additions and 28 deletions
+1 -1
View File
@@ -64,7 +64,7 @@
"@vanilla-extract/css": "^1.12.0",
"typescript": "^5.1.6",
"vite": "^4.3.9",
"yjs": "^13.6.6"
"yjs": "^13.6.7"
},
"version": "0.7.1"
}
+2 -2
View File
@@ -37,7 +37,7 @@
},
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.4",
"lib0": "^0.2.78",
"yjs": "^13.6.6"
"lib0": "^0.2.80",
"yjs": "^13.6.7"
}
}
+2 -2
View File
@@ -24,11 +24,11 @@
"jotai": "^2.2.2",
"js-base64": "^3.7.5",
"ky": "^0.33.3",
"lib0": "^0.2.78",
"lib0": "^0.2.80",
"react": "18.2.0",
"react-dom": "18.2.0",
"y-protocols": "^1.0.5",
"yjs": "^13.6.6",
"yjs": "^13.6.7",
"zod": "^3.21.4"
},
"devDependencies": {
+14 -9
View File
@@ -9,7 +9,7 @@ import { Workspace } from '@blocksuite/store';
import { createBroadcastChannelProvider } from '@blocksuite/store/providers/broadcast-channel';
import {
createIndexedDBProvider as create,
downloadBinary,
downloadBinaries,
EarlyDisconnectError,
} from '@toeverything/y-indexeddb';
import type { Doc } from 'yjs';
@@ -61,7 +61,7 @@ const createIndexedDBBackgroundProvider: DocProviderCreator = (
};
};
const cache: WeakMap<Doc, Uint8Array> = new WeakMap();
const cache: WeakMap<Doc, Uint8Array[]> = new WeakMap();
const createIndexedDBDownloadProvider: DocProviderCreator = (
id,
@@ -75,15 +75,20 @@ const createIndexedDBDownloadProvider: DocProviderCreator = (
});
async function downloadBinaryRecursively(doc: Doc) {
if (cache.has(doc)) {
const binary = cache.get(doc) as Uint8Array;
Y.applyUpdate(doc, binary);
} else {
const binary = await downloadBinary(doc.guid);
if (binary) {
const binaries = cache.get(doc) as Uint8Array[];
binaries.forEach(binary => {
Y.applyUpdate(doc, binary);
cache.set(doc, binary);
}
});
return;
}
const binaries = await downloadBinaries(doc.guid);
if (!binaries) {
return;
}
cache.set(doc, binaries);
binaries.forEach(binary => {
Y.applyUpdate(doc, binary);
});
await Promise.all([...doc.subdocs].map(downloadBinaryRecursively));
}
return {
+17
View File
@@ -144,6 +144,23 @@ export async function downloadBinary(
}
}
export async function downloadBinaries(
guid: string,
dbName = DEFAULT_DB_NAME
): Promise<UpdateMessage['update'][] | false> {
const dbPromise = openDB<BlockSuiteBinaryDB>(dbName, dbVersion, {
upgrade: upgradeDB,
});
const db = await dbPromise;
const t = db.transaction('workspace', 'readonly').objectStore('workspace');
const doc = await t.get(guid);
if (!doc) {
return false;
} else {
return doc.updates.map(({ update }) => update);
}
}
export async function overwriteBinary(
guid: string,
update: UpdateMessage['update'],