mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix(workspace): leave workspace correctly (#5379)
This commit is contained in:
@@ -92,7 +92,7 @@ export class SyncEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
canGracefulStop() {
|
canGracefulStop() {
|
||||||
return !!this.status.local && this.status.local.pendingPushUpdates > 0;
|
return !!this.status.local && this.status.local.pendingPushUpdates === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async waitForGracefulStop(abort?: AbortSignal) {
|
async waitForGracefulStop(abort?: AbortSignal) {
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ export function createCloudAwarenessProvider(
|
|||||||
removeAwarenessStates(awareness, [awareness.clientID], 'window unload');
|
removeAwarenessStates(awareness, [awareness.clientID], 'window unload');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function handleConnect() {
|
||||||
|
socket.emit('client-handshake-awareness', workspaceId);
|
||||||
|
socket.emit('awareness-init', workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
connect: () => {
|
connect: () => {
|
||||||
socket.on('server-awareness-broadcast', awarenessBroadcast);
|
socket.on('server-awareness-broadcast', awarenessBroadcast);
|
||||||
@@ -86,16 +91,19 @@ export function createCloudAwarenessProvider(
|
|||||||
|
|
||||||
socket.connect();
|
socket.connect();
|
||||||
|
|
||||||
|
socket.on('connect', handleConnect);
|
||||||
|
|
||||||
socket.emit('client-handshake-awareness', workspaceId);
|
socket.emit('client-handshake-awareness', workspaceId);
|
||||||
socket.emit('awareness-init', workspaceId);
|
socket.emit('awareness-init', workspaceId);
|
||||||
},
|
},
|
||||||
disconnect: () => {
|
disconnect: () => {
|
||||||
|
removeAwarenessStates(awareness, [awareness.clientID], 'disconnect');
|
||||||
awareness.off('update', awarenessUpdate);
|
awareness.off('update', awarenessUpdate);
|
||||||
socket.emit('client-leave-awareness', workspaceId);
|
socket.emit('client-leave-awareness', workspaceId);
|
||||||
socket.off('server-awareness-broadcast', awarenessBroadcast);
|
socket.off('server-awareness-broadcast', awarenessBroadcast);
|
||||||
socket.off('new-client-awareness-init', newClientAwarenessInitHandler);
|
socket.off('new-client-awareness-init', newClientAwarenessInitHandler);
|
||||||
|
socket.off('connect', handleConnect);
|
||||||
window.removeEventListener('unload', windowBeforeUnloadHandler);
|
window.removeEventListener('unload', windowBeforeUnloadHandler);
|
||||||
socket.disconnect();
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,8 +51,7 @@ export function createAffineStorage(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: handle error
|
function handleConnect() {
|
||||||
socket.on('connect', () => {
|
|
||||||
socket.emit(
|
socket.emit(
|
||||||
'client-handshake-sync',
|
'client-handshake-sync',
|
||||||
workspaceId,
|
workspaceId,
|
||||||
@@ -62,10 +61,22 @@ export function createAffineStorage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
socket.on('connect', handleConnect);
|
||||||
|
|
||||||
socket.connect();
|
socket.connect();
|
||||||
|
|
||||||
|
socket.emit(
|
||||||
|
'client-handshake-sync',
|
||||||
|
workspaceId,
|
||||||
|
(response: { error?: any }) => {
|
||||||
|
if (!response.error) {
|
||||||
|
syncSender.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'affine-cloud',
|
name: 'affine-cloud',
|
||||||
async pull(docId, state) {
|
async pull(docId, state) {
|
||||||
@@ -124,14 +135,6 @@ export function createAffineStorage(
|
|||||||
await syncSender.send(docId, update);
|
await syncSender.send(docId, update);
|
||||||
},
|
},
|
||||||
async subscribe(cb, disconnect) {
|
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: {
|
const handleUpdate = async (message: {
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
guid: string;
|
guid: string;
|
||||||
@@ -157,7 +160,7 @@ export function createAffineStorage(
|
|||||||
disconnect() {
|
disconnect() {
|
||||||
syncSender.stop();
|
syncSender.stop();
|
||||||
socket.emit('client-leave-sync', workspaceId);
|
socket.emit('client-leave-sync', workspaceId);
|
||||||
socket.disconnect();
|
socket.off('connect', handleConnect);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user