2017-09-11 15:59:09 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2021-07-12 07:59:31 +00:00
|
|
|
import { EventSubscription } from 'fbemitter';
|
2017-09-11 15:59:09 +00:00
|
|
|
import RoomViewStore from './stores/RoomViewStore';
|
|
|
|
|
2021-10-15 14:31:29 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
type Listener = (isActive: boolean) => void;
|
|
|
|
|
2017-09-11 15:59:09 +00:00
|
|
|
/**
|
|
|
|
* Consumes changes from the RoomViewStore and notifies specific things
|
|
|
|
* about when the active room changes. Unlike listening for RoomViewStore
|
|
|
|
* changes, you can subscribe to only changes relevant to a particular
|
|
|
|
* room.
|
|
|
|
*
|
|
|
|
* TODO: If we introduce an observer for something else, factor out
|
|
|
|
* the adding / removing of listeners & emitting into a common class.
|
|
|
|
*/
|
2020-07-21 09:14:12 +00:00
|
|
|
export class ActiveRoomObserver {
|
|
|
|
private listeners: {[key: string]: Listener[]} = {};
|
2020-07-21 16:57:08 +00:00
|
|
|
private _activeRoomId = RoomViewStore.getRoomId();
|
2021-07-12 07:59:31 +00:00
|
|
|
private readonly roomStoreToken: EventSubscription;
|
2017-09-11 15:59:09 +00:00
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
constructor() {
|
|
|
|
// TODO: We could self-destruct when the last listener goes away, or at least stop listening.
|
|
|
|
this.roomStoreToken = RoomViewStore.addListener(this.onRoomViewStoreUpdate);
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
public get activeRoomId(): string {
|
2020-06-05 20:08:20 +00:00
|
|
|
return this._activeRoomId;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
public addListener(roomId, listener) {
|
|
|
|
if (!this.listeners[roomId]) this.listeners[roomId] = [];
|
|
|
|
this.listeners[roomId].push(listener);
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
public removeListener(roomId, listener) {
|
|
|
|
if (this.listeners[roomId]) {
|
|
|
|
const i = this.listeners[roomId].indexOf(listener);
|
2017-09-11 15:59:09 +00:00
|
|
|
if (i > -1) {
|
2020-07-21 09:14:12 +00:00
|
|
|
this.listeners[roomId].splice(i, 1);
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("Unregistering unrecognised listener (roomId=" + roomId + ")");
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
private emit(roomId, isActive: boolean) {
|
|
|
|
if (!this.listeners[roomId]) return;
|
2017-09-11 15:59:09 +00:00
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
for (const l of this.listeners[roomId]) {
|
2020-06-05 20:08:20 +00:00
|
|
|
l.call(null, isActive);
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:14:12 +00:00
|
|
|
private onRoomViewStoreUpdate = () => {
|
2017-09-11 15:59:09 +00:00
|
|
|
// emit for the old room ID
|
2020-07-21 09:14:12 +00:00
|
|
|
if (this._activeRoomId) this.emit(this._activeRoomId, false);
|
2017-09-11 15:59:09 +00:00
|
|
|
|
|
|
|
// update our cache
|
|
|
|
this._activeRoomId = RoomViewStore.getRoomId();
|
|
|
|
|
|
|
|
// and emit for the new one
|
2020-07-21 09:14:12 +00:00
|
|
|
if (this._activeRoomId) this.emit(this._activeRoomId, true);
|
|
|
|
};
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 16:56:50 +00:00
|
|
|
if (window.mxActiveRoomObserver === undefined) {
|
|
|
|
window.mxActiveRoomObserver = new ActiveRoomObserver();
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
2020-07-21 16:56:50 +00:00
|
|
|
export default window.mxActiveRoomObserver;
|