From 855588ca8b7926eeaf578f814df7426ad0238e2c Mon Sep 17 00:00:00 2001 From: Himself65 Date: Thu, 2 Mar 2023 01:11:36 -0600 Subject: [PATCH] fix: reduce error message (#1254) --- apps/desktop/package.json | 2 +- .../providers/broad-cast-channel/index.ts | 50 +++++++++++++------ .../providers/broad-cast-channel/type.ts | 5 ++ packages/component/package.json | 2 +- packages/data-center/package.json | 2 +- pnpm-lock.yaml | 10 ++-- 6 files changed, 49 insertions(+), 22 deletions(-) diff --git a/apps/desktop/package.json b/apps/desktop/package.json index 7dda04a166..551c2412f0 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -30,7 +30,7 @@ "react-router": "^6.5.0", "react-router-dom": "^6.5.0", "y-protocols": "^1.0.5", - "yjs": "^13.5.46" + "yjs": "^13.5.47" }, "devDependencies": { "@tauri-apps/cli": "^1.2.3", diff --git a/apps/web/src/blocksuite/providers/broad-cast-channel/index.ts b/apps/web/src/blocksuite/providers/broad-cast-channel/index.ts index b646189651..6c23d3a95b 100644 --- a/apps/web/src/blocksuite/providers/broad-cast-channel/index.ts +++ b/apps/web/src/blocksuite/providers/broad-cast-channel/index.ts @@ -7,6 +7,7 @@ import { import { BlockSuiteWorkspace, BroadCastChannelProvider } from '../../../shared'; import { + AwarenessChanges, BroadcastChannelMessageEvent, getClients, TypedBroadcastChannel, @@ -27,13 +28,15 @@ export const createBroadCastChannelProvider = ( switch (eventName) { case 'doc:diff': { const [, diff, clientId] = event.data; - const updateV2 = Y.encodeStateAsUpdateV2(doc, diff); - broadcastChannel!.postMessage(['doc:update', updateV2, clientId]); + const update = Y.encodeStateAsUpdate(doc, diff); + broadcastChannel!.postMessage(['doc:update', update, clientId]); break; } case 'doc:update': { - const [, updateV2, clientId] = event.data; - Y.applyUpdateV2(doc, updateV2, clientId); + const [, update, clientId] = event.data; + if (!clientId || clientId === awareness.clientID) { + Y.applyUpdate(doc, update, broadcastChannel); + } break; } case 'awareness:query': { @@ -45,11 +48,31 @@ export const createBroadCastChannelProvider = ( } case 'awareness:update': { const [, update, clientId] = event.data; - applyAwarenessUpdate(awareness, update, clientId); + if (!clientId || clientId === awareness.clientID) { + applyAwarenessUpdate(awareness, update, broadcastChannel); + } break; } } }; + const handleDocUpdate = (updateV1: Uint8Array, origin: any) => { + if (origin === broadcastChannel) { + // not self update, ignore + return; + } + broadcastChannel?.postMessage(['doc:update', updateV1]); + }; + const handleAwarenessUpdate = (changes: AwarenessChanges, origin: any) => { + if (origin === broadcastChannel) { + return; + } + const changedClients = Object.values(changes).reduce((res, cur) => [ + ...res, + ...cur, + ]); + const update = encodeAwarenessUpdate(awareness, changedClients); + broadcastChannel?.postMessage(['awareness:update', update]); + }; return { flavour: 'broadcast-channel', connect: () => { @@ -60,31 +83,30 @@ export const createBroadCastChannelProvider = ( onmessage: handleBroadcastChannelMessage, } ); + console.log('connect broadcast channel', blockSuiteWorkspace.room); const docDiff = Y.encodeStateVector(doc); broadcastChannel.postMessage(['doc:diff', docDiff, awareness.clientID]); - const docUpdateV2 = Y.encodeStateAsUpdateV2(doc); + const docUpdateV2 = Y.encodeStateAsUpdate(doc); broadcastChannel.postMessage(['doc:update', docUpdateV2]); broadcastChannel.postMessage(['awareness:query', awareness.clientID]); const awarenessUpdate = encodeAwarenessUpdate(awareness, [ awareness.clientID, ]); broadcastChannel.postMessage(['awareness:update', awarenessUpdate]); - const handleDocUpdate = (updateV1: Uint8Array, origin: any) => { - if (origin !== awareness.clientID) { - // not self update, ignore - return; - } - const updateV2 = Y.convertUpdateFormatV1ToV2(updateV1); - broadcastChannel?.postMessage(['doc:update', updateV2]); - }; doc.on('update', handleDocUpdate); + awareness.on('update', handleAwarenessUpdate); }, disconnect: () => { assertExists(broadcastChannel); + console.log('disconnect broadcast channel', blockSuiteWorkspace.room); + doc.off('update', handleDocUpdate); + awareness.off('update', handleAwarenessUpdate); broadcastChannel.close(); }, cleanup: () => { assertExists(broadcastChannel); + doc.off('update', handleDocUpdate); + awareness.off('update', handleAwarenessUpdate); broadcastChannel.close(); }, }; diff --git a/apps/web/src/blocksuite/providers/broad-cast-channel/type.ts b/apps/web/src/blocksuite/providers/broad-cast-channel/type.ts index 9b84a66cdb..ecdbddff93 100644 --- a/apps/web/src/blocksuite/providers/broad-cast-channel/type.ts +++ b/apps/web/src/blocksuite/providers/broad-cast-channel/type.ts @@ -71,6 +71,11 @@ export type BroadcastChannelMessageData< export type BroadcastChannelMessageEvent = MessageEvent; +export type AwarenessChanges = Record< + 'added' | 'updated' | 'removed', + ClientId[] +>; + export interface TypedBroadcastChannel extends BroadcastChannel { onmessage: ((event: BroadcastChannelMessageEvent) => void) | null; postMessage: (message: BroadcastChannelMessageData) => void; diff --git a/packages/component/package.json b/packages/component/package.json index c6571f3b4e..43c7c65c5c 100644 --- a/packages/component/package.json +++ b/packages/component/package.json @@ -42,6 +42,6 @@ "typescript": "^4.9.5", "vite": "^4.1.2", "webpack": "^5.75.0", - "yjs": "^13.5.46" + "yjs": "^13.5.47" } } diff --git a/packages/data-center/package.json b/packages/data-center/package.json index fd49479c42..ce8fa03bf5 100644 --- a/packages/data-center/package.json +++ b/packages/data-center/package.json @@ -26,6 +26,6 @@ "lib0": "^0.2.62", "lit": "^2.6.1", "y-protocols": "^1.0.5", - "yjs": "^13.5.46" + "yjs": "^13.5.47" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ccfca27d0a..2daee71d5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -104,7 +104,7 @@ importers: typesync: ^0.10.0 vite: ^4.1.2 y-protocols: ^1.0.5 - yjs: ^13.5.46 + yjs: ^13.5.47 zx: ^7.1.1 dependencies: '@blocksuite/blocks': 0.4.1-20230220214107-0a354de_txcevohh7lsyxi5mbohonudlma @@ -263,7 +263,7 @@ importers: typescript: ^4.9.5 vite: ^4.1.2 webpack: ^5.75.0 - yjs: ^13.5.46 + yjs: ^13.5.47 dependencies: '@affine/debug': link:../debug '@affine/i18n': link:../i18n @@ -317,7 +317,7 @@ importers: lit: ^2.6.1 typescript: ^4.9.5 y-protocols: ^1.0.5 - yjs: ^13.5.46 + yjs: ^13.5.47 dependencies: '@affine/debug': link:../debug '@blocksuite/blocks': 0.4.1-20230220214107-0a354de_txcevohh7lsyxi5mbohonudlma @@ -10160,13 +10160,13 @@ packages: engines: {node: '>=14'} dependencies: isomorphic.js: 0.2.5 + dev: false /lib0/0.2.63: resolution: {integrity: sha512-JhUd/JXR4rnWsSP1zup904ACbVFcdRieWoIGwGAtHww4zms0gNyi8EM30Rfftnk+6A10qQMSufjLM0/ncq06xw==} engines: {node: '>=14'} dependencies: isomorphic.js: 0.2.5 - dev: false /lilconfig/2.0.6: resolution: {integrity: sha512-9JROoBW7pobfsx+Sq2JsASvCo6Pfo6WWoUW79HuB1BCoBXD4PLWJPqDF6fNj67pqBYTbAHkE57M1kS/+L1neOg==} @@ -14211,7 +14211,7 @@ packages: /yjs/13.5.47: resolution: {integrity: sha512-F7BZ+Bt36OAt+bdSQS7TN43KSxsHjfXWfcLC526tJ3mctO1FYHGEtOrUqpLC7pSp4jPn4rKSYHv1PVQcdb/vIQ==} dependencies: - lib0: 0.2.62 + lib0: 0.2.63 /yn/3.1.1: resolution: {integrity: sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==}