30 lines
567 B
TypeScript
Raw Normal View History

import { joinRoom, Room, RoomConfig } from 'trystero'
2022-08-13 12:11:59 -05:00
export class PeerRoom {
2022-08-14 13:21:34 -05:00
private room?: Room
2022-08-13 12:11:59 -05:00
private roomConfig: RoomConfig
constructor(config: RoomConfig) {
this.roomConfig = config
}
2022-08-14 21:26:50 -05:00
joinRoom = (roomId: string) => {
this.room = joinRoom(this.roomConfig, roomId)
2022-08-13 12:11:59 -05:00
}
2022-08-14 21:26:50 -05:00
leaveRoom = () => {
2022-08-13 12:11:59 -05:00
if (this.room) {
this.room.leave()
}
}
2022-08-14 17:49:31 -05:00
2022-08-14 21:26:50 -05:00
makeAction = <T>(namespace: string) => {
if (!this.room) {
throw new Error('PeerRoom: Called makeAction before joinRoom')
}
return this.room.makeAction<T>(namespace)
2022-08-14 17:49:31 -05:00
}
2022-08-13 12:11:59 -05:00
}