import { Injectable } from '@angular/core' ; import { ReplaySubject } from 'rxjs' ; import { List } from 'immutable' ; import { SocketService } from './socket.service' ; import { UserService } from './user.service' ; import { IRoom } from '../../models' ; @ Injectable ( ) export class RoomService { rooms : ReplaySubject < any > = new ReplaySubject ( 1 ) ; private list : IRoom [ ] = [ ] ; private socketService : SocketService ; constructor ( private userService ) { this . socketService = new SocketService ( 'room' ) ; this . socketService . items ( ) . subscribe ( rooms => { this . list = rooms ; this . rooms . next ( this . list ) ; } , error => console . log ( error ) ) ; this . socketService . list ( ) ; } join ( name ) { const matches = this . list . filter ( room => room . name === name ) ; const alreadyJoined = this . userService . rooms . filter ( room => room . name === name ) ; if ( matches [ 0 ] && ! alreadyJoined [ 0 ] ) { this . userService . rooms . push ( matches [ 0 ] ) ; } } leave ( name ) { this . userService . rooms = this . userService . rooms . filter ( room => room . name !== name ) ; } create ( name ) { this . socketService . create ( name ) ; } remove ( name ) { this . leave ( name ) ; this . socketService . remove ( name ) ; } }