Convert WatchManager to TS
parent
c96def81ae
commit
d627baf508
|
@ -14,41 +14,52 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { SettingLevel } from "./SettingLevel";
|
||||||
|
|
||||||
|
export type CallbackFn = (changedInRoomId: string, atLevel: SettingLevel, newValAtLevel: any) => void;
|
||||||
|
|
||||||
|
const IRRELEVANT_ROOM: symbol = Symbol("any room");
|
||||||
|
|
||||||
|
interface RoomWatcherMap {
|
||||||
|
// @ts-ignore - TS wants string-only keys but we know better - https://github.com/Microsoft/TypeScript/issues/1863
|
||||||
|
[roomId: string | symbol]: CallbackFn[];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generalized management class for dealing with watchers on a per-handler (per-level)
|
* 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
|
* basis without duplicating code. Handlers are expected to push updates through this
|
||||||
* class, which are then proxied outwards to any applicable watchers.
|
* class, which are then proxied outwards to any applicable watchers.
|
||||||
*/
|
*/
|
||||||
export class WatchManager {
|
export class WatchManager {
|
||||||
_watchers = {}; // { settingName: { roomId: callbackFns[] } }
|
private watchers: {[settingName: string]: RoomWatcherMap} = {};
|
||||||
|
|
||||||
// Proxy for handlers to delegate changes to this manager
|
// Proxy for handlers to delegate changes to this manager
|
||||||
watchSetting(settingName, roomId, cb) {
|
public watchSetting(settingName: string, roomId: string | null, cb: CallbackFn) {
|
||||||
if (!this._watchers[settingName]) this._watchers[settingName] = {};
|
if (!this.watchers[settingName]) this.watchers[settingName] = {};
|
||||||
if (!this._watchers[settingName][roomId]) this._watchers[settingName][roomId] = [];
|
if (!this.watchers[settingName][roomId]) this.watchers[settingName][roomId] = [];
|
||||||
this._watchers[settingName][roomId].push(cb);
|
this.watchers[settingName][roomId].push(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxy for handlers to delegate changes to this manager
|
// Proxy for handlers to delegate changes to this manager
|
||||||
unwatchSetting(cb) {
|
public unwatchSetting(cb: CallbackFn) {
|
||||||
for (const settingName of Object.keys(this._watchers)) {
|
for (const settingName of Object.keys(this.watchers)) {
|
||||||
for (const roomId of Object.keys(this._watchers[settingName])) {
|
for (const roomId of Object.keys(this.watchers[settingName])) {
|
||||||
let idx;
|
let idx;
|
||||||
while ((idx = this._watchers[settingName][roomId].indexOf(cb)) !== -1) {
|
while ((idx = this.watchers[settingName][roomId].indexOf(cb)) !== -1) {
|
||||||
this._watchers[settingName][roomId].splice(idx, 1);
|
this.watchers[settingName][roomId].splice(idx, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyUpdate(settingName, inRoomId, atLevel, newValueAtLevel) {
|
public notifyUpdate(settingName: string, inRoomId: string, atLevel: SettingLevel, newValueAtLevel: any) {
|
||||||
// Dev note: We could avoid raising changes for ultimately inconsequential changes, but
|
// 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
|
// 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.
|
// let it fall through regardless and let the receiver dedupe if they want to.
|
||||||
|
|
||||||
if (!this._watchers[settingName]) return;
|
if (!this.watchers[settingName]) return;
|
||||||
|
|
||||||
const roomWatchers = this._watchers[settingName];
|
const roomWatchers = this.watchers[settingName];
|
||||||
const callbacks = [];
|
const callbacks = [];
|
||||||
|
|
||||||
if (inRoomId !== null && roomWatchers[inRoomId]) {
|
if (inRoomId !== null && roomWatchers[inRoomId]) {
|
||||||
|
@ -59,8 +70,8 @@ export class WatchManager {
|
||||||
// Fire updates to all the individual room watchers too, as they probably
|
// Fire updates to all the individual room watchers too, as they probably
|
||||||
// care about the change higher up.
|
// care about the change higher up.
|
||||||
callbacks.push(...Object.values(roomWatchers).reduce((r, a) => [...r, ...a], []));
|
callbacks.push(...Object.values(roomWatchers).reduce((r, a) => [...r, ...a], []));
|
||||||
} else if (roomWatchers[null]) {
|
} else if (roomWatchers[IRRELEVANT_ROOM]) {
|
||||||
callbacks.push(...roomWatchers[null]);
|
callbacks.push(...roomWatchers[IRRELEVANT_ROOM]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const callback of callbacks) {
|
for (const callback of callbacks) {
|
Loading…
Reference in New Issue