From dffce96995dfd10f0e2c86041fa8af41d93543a5 Mon Sep 17 00:00:00 2001 From: dwelle Date: Sun, 1 Jan 2023 17:35:27 +0100 Subject: [PATCH] modify room init --- src/excalidraw-app/collab/Collab.tsx | 29 ++++++++-------------------- src/excalidraw-app/collab/Portal.tsx | 1 + 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/src/excalidraw-app/collab/Collab.tsx b/src/excalidraw-app/collab/Collab.tsx index d53d38341..9eac71f2d 100644 --- a/src/excalidraw-app/collab/Collab.tsx +++ b/src/excalidraw-app/collab/Collab.tsx @@ -257,13 +257,6 @@ class Collab extends PureComponent { ), ); - if (this.portal.socket && this.fallbackInitializationHandler) { - this.portal.socket.off( - "connect_error", - this.fallbackInitializationHandler, - ); - } - if (!keepRemoteState) { LocalData.fileStorage.reset(); this.destroySocketClient(); @@ -359,8 +352,6 @@ class Collab extends PureComponent { } }; - private fallbackInitializationHandler: null | (() => any) = null; - startCollaboration = async ( existingRoomLinkData: null | { roomId: string; roomKey: string }, ): Promise => { @@ -399,7 +390,6 @@ class Collab extends PureComponent { scenePromise.resolve(scene); }); }; - this.fallbackInitializationHandler = fallbackInitializationHandler; try { const socketServerData = await getCollabServer(); @@ -429,11 +419,11 @@ class Collab extends PureComponent { fallbackInitializationHandler(); }); - this.portal.socket.once("connect_error", (err: any) => { + this.portal.socket.on("connect_error", () => { + fallbackInitializationHandler(); this.setState({ errorMessage: t("errors.socketConnectionError"), }); - fallbackInitializationHandler(); }); } catch (error: any) { console.error(error); @@ -461,6 +451,7 @@ class Collab extends PureComponent { this.saveCollabRoomToFirebase(getSyncableElements(elements)); } + clearTimeout(this.socketInitializationTimer!); // fallback in case you're not alone in the room but still don't receive // initial SCENE_INIT message this.socketInitializationTimer = window.setTimeout( @@ -571,12 +562,12 @@ class Collab extends PureComponent { } | { fetchScene: false; roomLinkData?: null }) => { clearTimeout(this.socketInitializationTimer!); - if (this.portal.socket && this.fallbackInitializationHandler) { - this.portal.socket.off( - "connect_error", - this.fallbackInitializationHandler, - ); + + if (!this.portal.socket || this.portal.socketInitialized) { + return null; } + this.portal.socketInitialized = true; + if (fetchScene && roomLinkData && this.portal.socket) { this.excalidrawAPI.resetScene(); @@ -599,11 +590,7 @@ class Collab extends PureComponent { } catch (error: any) { // log the error and move on. other peers will sync us the scene. console.error(error); - } finally { - this.portal.socketInitialized = true; } - } else { - this.portal.socketInitialized = true; } return null; }; diff --git a/src/excalidraw-app/collab/Portal.tsx b/src/excalidraw-app/collab/Portal.tsx index eaff5fd65..b2f0a038c 100644 --- a/src/excalidraw-app/collab/Portal.tsx +++ b/src/excalidraw-app/collab/Portal.tsx @@ -64,6 +64,7 @@ class Portal { return; } this.queueFileUpload.flush(); + this.socket.off(); this.socket.close(); this.socket = null; this.roomId = null;