2019-03-08 19:58:54 +01:00
|
|
|
import EventEmitter from "../../../EventEmitter.js";
|
2019-06-01 18:29:23 +02:00
|
|
|
import TimelineViewModel from "./timeline/TimelineViewModel.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
|
|
|
export default class RoomViewModel extends EventEmitter {
|
|
|
|
constructor(room) {
|
|
|
|
super();
|
|
|
|
this._room = room;
|
|
|
|
this._timeline = null;
|
2019-06-01 18:29:23 +02:00
|
|
|
this._timelineVM = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
this._onRoomChange = this._onRoomChange.bind(this);
|
2019-03-09 00:43:43 +01:00
|
|
|
this._timelineError = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async enable() {
|
|
|
|
this._room.on("change", this._onRoomChange);
|
2019-03-09 00:43:43 +01:00
|
|
|
try {
|
|
|
|
this._timeline = await this._room.openTimeline();
|
2019-06-01 18:29:23 +02:00
|
|
|
this._timelineVM = new TimelineViewModel(this._timeline);
|
2019-06-02 14:59:30 +02:00
|
|
|
this.emit("change", "timelineViewModel");
|
2019-03-09 00:43:43 +01:00
|
|
|
} catch (err) {
|
2019-06-02 14:59:30 +02:00
|
|
|
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);
|
2019-03-09 00:43:43 +01:00
|
|
|
this._timelineError = err;
|
|
|
|
this.emit("change", "error");
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
if (this._timeline) {
|
2019-06-01 18:29:23 +02:00
|
|
|
// will stop the timeline from delivering updates on entries
|
2019-02-27 22:50:08 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:29:23 +02:00
|
|
|
get timelineViewModel() {
|
|
|
|
return this._timelineVM;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
2019-03-09 00:43:43 +01:00
|
|
|
|
|
|
|
get error() {
|
|
|
|
if (this._timelineError) {
|
|
|
|
return `Something went wrong loading the timeline: ${this._timelineError.message}`;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|