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.
|
|
|
|
*/
|
|
|
|
|
2018-11-07 12:25:42 +00:00
|
|
|
import OpenRoomsStore from './stores/OpenRoomsStore';
|
2017-09-11 15:59:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-11-07 12:25:42 +00:00
|
|
|
* Consumes changes from the OpenRoomsStore and notifies specific things
|
2017-09-11 15:59:09 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class ActiveRoomObserver {
|
|
|
|
constructor() {
|
|
|
|
this._listeners = {};
|
2018-11-22 12:23:07 +00:00
|
|
|
const roomStore = OpenRoomsStore.getActiveRoomStore();
|
2018-11-07 12:25:42 +00:00
|
|
|
this._activeRoomId = roomStore && roomStore.getRoomId();
|
2017-09-11 15:59:09 +00:00
|
|
|
// TODO: We could self-destruct when the last listener goes away, or at least
|
|
|
|
// stop listening.
|
2018-11-07 12:25:42 +00:00
|
|
|
this._roomStoreToken = OpenRoomsStore.addListener(this._onOpenRoomsStoreUpdate.bind(this));
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 15:29:17 +00:00
|
|
|
getActiveRoomId() {
|
|
|
|
return this._activeRoomId;
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:59:09 +00:00
|
|
|
addListener(roomId, listener) {
|
|
|
|
if (!this._listeners[roomId]) this._listeners[roomId] = [];
|
|
|
|
this._listeners[roomId].push(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeListener(roomId, listener) {
|
|
|
|
if (this._listeners[roomId]) {
|
|
|
|
const i = this._listeners[roomId].indexOf(listener);
|
|
|
|
if (i > -1) {
|
|
|
|
this._listeners[roomId].splice(i, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn("Unregistering unrecognised listener (roomId=" + roomId + ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 19:06:58 +00:00
|
|
|
_emit(roomId, newActiveRoomId) {
|
2017-09-11 15:59:09 +00:00
|
|
|
if (!this._listeners[roomId]) return;
|
|
|
|
|
|
|
|
for (const l of this._listeners[roomId]) {
|
2018-11-22 19:06:58 +00:00
|
|
|
l.call(l, newActiveRoomId);
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:25:42 +00:00
|
|
|
_onOpenRoomsStoreUpdate() {
|
2018-11-22 12:23:07 +00:00
|
|
|
const activeRoomStore = OpenRoomsStore.getActiveRoomStore();
|
2018-11-22 19:06:58 +00:00
|
|
|
const newActiveRoomId = activeRoomStore && activeRoomStore.getRoomId();
|
|
|
|
// emit for the old room ID
|
|
|
|
if (this._activeRoomId) this._emit(this._activeRoomId, newActiveRoomId);
|
2017-09-11 15:59:09 +00:00
|
|
|
// update our cache
|
2018-11-22 19:06:58 +00:00
|
|
|
this._activeRoomId = newActiveRoomId;
|
2017-09-11 15:59:09 +00:00
|
|
|
// and emit for the new one
|
2018-11-22 19:06:58 +00:00
|
|
|
if (this._activeRoomId) this._emit(this._activeRoomId, this._activeRoomId);
|
2017-09-11 15:59:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.mx_ActiveRoomObserver === undefined) {
|
|
|
|
global.mx_ActiveRoomObserver = new ActiveRoomObserver();
|
|
|
|
}
|
|
|
|
export default global.mx_ActiveRoomObserver;
|