2021-09-26 17:57:02 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EventEmitter from 'events';
|
2022-03-01 20:42:05 +00:00
|
|
|
import { MatrixEvent, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2022-03-15 12:15:26 +00:00
|
|
|
import { RoomState } from "matrix-js-sdk/src/models/room-state";
|
2021-09-26 17:57:02 +00:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from '../MatrixClientPeg';
|
2022-03-15 12:15:26 +00:00
|
|
|
import WidgetUtils from "../utils/WidgetUtils";
|
2021-09-26 17:57:02 +00:00
|
|
|
import { WidgetMessagingStore } from "./widgets/WidgetMessagingStore";
|
|
|
|
|
|
|
|
export enum ActiveWidgetStoreEvent {
|
|
|
|
Update = "update",
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
export default class ActiveWidgetStore extends EventEmitter {
|
|
|
|
private static internalInstance: ActiveWidgetStore;
|
|
|
|
private persistentWidgetId: string;
|
2022-03-15 12:15:26 +00:00
|
|
|
private persistentRoomId: string;
|
2021-09-26 17:57:02 +00:00
|
|
|
|
|
|
|
public static get instance(): ActiveWidgetStore {
|
|
|
|
if (!ActiveWidgetStore.internalInstance) {
|
|
|
|
ActiveWidgetStore.internalInstance = new ActiveWidgetStore();
|
|
|
|
}
|
|
|
|
return ActiveWidgetStore.internalInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public start(): void {
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().on(RoomStateEvent.Events, this.onRoomStateEvents);
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public stop(): void {
|
2022-03-15 12:15:26 +00:00
|
|
|
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:15:26 +00:00
|
|
|
private onRoomStateEvents = (ev: MatrixEvent, { roomId }: RoomState): void => {
|
2021-09-26 17:57:02 +00:00
|
|
|
// 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.
|
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2022-03-15 12:15:26 +00:00
|
|
|
if (ev.getType() === "im.vector.modular.widgets") {
|
|
|
|
this.destroyPersistentWidget(ev.getStateKey(), roomId);
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-15 12:15:26 +00:00
|
|
|
public destroyPersistentWidget(widgetId: string, roomId: string): void {
|
|
|
|
if (!this.getWidgetPersistence(widgetId, roomId)) return;
|
|
|
|
WidgetMessagingStore.instance.stopMessagingByUid(WidgetUtils.calcWidgetUid(widgetId, roomId));
|
|
|
|
this.setWidgetPersistence(widgetId, roomId, false);
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:15:26 +00:00
|
|
|
public setWidgetPersistence(widgetId: string, roomId: string, val: boolean): void {
|
|
|
|
const isPersisted = this.getWidgetPersistence(widgetId, roomId);
|
|
|
|
|
|
|
|
if (isPersisted && !val) {
|
2021-09-26 17:57:02 +00:00
|
|
|
this.persistentWidgetId = null;
|
2022-03-15 12:15:26 +00:00
|
|
|
this.persistentRoomId = null;
|
|
|
|
} else if (!isPersisted && val) {
|
2021-09-26 17:57:02 +00:00
|
|
|
this.persistentWidgetId = widgetId;
|
2022-03-15 12:15:26 +00:00
|
|
|
this.persistentRoomId = roomId;
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
this.emit(ActiveWidgetStoreEvent.Update);
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:15:26 +00:00
|
|
|
public getWidgetPersistence(widgetId: string, roomId: string): boolean {
|
|
|
|
return this.persistentWidgetId === widgetId && this.persistentRoomId === roomId;
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPersistentWidgetId(): string {
|
|
|
|
return this.persistentWidgetId;
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:15:26 +00:00
|
|
|
public getPersistentRoomId(): string {
|
|
|
|
return this.persistentRoomId;
|
2021-09-26 17:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.mxActiveWidgetStore = ActiveWidgetStore.instance;
|