vector-im-hydrogen-web/src/domain/session/room/RoomViewModel.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-03-08 19:58:54 +01:00
import EventEmitter from "../../../EventEmitter.js";
import TimelineViewModel from "./timeline/TimelineViewModel.js";
import {avatarInitials} from "../avatar.js";
export default class RoomViewModel extends EventEmitter {
2019-06-16 10:53:23 +02:00
constructor(room, ownUserId) {
super();
this._room = room;
2019-06-16 10:53:23 +02:00
this._ownUserId = ownUserId;
this._timeline = null;
this._timelineVM = null;
this._onRoomChange = this._onRoomChange.bind(this);
this._timelineError = null;
}
async enable() {
this._room.on("change", this._onRoomChange);
try {
this._timeline = await this._room.openTimeline();
2019-06-16 10:53:23 +02:00
this._timelineVM = new TimelineViewModel(this._timeline, this._ownUserId);
2019-06-02 14:59:30 +02:00
this.emit("change", "timelineViewModel");
} catch (err) {
2019-06-02 14:59:30 +02:00
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);
this._timelineError = err;
this.emit("change", "error");
}
}
disable() {
if (this._timeline) {
// will stop the timeline from delivering updates on entries
this._timeline.close();
}
}
// room doesn't tell us yet which fields changed,
// so emit all fields originating from summary
_onRoomChange() {
this.emit("change", "name");
}
get name() {
return this._room.name;
}
get timelineViewModel() {
return this._timelineVM;
}
get error() {
if (this._timelineError) {
return `Something went wrong loading the timeline: ${this._timelineError.message}`;
}
return "";
}
get avatarInitials() {
return avatarInitials(this._room.name);
}
}