refactor: configure PeerRoom via constructor

This commit is contained in:
Jeremy Kahn 2022-08-14 17:29:24 -05:00
parent 4eb1d05b92
commit 2ec8c52f7c
2 changed files with 12 additions and 6 deletions

View File

@ -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()

View File

@ -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() {