From 2ec8c52f7c5140baaa4569e8e2d929bfbbe959e3 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sun, 14 Aug 2022 17:29:24 -0500 Subject: [PATCH] refactor: configure PeerRoom via constructor --- src/hooks/usePeerRoom/usePeerRoom.ts | 6 +++--- src/services/PeerRoom/PeerRoom.ts | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/hooks/usePeerRoom/usePeerRoom.ts b/src/hooks/usePeerRoom/usePeerRoom.ts index de0b4af..e8ad7a9 100644 --- a/src/hooks/usePeerRoom/usePeerRoom.ts +++ b/src/hooks/usePeerRoom/usePeerRoom.ts @@ -9,11 +9,11 @@ interface PeerRoomProps { export function usePeerRoom({ appId, roomId }: PeerRoomProps) { const peerRoom = useMemo(() => { - return new PeerRoom() - }, []) + return new PeerRoom({ appId }) + }, [appId]) useEffect(() => { - peerRoom.joinRoom(appId, roomId) + peerRoom.joinRoom(roomId) return () => { peerRoom.leaveRoom() diff --git a/src/services/PeerRoom/PeerRoom.ts b/src/services/PeerRoom/PeerRoom.ts index e84bc02..6cf9586 100644 --- a/src/services/PeerRoom/PeerRoom.ts +++ b/src/services/PeerRoom/PeerRoom.ts @@ -1,10 +1,16 @@ -import { joinRoom, Room } from 'trystero' +import { joinRoom, Room, RoomConfig } from 'trystero' export class PeerRoom { private room?: Room - joinRoom(appId: string, roomId: string) { - this.room = joinRoom({ appId }, roomId) + private roomConfig: RoomConfig + + constructor(config: RoomConfig) { + this.roomConfig = config + } + + joinRoom(roomId: string) { + this.room = joinRoom(this.roomConfig, roomId) } leaveRoom() {