modify room init

This commit is contained in:
dwelle 2023-01-01 17:35:27 +01:00
parent 1c84872b31
commit dffce96995
2 changed files with 9 additions and 21 deletions

View File

@ -257,13 +257,6 @@ class Collab extends PureComponent<Props, CollabState> {
), ),
); );
if (this.portal.socket && this.fallbackInitializationHandler) {
this.portal.socket.off(
"connect_error",
this.fallbackInitializationHandler,
);
}
if (!keepRemoteState) { if (!keepRemoteState) {
LocalData.fileStorage.reset(); LocalData.fileStorage.reset();
this.destroySocketClient(); this.destroySocketClient();
@ -359,8 +352,6 @@ class Collab extends PureComponent<Props, CollabState> {
} }
}; };
private fallbackInitializationHandler: null | (() => any) = null;
startCollaboration = async ( startCollaboration = async (
existingRoomLinkData: null | { roomId: string; roomKey: string }, existingRoomLinkData: null | { roomId: string; roomKey: string },
): Promise<ImportedDataState | null> => { ): Promise<ImportedDataState | null> => {
@ -399,7 +390,6 @@ class Collab extends PureComponent<Props, CollabState> {
scenePromise.resolve(scene); scenePromise.resolve(scene);
}); });
}; };
this.fallbackInitializationHandler = fallbackInitializationHandler;
try { try {
const socketServerData = await getCollabServer(); const socketServerData = await getCollabServer();
@ -429,11 +419,11 @@ class Collab extends PureComponent<Props, CollabState> {
fallbackInitializationHandler(); fallbackInitializationHandler();
}); });
this.portal.socket.once("connect_error", (err: any) => { this.portal.socket.on("connect_error", () => {
fallbackInitializationHandler();
this.setState({ this.setState({
errorMessage: t("errors.socketConnectionError"), errorMessage: t("errors.socketConnectionError"),
}); });
fallbackInitializationHandler();
}); });
} catch (error: any) { } catch (error: any) {
console.error(error); console.error(error);
@ -461,6 +451,7 @@ class Collab extends PureComponent<Props, CollabState> {
this.saveCollabRoomToFirebase(getSyncableElements(elements)); this.saveCollabRoomToFirebase(getSyncableElements(elements));
} }
clearTimeout(this.socketInitializationTimer!);
// fallback in case you're not alone in the room but still don't receive // fallback in case you're not alone in the room but still don't receive
// initial SCENE_INIT message // initial SCENE_INIT message
this.socketInitializationTimer = window.setTimeout( this.socketInitializationTimer = window.setTimeout(
@ -571,12 +562,12 @@ class Collab extends PureComponent<Props, CollabState> {
} }
| { fetchScene: false; roomLinkData?: null }) => { | { fetchScene: false; roomLinkData?: null }) => {
clearTimeout(this.socketInitializationTimer!); clearTimeout(this.socketInitializationTimer!);
if (this.portal.socket && this.fallbackInitializationHandler) {
this.portal.socket.off( if (!this.portal.socket || this.portal.socketInitialized) {
"connect_error", return null;
this.fallbackInitializationHandler,
);
} }
this.portal.socketInitialized = true;
if (fetchScene && roomLinkData && this.portal.socket) { if (fetchScene && roomLinkData && this.portal.socket) {
this.excalidrawAPI.resetScene(); this.excalidrawAPI.resetScene();
@ -599,11 +590,7 @@ class Collab extends PureComponent<Props, CollabState> {
} catch (error: any) { } catch (error: any) {
// log the error and move on. other peers will sync us the scene. // log the error and move on. other peers will sync us the scene.
console.error(error); console.error(error);
} finally {
this.portal.socketInitialized = true;
} }
} else {
this.portal.socketInitialized = true;
} }
return null; return null;
}; };

View File

@ -64,6 +64,7 @@ class Portal {
return; return;
} }
this.queueFileUpload.flush(); this.queueFileUpload.flush();
this.socket.off();
this.socket.close(); this.socket.close();
this.socket = null; this.socket = null;
this.roomId = null; this.roomId = null;