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.

84 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-11-04 20:32:00 -07:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-07-28 21:57:00 -06:00
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
Copyright 2017 Travis Ralston
2017-11-04 20:32:00 -07:00
2024-09-09 14:57:16 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
2017-11-04 20:32:00 -07:00
*/
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
}
public reset(): void {
this.cache = {};
this.handler.reset();
}
2017-11-05 15:37:06 -07:00
}