From 42b90e1d8d61bd1ab2be937ea9606f895d7c0904 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Fri, 11 Aug 2023 02:09:33 -0400 Subject: [PATCH] fix: remove usage of `mergeUpdates` (#3687) --- apps/electron/package.json | 2 +- apps/web/package.json | 2 +- packages/component/package.json | 2 +- packages/storage/package.json | 4 +-- packages/workspace/package.json | 4 +-- packages/workspace/src/providers/index.ts | 23 +++++++++------ packages/y-indexeddb/src/utils.ts | 17 +++++++++++ yarn.lock | 36 +++++++++++++++-------- 8 files changed, 62 insertions(+), 28 deletions(-) diff --git a/apps/electron/package.json b/apps/electron/package.json index 306cf3944e..5ffeffa51f 100644 --- a/apps/electron/package.json +++ b/apps/electron/package.json @@ -64,7 +64,7 @@ "lodash-es": "^4.17.21", "nanoid": "^4.0.2", "rxjs": "^7.8.1", - "yjs": "^13.6.6" + "yjs": "^13.6.7" }, "build": { "protocols": [ diff --git a/apps/web/package.json b/apps/web/package.json index ab70a526af..dd26a73f81 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -54,7 +54,7 @@ "rxjs": "^7.8.1", "swr": "^2.1.5", "y-protocols": "^1.0.5", - "yjs": "^13.6.6", + "yjs": "^13.6.7", "zod": "^3.21.4" }, "devDependencies": { diff --git a/packages/component/package.json b/packages/component/package.json index 63d17726a2..43c6245d7f 100644 --- a/packages/component/package.json +++ b/packages/component/package.json @@ -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" } diff --git a/packages/storage/package.json b/packages/storage/package.json index 99afdf9302..542421bce0 100644 --- a/packages/storage/package.json +++ b/packages/storage/package.json @@ -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" } } diff --git a/packages/workspace/package.json b/packages/workspace/package.json index df10413911..33989dc053 100644 --- a/packages/workspace/package.json +++ b/packages/workspace/package.json @@ -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": { diff --git a/packages/workspace/src/providers/index.ts b/packages/workspace/src/providers/index.ts index 43ef66a3a1..c720f8978c 100644 --- a/packages/workspace/src/providers/index.ts +++ b/packages/workspace/src/providers/index.ts @@ -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 = new WeakMap(); +const cache: WeakMap = 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 { diff --git a/packages/y-indexeddb/src/utils.ts b/packages/y-indexeddb/src/utils.ts index 9258975145..06153aac5f 100644 --- a/packages/y-indexeddb/src/utils.ts +++ b/packages/y-indexeddb/src/utils.ts @@ -144,6 +144,23 @@ export async function downloadBinary( } } +export async function downloadBinaries( + guid: string, + dbName = DEFAULT_DB_NAME +): Promise { + const dbPromise = openDB(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'], diff --git a/yarn.lock b/yarn.lock index 90ee15681d..155b6a3d7f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -145,7 +145,7 @@ __metadata: rxjs: ^7.8.1 typescript: ^5.1.6 vite: ^4.3.9 - yjs: ^13.6.6 + yjs: ^13.6.7 peerDependencies: "@blocksuite/blocks": "*" "@blocksuite/editor": "*" @@ -258,7 +258,7 @@ __metadata: undici: ^5.22.1 uuid: ^9.0.0 which: ^3.0.1 - yjs: ^13.6.6 + yjs: ^13.6.7 zx: ^7.2.2 peerDependencies: ts-node: "*" @@ -461,8 +461,8 @@ __metadata: resolution: "@affine/storage@workspace:packages/storage" dependencies: "@napi-rs/cli": ^3.0.0-alpha.4 - lib0: ^0.2.78 - yjs: ^13.6.6 + lib0: ^0.2.80 + yjs: ^13.6.7 languageName: unknown linkType: soft @@ -590,7 +590,7 @@ __metadata: typescript: ^5.1.6 webpack: ^5.88.1 y-protocols: ^1.0.5 - yjs: ^13.6.6 + yjs: ^13.6.7 zod: ^3.21.4 languageName: unknown linkType: soft @@ -610,13 +610,13 @@ __metadata: jotai: ^2.2.2 js-base64: ^3.7.5 ky: ^0.33.3 - lib0: ^0.2.78 + lib0: ^0.2.80 next: =13.4.2 react: 18.2.0 react-dom: 18.2.0 ws: ^8.13.0 y-protocols: ^1.0.5 - yjs: ^13.6.6 + yjs: ^13.6.7 zod: ^3.21.4 peerDependencies: "@blocksuite/blocks": "*" @@ -22707,7 +22707,7 @@ __metadata: languageName: node linkType: hard -"lib0@npm:^0.2.42, lib0@npm:^0.2.74, lib0@npm:^0.2.78": +"lib0@npm:^0.2.42, lib0@npm:^0.2.74": version: 0.2.78 resolution: "lib0@npm:0.2.78" dependencies: @@ -22719,6 +22719,18 @@ __metadata: languageName: node linkType: hard +"lib0@npm:^0.2.80": + version: 0.2.80 + resolution: "lib0@npm:0.2.80" + dependencies: + isomorphic.js: ^0.2.4 + bin: + 0gentesthtml: bin/gentesthtml.js + 0serve: bin/0serve.js + checksum: eddb9c6cc23a916e3f6c26b3e729882c1f886de138b2de4055fb51a941acd7dab08eb65c9b9b43f01a5c83e0936169010e0c6e517d30446540019eebcb1ef6f6 + languageName: node + linkType: hard + "lie@npm:3.1.1": version: 3.1.1 resolution: "lie@npm:3.1.1" @@ -31509,12 +31521,12 @@ __metadata: languageName: node linkType: hard -"yjs@npm:^13.6.6": - version: 13.6.6 - resolution: "yjs@npm:13.6.6" +"yjs@npm:^13.6.7": + version: 13.6.7 + resolution: "yjs@npm:13.6.7" dependencies: lib0: ^0.2.74 - checksum: c69cb9a084aa433e813f6d0a191ebad5800a9a7098f7c6715952e4f8e5fc23270e3b8d7d747e1b0d0f1adca5f6efe01019654389eddb3977006814c4e2ff7ce6 + checksum: 8e89257c8b565ab97cf3354fca2ce0b6bc3d1abe90b9d45a218a94b35da372c88d2411b353ed8ca03a6619004c4da76c96f7c203c38506c3758c9f8c1a730ca4 languageName: node linkType: hard