import { IRoomOptions , Room } from 'room/room' ; import { ExtendedSocket } from 'extendedsocket' ; class export class Channel { param emptyRoom param channel private static onEmptyRoomCallback ( emptyRoom , channel ) { channel . rooms . splice ( channel . rooms . indexOf ( emptyRoom ) , 1 ) ; } public index : number ; public name : string ; public rooms : Room [ ] ; private nextRoomId : number ; private userConns : ExtendedSocket [ ] ; constructor ( index , name ) { this . index = index ; this . name = name ; this . rooms = [ ] ; this . nextRoomId = 1 ; this . userConns = [ ] ; } param id returns public getRoomById ( id ) { for ( const room of this . rooms ) { if ( room . id === id ) { return room ; } } return null ; } param hostUserId param hostConn param options returns public createRoom ( hostUserId , hostConn , options ? ) { const newRoom = new Room ( this . nextRoomId ++ , hostUserId , hostConn , this , ( emptyRoom , channel ) => { Channel . onEmptyRoomCallback ( emptyRoom , channel ) ; } , options ) ; this . rooms . push ( newRoom ) ; return this . rooms [ this . rooms . length - 1 ] ; } param roomId public removeRoomById ( roomId ) { for ( const room of this . rooms ) { if ( room . id === roomId ) { this . rooms . splice ( this . rooms . indexOf ( room ) , 1 ) ; return ; } } } param fn public recurseUsers ( fn ) { for ( const user of this . userConns ) { fn ( user ) ; } } param u public OnUserJoined ( u ) { this . userConns . push ( u ) ; } public OnUserLeft ( u ) { this . userConns . splice ( this . userConns . indexOf ( u ) , 1 ) ; } }