2019-03-09 00:28:54 +05:30
|
|
|
import EventEmitter from "../../../EventEmitter.js";
|
2019-06-01 21:59:23 +05:30
|
|
|
import TimelineViewModel from "./timeline/TimelineViewModel.js";
|
2019-06-16 18:51:20 +05:30
|
|
|
import {avatarInitials} from "../avatar.js";
|
2019-02-28 03:20:08 +05:30
|
|
|
|
|
|
|
export default class RoomViewModel extends EventEmitter {
|
2019-06-27 02:44:39 +05:30
|
|
|
constructor({room, ownUserId, closeCallback}) {
|
2019-02-28 03:20:08 +05:30
|
|
|
super();
|
|
|
|
this._room = room;
|
2019-06-16 14:23:23 +05:30
|
|
|
this._ownUserId = ownUserId;
|
2019-02-28 03:20:08 +05:30
|
|
|
this._timeline = null;
|
2019-06-01 21:59:23 +05:30
|
|
|
this._timelineVM = null;
|
2019-02-28 03:20:08 +05:30
|
|
|
this._onRoomChange = this._onRoomChange.bind(this);
|
2019-03-09 05:13:43 +05:30
|
|
|
this._timelineError = null;
|
2019-06-27 02:44:39 +05:30
|
|
|
this._closeCallback = closeCallback;
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2019-06-27 02:44:39 +05:30
|
|
|
async load() {
|
2019-02-28 03:20:08 +05:30
|
|
|
this._room.on("change", this._onRoomChange);
|
2019-03-09 05:13:43 +05:30
|
|
|
try {
|
|
|
|
this._timeline = await this._room.openTimeline();
|
2019-06-16 14:23:23 +05:30
|
|
|
this._timelineVM = new TimelineViewModel(this._timeline, this._ownUserId);
|
2019-06-02 18:29:30 +05:30
|
|
|
this.emit("change", "timelineViewModel");
|
2019-03-09 05:13:43 +05:30
|
|
|
} catch (err) {
|
2019-06-02 18:29:30 +05:30
|
|
|
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);
|
2019-03-09 05:13:43 +05:30
|
|
|
this._timelineError = err;
|
|
|
|
this.emit("change", "error");
|
|
|
|
}
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2019-06-27 02:44:39 +05:30
|
|
|
dispose() {
|
|
|
|
// this races with enable, on the await openTimeline()
|
2019-02-28 03:20:08 +05:30
|
|
|
if (this._timeline) {
|
2019-06-01 21:59:23 +05:30
|
|
|
// will stop the timeline from delivering updates on entries
|
2019-02-28 03:20:08 +05:30
|
|
|
this._timeline.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:44:39 +05:30
|
|
|
close() {
|
|
|
|
this._closeCallback();
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:20:08 +05:30
|
|
|
// 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 21:59:23 +05:30
|
|
|
get timelineViewModel() {
|
|
|
|
return this._timelineVM;
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
2019-03-09 05:13:43 +05:30
|
|
|
|
|
|
|
get error() {
|
|
|
|
if (this._timelineError) {
|
|
|
|
return `Something went wrong loading the timeline: ${this._timelineError.message}`;
|
|
|
|
}
|
2019-06-16 18:51:20 +05:30
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
get avatarInitials() {
|
|
|
|
return avatarInitials(this._room.name);
|
2019-03-09 05:13:43 +05:30
|
|
|
}
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|