Files
ThreadNet-Web/src/settings/handlers/LocalEchoWrapper.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.9 KiB
TypeScript
Raw Normal View History

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";
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
* @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 {
const cacheRoomId = roomId ?? "UNDEFINED"; // avoid weird keys
2020-07-28 21:57:00 -06:00
const bySetting = this.cache[settingName];
if (bySetting?.hasOwnProperty(cacheRoomId)) {
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
}
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
const cacheRoomId = roomId ?? "UNDEFINED"; // avoid weird keys
2017-11-04 20:32:00 -07:00
bySetting[cacheRoomId] = newValue;
const currentValue = this.handler.getValue(settingName, roomId);
2020-07-28 21:57:00 -06:00
const handlerPromise = this.handler.setValue(settingName, roomId, newValue);
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, newValue);
try {
await handlerPromise;
} catch (e) {
// notify of a rollback
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, currentValue);
} 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
}