2020-04-20 21:26:39 +02:00
|
|
|
import {RoomTileViewModel} from "./roomlist/RoomTileViewModel.js";
|
|
|
|
import {RoomViewModel} from "./room/RoomViewModel.js";
|
|
|
|
import {SyncStatusViewModel} from "./SyncStatusViewModel.js";
|
2020-05-04 19:23:11 +02:00
|
|
|
import {ViewModel} from "../ViewModel.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
2020-05-04 19:23:11 +02:00
|
|
|
export class SessionViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const sessionContainer = options.sessionContainer;
|
2020-04-22 20:52:56 +02:00
|
|
|
this._session = sessionContainer.session;
|
2020-05-05 23:16:51 +02:00
|
|
|
this._sessionStatusViewModel = this.track(new SessionStatusViewModel(this.childOptions({
|
|
|
|
syncStatus: sessionContainer.sync.status,
|
|
|
|
reconnector: sessionContainer.reconnector
|
|
|
|
})));
|
2019-02-27 22:50:08 +01:00
|
|
|
this._currentRoomViewModel = null;
|
|
|
|
const roomTileVMs = this._session.rooms.mapValues((room, emitUpdate) => {
|
2019-06-26 23:14:39 +02:00
|
|
|
return new RoomTileViewModel({
|
|
|
|
room,
|
|
|
|
emitUpdate,
|
|
|
|
emitOpen: room => this._openRoom(room)
|
2019-02-27 22:50:08 +01:00
|
|
|
});
|
2019-06-26 23:14:39 +02:00
|
|
|
});
|
2019-02-27 22:50:08 +01:00
|
|
|
this._roomList = roomTileVMs.sortValues((a, b) => a.compare(b));
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
start() {
|
|
|
|
this._sessionStatusViewModel.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
get sessionStatusViewModel() {
|
|
|
|
return this._sessionStatusViewModel;
|
2019-06-16 10:54:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
get roomList() {
|
|
|
|
return this._roomList;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentRoom() {
|
|
|
|
return this._currentRoomViewModel;
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:19:49 +02:00
|
|
|
dispose() {
|
|
|
|
if (this._currentRoomViewModel) {
|
|
|
|
this._currentRoomViewModel.dispose();
|
|
|
|
this._currentRoomViewModel = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:14:39 +02:00
|
|
|
_closeCurrentRoom() {
|
|
|
|
if (this._currentRoomViewModel) {
|
|
|
|
this._currentRoomViewModel.dispose();
|
|
|
|
this._currentRoomViewModel = null;
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("currentRoom");
|
2019-06-26 23:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
_openRoom(room) {
|
|
|
|
if (this._currentRoomViewModel) {
|
2019-06-26 23:14:39 +02:00
|
|
|
this._currentRoomViewModel.dispose();
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
2020-05-04 19:23:11 +02:00
|
|
|
this._currentRoomViewModel = new RoomViewModel(this.childOptions({
|
2019-06-26 23:14:39 +02:00
|
|
|
room,
|
2019-07-29 10:23:15 +02:00
|
|
|
ownUserId: this._session.user.id,
|
2019-06-26 23:14:39 +02:00
|
|
|
closeCallback: () => this._closeCurrentRoom(),
|
2020-05-04 19:23:11 +02:00
|
|
|
}));
|
2019-06-26 23:14:39 +02:00
|
|
|
this._currentRoomViewModel.load();
|
2020-05-04 19:23:11 +02:00
|
|
|
this.emitChange("currentRoom");
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|