element-web/src/settings/WatchManager.ts

81 lines
3.1 KiB
TypeScript
Raw Normal View History

/*
Copyright 2019 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.
*/
2020-07-29 00:40:24 +03:00
import { SettingLevel } from "./SettingLevel";
export type CallbackFn = (changedInRoomId: string, atLevel: SettingLevel, newValAtLevel: any) => void;
2020-09-08 17:07:16 +03:00
const IRRELEVANT_ROOM: string = null;
2020-07-29 00:40:24 +03:00
interface RoomWatcherMap {
2020-09-08 13:01:36 +03:00
[roomId: string]: CallbackFn[];
2020-07-29 00:40:24 +03:00
}
/**
* Generalized management class for dealing with watchers on a per-handler (per-level)
* basis without duplicating code. Handlers are expected to push updates through this
* class, which are then proxied outwards to any applicable watchers.
*/
export class WatchManager {
2020-07-29 00:40:24 +03:00
private watchers: {[settingName: string]: RoomWatcherMap} = {};
// Proxy for handlers to delegate changes to this manager
2020-07-29 00:40:24 +03:00
public watchSetting(settingName: string, roomId: string | null, cb: CallbackFn) {
if (!this.watchers[settingName]) this.watchers[settingName] = {};
if (!this.watchers[settingName][roomId]) this.watchers[settingName][roomId] = [];
this.watchers[settingName][roomId].push(cb);
}
// Proxy for handlers to delegate changes to this manager
2020-07-29 00:40:24 +03:00
public unwatchSetting(cb: CallbackFn) {
for (const settingName of Object.keys(this.watchers)) {
for (const roomId of Object.keys(this.watchers[settingName])) {
let idx;
2020-07-29 00:40:24 +03:00
while ((idx = this.watchers[settingName][roomId].indexOf(cb)) !== -1) {
this.watchers[settingName][roomId].splice(idx, 1);
}
}
}
}
2020-07-29 01:31:13 +03:00
public notifyUpdate(settingName: string, inRoomId: string | null, atLevel: SettingLevel, newValueAtLevel: any) {
// Dev note: We could avoid raising changes for ultimately inconsequential changes, but
// we also don't have a reliable way to get the old value of a setting. Instead, we'll just
// let it fall through regardless and let the receiver dedupe if they want to.
2020-07-29 00:40:24 +03:00
if (!this.watchers[settingName]) return;
2020-07-29 00:40:24 +03:00
const roomWatchers = this.watchers[settingName];
const callbacks = [];
if (inRoomId !== null && roomWatchers[inRoomId]) {
callbacks.push(...roomWatchers[inRoomId]);
}
if (!inRoomId) {
// Fire updates to all the individual room watchers too, as they probably
// care about the change higher up.
2020-09-08 13:01:36 +03:00
callbacks.push(...Object.values(roomWatchers).flat(1));
2020-07-29 00:40:24 +03:00
} else if (roomWatchers[IRRELEVANT_ROOM]) {
callbacks.push(...roomWatchers[IRRELEVANT_ROOM]);
}
for (const callback of callbacks) {
callback(inRoomId, atLevel, newValueAtLevel);
}
}
}