Have LocalEchoWrapper emit updates so the app can react faster (#7358)

This commit is contained in:
Michael Telatynski 2021-12-17 08:53:03 +00:00 committed by GitHub
parent feea80dfd5
commit 9ed771ad7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 16 deletions

View file

@ -28,7 +28,7 @@ import { _t } from '../languageHandler';
import dis from '../dispatcher/dispatcher';
import { IFeature, ISetting, LabGroup, SETTINGS } from "./Settings";
import LocalEchoWrapper from "./handlers/LocalEchoWrapper";
import { WatchManager, CallbackFn as WatchCallbackFn } from "./WatchManager";
import { CallbackFn as WatchCallbackFn, WatchManager } from "./WatchManager";
import { SettingLevel } from "./SettingLevel";
import SettingsHandler from "./handlers/SettingsHandler";
import { SettingUpdatedPayload } from "../dispatcher/payloads/SettingUpdatedPayload";
@ -50,21 +50,20 @@ for (const key of Object.keys(SETTINGS)) {
}
}
// Only wrap the handlers with async setters in a local echo wrapper
const LEVEL_HANDLERS = {
[SettingLevel.DEVICE]: new DeviceSettingsHandler(featureNames, defaultWatchManager),
[SettingLevel.ROOM_DEVICE]: new RoomDeviceSettingsHandler(defaultWatchManager),
[SettingLevel.ROOM_ACCOUNT]: new RoomAccountSettingsHandler(defaultWatchManager),
[SettingLevel.ACCOUNT]: new AccountSettingsHandler(defaultWatchManager),
[SettingLevel.ROOM]: new RoomSettingsHandler(defaultWatchManager),
[SettingLevel.ROOM_ACCOUNT]: new LocalEchoWrapper(
new RoomAccountSettingsHandler(defaultWatchManager),
SettingLevel.ROOM_ACCOUNT,
),
[SettingLevel.ACCOUNT]: new LocalEchoWrapper(new AccountSettingsHandler(defaultWatchManager), SettingLevel.ACCOUNT),
[SettingLevel.ROOM]: new LocalEchoWrapper(new RoomSettingsHandler(defaultWatchManager), SettingLevel.ROOM),
[SettingLevel.CONFIG]: new ConfigSettingsHandler(featureNames),
[SettingLevel.DEFAULT]: new DefaultSettingsHandler(defaultSettings, invertedDefaultSettings),
};
// Wrap all the handlers with local echo
for (const key of Object.keys(LEVEL_HANDLERS)) {
LEVEL_HANDLERS[key] = new LocalEchoWrapper(LEVEL_HANDLERS[key]);
}
export const LEVEL_ORDER = [
SettingLevel.DEVICE,
SettingLevel.ROOM_DEVICE,

View file

@ -36,10 +36,14 @@ const ANALYTICS_EVENT_TYPE = "im.vector.analytics";
* This handler does not make use of the roomId parameter.
*/
export default class AccountSettingsHandler extends MatrixClientBackedSettingsHandler {
constructor(private watchers: WatchManager) {
constructor(public readonly watchers: WatchManager) {
super();
}
public get level(): SettingLevel {
return SettingLevel.ACCOUNT;
}
public initMatrixClient(oldClient: MatrixClient, newClient: MatrixClient) {
if (oldClient) {
oldClient.removeListener("accountData", this.onAccountData);

View file

@ -33,7 +33,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
* @param {string[]} featureNames The names of known features.
* @param {WatchManager} watchers The watch manager to notify updates to
*/
constructor(private featureNames: string[], private watchers: WatchManager) {
constructor(private featureNames: string[], public readonly watchers: WatchManager) {
super();
}

View file

@ -16,6 +16,7 @@ limitations under the License.
*/
import SettingsHandler from "./SettingsHandler";
import { SettingLevel } from "../SettingLevel";
/**
* A wrapper for a SettingsHandler that performs local echo on
@ -32,8 +33,9 @@ export default class LocalEchoWrapper extends SettingsHandler {
/**
* Creates a new local echo wrapper
* @param {SettingsHandler} handler The handler to wrap
* @param {SettingLevel} level The level to notify updates at
*/
constructor(private handler: SettingsHandler) {
constructor(private readonly handler: SettingsHandler, private readonly level: SettingLevel) {
super();
}
@ -54,8 +56,13 @@ export default class LocalEchoWrapper extends SettingsHandler {
const cacheRoomId = roomId ? roomId : "UNDEFINED"; // avoid weird keys
bySetting[cacheRoomId] = newValue;
const currentValue = this.handler.getValue(settingName, roomId);
const handlerPromise = this.handler.setValue(settingName, roomId, newValue);
return Promise.resolve(handlerPromise).finally(() => {
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, newValue);
return Promise.resolve(handlerPromise).catch(() => {
// notify of a rollback
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, currentValue);
}).finally(() => {
delete bySetting[cacheRoomId];
});
}

View file

@ -31,7 +31,7 @@ const ALLOWED_WIDGETS_EVENT_TYPE = "im.vector.setting.allowed_widgets";
* Gets and sets settings at the "room-account" level for the current user.
*/
export default class RoomAccountSettingsHandler extends MatrixClientBackedSettingsHandler {
constructor(private watchers: WatchManager) {
constructor(public readonly watchers: WatchManager) {
super();
}

View file

@ -24,7 +24,7 @@ import { WatchManager } from "../WatchManager";
* room.
*/
export default class RoomDeviceSettingsHandler extends SettingsHandler {
constructor(private watchers: WatchManager) {
constructor(public readonly watchers: WatchManager) {
super();
}

View file

@ -29,7 +29,7 @@ import { WatchManager } from "../WatchManager";
* Gets and sets settings at the "room" level.
*/
export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandler {
constructor(private watchers: WatchManager) {
constructor(public readonly watchers: WatchManager) {
super();
}

View file

@ -15,11 +15,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { WatchManager } from "../WatchManager";
/**
* Represents the base class for all level handlers. This class performs no logic
* and should be overridden.
*/
export default abstract class SettingsHandler {
public readonly watchers?: WatchManager;
/**
* Gets the value for a particular setting at this level for a particular room.
* If no room is applicable, the roomId may be null. The roomId may not be