fix: reduce error message (#1254)

This commit is contained in:
Himself65 2023-03-02 01:11:36 -06:00 committed by GitHub
parent 68b33cbdbd
commit 855588ca8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 22 deletions

View File

@ -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",

View File

@ -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();
},
};

View File

@ -71,6 +71,11 @@ export type BroadcastChannelMessageData<
export type BroadcastChannelMessageEvent =
MessageEvent<BroadcastChannelMessageData>;
export type AwarenessChanges = Record<
'added' | 'updated' | 'removed',
ClientId[]
>;
export interface TypedBroadcastChannel extends BroadcastChannel {
onmessage: ((event: BroadcastChannelMessageEvent) => void) | null;
postMessage: (message: BroadcastChannelMessageData) => void;

View File

@ -42,6 +42,6 @@
"typescript": "^4.9.5",
"vite": "^4.1.2",
"webpack": "^5.75.0",
"yjs": "^13.5.46"
"yjs": "^13.5.47"
}
}

View File

@ -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"
}
}

View File

@ -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==}