2018-07-11 20:07:32 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-07-24 18:21:43 +03:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../MatrixClientPeg';
|
2020-09-28 22:34:13 +03:00
|
|
|
import {WidgetMessagingStore} from "./widgets/WidgetMessagingStore";
|
2018-07-24 18:21:43 +03:00
|
|
|
|
2018-07-11 20:07:32 +03:00
|
|
|
/**
|
|
|
|
* Stores information about the widgets active in the app right now:
|
|
|
|
* * What widget is set to remain always-on-screen, if any
|
|
|
|
* Only one widget may be 'always on screen' at any one time.
|
|
|
|
* * Negotiated capabilities for active apps
|
|
|
|
*/
|
2018-07-24 18:21:43 +03:00
|
|
|
class ActiveWidgetStore extends EventEmitter {
|
2018-07-11 20:07:32 +03:00
|
|
|
constructor() {
|
2018-07-24 18:21:43 +03:00
|
|
|
super();
|
2018-07-11 20:07:32 +03:00
|
|
|
this._persistentWidgetId = null;
|
|
|
|
|
2018-07-12 20:43:49 +03:00
|
|
|
// What room ID each widget is associated with (if it's a room widget)
|
|
|
|
this._roomIdByWidgetId = {};
|
2018-07-24 18:21:43 +03:00
|
|
|
|
|
|
|
this.onRoomStateEvents = this.onRoomStateEvents.bind(this);
|
|
|
|
|
|
|
|
this.dispatcherRef = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
MatrixClientPeg.get().on('RoomState.events', this.onRoomStateEvents);
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2018-07-24 18:55:45 +03:00
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener('RoomState.events', this.onRoomStateEvents);
|
|
|
|
}
|
2018-07-24 18:21:43 +03:00
|
|
|
this._roomIdByWidgetId = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
onRoomStateEvents(ev, state) {
|
|
|
|
// XXX: This listens for state events in order to remove the active widget.
|
|
|
|
// Everything else relies on views listening for events and calling setters
|
|
|
|
// on this class which is terrible. This store should just listen for events
|
|
|
|
// and keep itself up to date.
|
2020-08-03 18:02:26 +03:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2018-07-24 18:21:43 +03:00
|
|
|
if (ev.getType() !== 'im.vector.modular.widgets') return;
|
|
|
|
|
|
|
|
if (ev.getStateKey() === this._persistentWidgetId) {
|
2019-08-22 18:57:51 +03:00
|
|
|
this.destroyPersistentWidget(this._persistentWidgetId);
|
2018-07-24 18:21:43 +03:00
|
|
|
}
|
2018-07-11 20:07:32 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 18:57:51 +03:00
|
|
|
destroyPersistentWidget(id) {
|
|
|
|
if (id !== this._persistentWidgetId) return;
|
2018-07-24 18:50:34 +03:00
|
|
|
const toDeleteId = this._persistentWidgetId;
|
|
|
|
|
2020-09-28 22:34:13 +03:00
|
|
|
const result = WidgetMessagingStore.instance.findWidgetById(id);
|
|
|
|
if (result) {
|
|
|
|
if (result.room) {
|
|
|
|
WidgetMessagingStore.instance.stopMessagingForRoomWidget(result.room, result.widget);
|
|
|
|
} else {
|
|
|
|
WidgetMessagingStore.instance.stopMessagingForAccountWidget(result.widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 18:50:34 +03:00
|
|
|
this.setWidgetPersistence(toDeleteId, false);
|
|
|
|
this.delRoomId(toDeleteId);
|
|
|
|
}
|
|
|
|
|
2018-07-11 20:07:32 +03:00
|
|
|
setWidgetPersistence(widgetId, val) {
|
|
|
|
if (this._persistentWidgetId === widgetId && !val) {
|
|
|
|
this._persistentWidgetId = null;
|
|
|
|
} else if (this._persistentWidgetId !== widgetId && val) {
|
|
|
|
this._persistentWidgetId = widgetId;
|
|
|
|
}
|
2018-07-24 18:21:43 +03:00
|
|
|
this.emit('update');
|
2018-07-11 20:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getWidgetPersistence(widgetId) {
|
|
|
|
return this._persistentWidgetId === widgetId;
|
|
|
|
}
|
|
|
|
|
2018-07-12 20:43:49 +03:00
|
|
|
getPersistentWidgetId() {
|
|
|
|
return this._persistentWidgetId;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRoomId(widgetId) {
|
|
|
|
return this._roomIdByWidgetId[widgetId];
|
|
|
|
}
|
|
|
|
|
|
|
|
setRoomId(widgetId, roomId) {
|
|
|
|
this._roomIdByWidgetId[widgetId] = roomId;
|
2018-07-24 18:21:43 +03:00
|
|
|
this.emit('update');
|
2018-07-12 20:43:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
delRoomId(widgetId) {
|
|
|
|
delete this._roomIdByWidgetId[widgetId];
|
2018-07-24 18:21:43 +03:00
|
|
|
this.emit('update');
|
2018-07-12 20:43:49 +03:00
|
|
|
}
|
2018-07-11 20:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (global.singletonActiveWidgetStore === undefined) {
|
|
|
|
global.singletonActiveWidgetStore = new ActiveWidgetStore();
|
|
|
|
}
|
|
|
|
export default global.singletonActiveWidgetStore;
|