From 9a564e9844e220cac33b01a58e2740e5218048f1 Mon Sep 17 00:00:00 2001 From: Florens Douwes Date: Sat, 25 Apr 2020 15:46:02 +0200 Subject: [PATCH] connection: use other method of connected/disconnected hook --- frontend/src/lib/Connection.ts | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/frontend/src/lib/Connection.ts b/frontend/src/lib/Connection.ts index 7ffa493..ae795fc 100644 --- a/frontend/src/lib/Connection.ts +++ b/frontend/src/lib/Connection.ts @@ -10,6 +10,8 @@ class Connection { socket: SocketIOClient.Socket; + isConnected = new Sub(); + config = new Sub(); room = new Sub(); timeline = new Sub(); @@ -23,6 +25,8 @@ class Connection { calls: { [id: number]: Call } = {}; constructor() { + this.isConnected.set(false); + this.socket = io(this.url, { autoConnect: false, transports: ['websocket'] @@ -41,10 +45,12 @@ class Connection { setupSocketListeners() { this.socket.on('connect', () => { + this.isConnected.set(true); this.onConnect(); }) this.socket.on('disconnect', () => { + this.isConnected.set(false); this.onDisconnect(); }) @@ -314,21 +320,6 @@ export function roomTime(): number { return (connection.serverTime() - room.startTime) * room.speedFactor; } -export function useIsConnected() { - const [isConnected, setIsConnected] = useState(connection.socket.connected); - - useEffect(() => { - let connectListener = () => setIsConnected(true); - let disconnectListener = () => setIsConnected(false); - - connection.socket.on('connect', connectListener); - connection.socket.on('disconnect', disconnectListener); - - return () => { - connection.socket.off('connect', connectListener); - connection.socket.off('disconnect', disconnectListener); - } - }, [isConnected]); - - return isConnected; +export function useIsConnected(): boolean { + return useSub(connection.isConnected); }