2019-02-27 22:50:08 +01:00
|
|
|
import EventEmitter from "../../EventEmitter.js";
|
2019-03-08 19:58:54 +01:00
|
|
|
import RoomTileViewModel from "./roomlist/RoomTileViewModel.js";
|
|
|
|
import RoomViewModel from "./room/RoomViewModel.js";
|
2019-06-16 10:54:16 +02:00
|
|
|
import SyncStatusViewModel from "./SyncStatusViewModel.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
|
|
|
export default class SessionViewModel extends EventEmitter {
|
2019-06-16 10:54:16 +02:00
|
|
|
constructor(session, sync) {
|
2019-02-27 22:50:08 +01:00
|
|
|
super();
|
|
|
|
this._session = session;
|
2019-06-16 10:54:16 +02:00
|
|
|
this._syncStatusViewModel = new SyncStatusViewModel(sync);
|
2019-02-27 22:50:08 +01:00
|
|
|
this._currentRoomViewModel = null;
|
|
|
|
const roomTileVMs = this._session.rooms.mapValues((room, emitUpdate) => {
|
|
|
|
return new RoomTileViewModel({
|
|
|
|
room,
|
|
|
|
emitUpdate,
|
|
|
|
emitOpen: room => this._openRoom(room)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this._roomList = roomTileVMs.sortValues((a, b) => a.compare(b));
|
|
|
|
}
|
|
|
|
|
2019-06-16 10:54:16 +02:00
|
|
|
get syncStatusViewModel() {
|
|
|
|
return this._syncStatusViewModel;
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
get roomList() {
|
|
|
|
return this._roomList;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentRoom() {
|
|
|
|
return this._currentRoomViewModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
_openRoom(room) {
|
|
|
|
if (this._currentRoomViewModel) {
|
|
|
|
this._currentRoomViewModel.disable();
|
|
|
|
}
|
2019-06-16 10:53:23 +02:00
|
|
|
this._currentRoomViewModel = new RoomViewModel(room, this._session.userId);
|
2019-02-27 22:50:08 +01:00
|
|
|
this._currentRoomViewModel.enable();
|
|
|
|
this.emit("change", "currentRoom");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|