fix(workspace): leave workspace correctly (#5379)

This commit is contained in:
EYHN 2023-12-22 16:17:59 +08:00 committed by GitHub
parent 1ce4dd0497
commit 77d239ff81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 14 deletions

View File

@ -92,7 +92,7 @@ export class SyncEngine {
}
canGracefulStop() {
return !!this.status.local && this.status.local.pendingPushUpdates > 0;
return !!this.status.local && this.status.local.pendingPushUpdates === 0;
}
async waitForGracefulStop(abort?: AbortSignal) {

View File

@ -76,6 +76,11 @@ export function createCloudAwarenessProvider(
removeAwarenessStates(awareness, [awareness.clientID], 'window unload');
};
function handleConnect() {
socket.emit('client-handshake-awareness', workspaceId);
socket.emit('awareness-init', workspaceId);
}
return {
connect: () => {
socket.on('server-awareness-broadcast', awarenessBroadcast);
@ -86,16 +91,19 @@ export function createCloudAwarenessProvider(
socket.connect();
socket.on('connect', handleConnect);
socket.emit('client-handshake-awareness', workspaceId);
socket.emit('awareness-init', workspaceId);
},
disconnect: () => {
removeAwarenessStates(awareness, [awareness.clientID], 'disconnect');
awareness.off('update', awarenessUpdate);
socket.emit('client-leave-awareness', workspaceId);
socket.off('server-awareness-broadcast', awarenessBroadcast);
socket.off('new-client-awareness-init', newClientAwarenessInitHandler);
socket.off('connect', handleConnect);
window.removeEventListener('unload', windowBeforeUnloadHandler);
socket.disconnect();
},
};
}

View File

@ -51,8 +51,7 @@ export function createAffineStorage(
});
});
// TODO: handle error
socket.on('connect', () => {
function handleConnect() {
socket.emit(
'client-handshake-sync',
workspaceId,
@ -62,10 +61,22 @@ export function createAffineStorage(
}
}
);
});
}
socket.on('connect', handleConnect);
socket.connect();
socket.emit(
'client-handshake-sync',
workspaceId,
(response: { error?: any }) => {
if (!response.error) {
syncSender.start();
}
}
);
return {
name: 'affine-cloud',
async pull(docId, state) {
@ -124,14 +135,6 @@ export function createAffineStorage(
await syncSender.send(docId, update);
},
async subscribe(cb, disconnect) {
const response: { error?: any } = await socket
.timeout(10000)
.emitWithAck('client-handshake-sync', workspaceId);
if (response.error) {
throw new Error('client-handshake error, ' + response.error);
}
const handleUpdate = async (message: {
workspaceId: string;
guid: string;
@ -157,7 +160,7 @@ export function createAffineStorage(
disconnect() {
syncSender.stop();
socket.emit('client-leave-sync', workspaceId);
socket.disconnect();
socket.off('connect', handleConnect);
},
};
}