Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9ed771a

Browse files
authored
Have LocalEchoWrapper emit updates so the app can react faster (#7358)
1 parent feea80d commit 9ed771a

8 files changed

+30
-16
lines changed

src/settings/SettingsStore.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { _t } from '../languageHandler';
2828
import dis from '../dispatcher/dispatcher';
2929
import { IFeature, ISetting, LabGroup, SETTINGS } from "./Settings";
3030
import LocalEchoWrapper from "./handlers/LocalEchoWrapper";
31-
import { WatchManager, CallbackFn as WatchCallbackFn } from "./WatchManager";
31+
import { CallbackFn as WatchCallbackFn, WatchManager } from "./WatchManager";
3232
import { SettingLevel } from "./SettingLevel";
3333
import SettingsHandler from "./handlers/SettingsHandler";
3434
import { SettingUpdatedPayload } from "../dispatcher/payloads/SettingUpdatedPayload";
@@ -50,21 +50,20 @@ for (const key of Object.keys(SETTINGS)) {
5050
}
5151
}
5252

53+
// Only wrap the handlers with async setters in a local echo wrapper
5354
const LEVEL_HANDLERS = {
5455
[SettingLevel.DEVICE]: new DeviceSettingsHandler(featureNames, defaultWatchManager),
5556
[SettingLevel.ROOM_DEVICE]: new RoomDeviceSettingsHandler(defaultWatchManager),
56-
[SettingLevel.ROOM_ACCOUNT]: new RoomAccountSettingsHandler(defaultWatchManager),
57-
[SettingLevel.ACCOUNT]: new AccountSettingsHandler(defaultWatchManager),
58-
[SettingLevel.ROOM]: new RoomSettingsHandler(defaultWatchManager),
57+
[SettingLevel.ROOM_ACCOUNT]: new LocalEchoWrapper(
58+
new RoomAccountSettingsHandler(defaultWatchManager),
59+
SettingLevel.ROOM_ACCOUNT,
60+
),
61+
[SettingLevel.ACCOUNT]: new LocalEchoWrapper(new AccountSettingsHandler(defaultWatchManager), SettingLevel.ACCOUNT),
62+
[SettingLevel.ROOM]: new LocalEchoWrapper(new RoomSettingsHandler(defaultWatchManager), SettingLevel.ROOM),
5963
[SettingLevel.CONFIG]: new ConfigSettingsHandler(featureNames),
6064
[SettingLevel.DEFAULT]: new DefaultSettingsHandler(defaultSettings, invertedDefaultSettings),
6165
};
6266

63-
// Wrap all the handlers with local echo
64-
for (const key of Object.keys(LEVEL_HANDLERS)) {
65-
LEVEL_HANDLERS[key] = new LocalEchoWrapper(LEVEL_HANDLERS[key]);
66-
}
67-
6867
export const LEVEL_ORDER = [
6968
SettingLevel.DEVICE,
7069
SettingLevel.ROOM_DEVICE,

src/settings/handlers/AccountSettingsHandler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ const ANALYTICS_EVENT_TYPE = "im.vector.analytics";
3636
* This handler does not make use of the roomId parameter.
3737
*/
3838
export default class AccountSettingsHandler extends MatrixClientBackedSettingsHandler {
39-
constructor(private watchers: WatchManager) {
39+
constructor(public readonly watchers: WatchManager) {
4040
super();
4141
}
4242

43+
public get level(): SettingLevel {
44+
return SettingLevel.ACCOUNT;
45+
}
46+
4347
public initMatrixClient(oldClient: MatrixClient, newClient: MatrixClient) {
4448
if (oldClient) {
4549
oldClient.removeListener("accountData", this.onAccountData);

src/settings/handlers/DeviceSettingsHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
3333
* @param {string[]} featureNames The names of known features.
3434
* @param {WatchManager} watchers The watch manager to notify updates to
3535
*/
36-
constructor(private featureNames: string[], private watchers: WatchManager) {
36+
constructor(private featureNames: string[], public readonly watchers: WatchManager) {
3737
super();
3838
}
3939

src/settings/handlers/LocalEchoWrapper.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
*/
1717

1818
import SettingsHandler from "./SettingsHandler";
19+
import { SettingLevel } from "../SettingLevel";
1920

2021
/**
2122
* A wrapper for a SettingsHandler that performs local echo on
@@ -32,8 +33,9 @@ export default class LocalEchoWrapper extends SettingsHandler {
3233
/**
3334
* Creates a new local echo wrapper
3435
* @param {SettingsHandler} handler The handler to wrap
36+
* @param {SettingLevel} level The level to notify updates at
3537
*/
36-
constructor(private handler: SettingsHandler) {
38+
constructor(private readonly handler: SettingsHandler, private readonly level: SettingLevel) {
3739
super();
3840
}
3941

@@ -54,8 +56,13 @@ export default class LocalEchoWrapper extends SettingsHandler {
5456
const cacheRoomId = roomId ? roomId : "UNDEFINED"; // avoid weird keys
5557
bySetting[cacheRoomId] = newValue;
5658

59+
const currentValue = this.handler.getValue(settingName, roomId);
5760
const handlerPromise = this.handler.setValue(settingName, roomId, newValue);
58-
return Promise.resolve(handlerPromise).finally(() => {
61+
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, newValue);
62+
return Promise.resolve(handlerPromise).catch(() => {
63+
// notify of a rollback
64+
this.handler.watchers?.notifyUpdate(settingName, roomId, this.level, currentValue);
65+
}).finally(() => {
5966
delete bySetting[cacheRoomId];
6067
});
6168
}

src/settings/handlers/RoomAccountSettingsHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const ALLOWED_WIDGETS_EVENT_TYPE = "im.vector.setting.allowed_widgets";
3131
* Gets and sets settings at the "room-account" level for the current user.
3232
*/
3333
export default class RoomAccountSettingsHandler extends MatrixClientBackedSettingsHandler {
34-
constructor(private watchers: WatchManager) {
34+
constructor(public readonly watchers: WatchManager) {
3535
super();
3636
}
3737

src/settings/handlers/RoomDeviceSettingsHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { WatchManager } from "../WatchManager";
2424
* room.
2525
*/
2626
export default class RoomDeviceSettingsHandler extends SettingsHandler {
27-
constructor(private watchers: WatchManager) {
27+
constructor(public readonly watchers: WatchManager) {
2828
super();
2929
}
3030

src/settings/handlers/RoomSettingsHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { WatchManager } from "../WatchManager";
2929
* Gets and sets settings at the "room" level.
3030
*/
3131
export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandler {
32-
constructor(private watchers: WatchManager) {
32+
constructor(public readonly watchers: WatchManager) {
3333
super();
3434
}
3535

src/settings/handlers/SettingsHandler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18+
import { WatchManager } from "../WatchManager";
19+
1820
/**
1921
* Represents the base class for all level handlers. This class performs no logic
2022
* and should be overridden.
2123
*/
2224
export default abstract class SettingsHandler {
25+
public readonly watchers?: WatchManager;
26+
2327
/**
2428
* Gets the value for a particular setting at this level for a particular room.
2529
* If no room is applicable, the roomId may be null. The roomId may not be

0 commit comments

Comments
 (0)