2017-11-04 20:32:00 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2017 Travis Ralston
|
2020-07-28 21:57:00 -06:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2017-11-04 20:32:00 -07:00
|
|
|
|
|
|
|
|
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 SettingsHandler from "./SettingsHandler";
|
2021-12-17 08:53:03 +00:00
|
|
|
import { SettingLevel } from "../SettingLevel";
|
2017-11-04 20:32:00 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A wrapper for a SettingsHandler that performs local echo on
|
|
|
|
|
* changes to settings. This wrapper will use the underlying
|
|
|
|
|
* handler as much as possible to ensure values are not stale.
|
|
|
|
|
*/
|
|
|
|
|
export default class LocalEchoWrapper extends SettingsHandler {
|
2020-07-28 21:57:00 -06:00
|
|
|
private cache: {
|
|
|
|
|
[settingName: string]: {
|
|
|
|
|
[roomId: string]: any;
|
|
|
|
|
};
|
|
|
|
|
} = {};
|
|
|
|
|
|
2017-11-04 20:32:00 -07:00
|
|
|
/**
|
|
|
|
|
* Creates a new local echo wrapper
|
|
|
|
|
* @param {SettingsHandler} handler The handler to wrap
|
2021-12-17 08:53:03 +00:00
|
|
|
* @param {SettingLevel} level The level to notify updates at
|
2017-11-04 20:32:00 -07:00
|
|
|
*/
|
2024-01-02 18:56:39 +00:00
|
|
|
public constructor(
|
|
|
|
|
private readonly handler: SettingsHandler,
|
|
|
|
|
private readonly level: SettingLevel,
|
|
|
|
|
) {
|
2017-11-04 20:32:00 -07:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:57:00 -06:00
|
|
|
public getValue(settingName: string, roomId: string): any {
|
2022-03-03 22:09:06 +00:00
|
|
|
const cacheRoomId = roomId ?? "UNDEFINED"; // avoid weird keys
|
2020-07-28 21:57:00 -06:00
|
|
|
const bySetting = this.cache[settingName];
|
2022-03-03 22:09:06 +00:00
|
|
|
if (bySetting?.hasOwnProperty(cacheRoomId)) {
|
2020-06-16 19:38:54 +01:00
|
|
|
return bySetting[cacheRoomId];
|
2017-11-04 20:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:57:00 -06:00
|
|
|
return this.handler.getValue(settingName, roomId);
|
2017-11-04 20:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:09:06 +00:00
|
|
|
public async setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
2020-07-28 21:57:00 -06:00
|
|
|
if (!this.cache[settingName]) this.cache[settingName] = {};
|
|
|
|
|
const bySetting = this.cache[settingName];
|
2017-11-04 20:32:00 -07:00
|
|
|
|
2022-03-03 22:09:06 +00:00
|
|
|
const cacheRoomId = roomId ?? "UNDEFINED"; // avoid weird keys
|
2017-11-04 20:32:00 -07:00
|
|
|
bySetting[cacheRoomId] = newValue;
|
|
|
|
|
|
2021-12-17 08:53:03 +00:00
|
|
|
const currentValue = this.handler.getValue(settingName, roomId);
|
2020-07-28 21:57:00 -06:00
|
|
|
const handlerPromise = this.handler.setValue(settingName, roomId, newValue);
|
2021-12-17 08:53:03 +00:00
|
|
|
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, newValue);
|
2022-03-03 22:09:06 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await handlerPromise;
|
|
|
|
|
} catch (e) {
|
2021-12-17 08:53:03 +00:00
|
|
|
// notify of a rollback
|
|
|
|
|
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, currentValue);
|
2022-03-03 22:09:06 +00:00
|
|
|
} finally {
|
|
|
|
|
// only expire the cache if our value hasn't been overwritten yet
|
|
|
|
|
if (bySetting[cacheRoomId] === newValue) {
|
|
|
|
|
delete bySetting[cacheRoomId];
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-04 20:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-30 08:49:42 -06:00
|
|
|
public canSetValue(settingName: string, roomId: string): boolean {
|
2020-07-28 21:57:00 -06:00
|
|
|
return this.handler.canSetValue(settingName, roomId);
|
2017-11-04 20:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:57:00 -06:00
|
|
|
public isSupported(): boolean {
|
|
|
|
|
return this.handler.isSupported();
|
2017-11-04 20:32:00 -07:00
|
|
|
}
|
2017-11-05 15:37:06 -07:00
|
|
|
}
|