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

Commit 424d33d

Browse files
authored
Fix PlatformSettingsHandler always returning true due to returning a Promise (#8954)
* Fix PlatformSettingsHandler always returning true due to returning a Promise * Improve typescript
1 parent c3f26d6 commit 424d33d

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

src/PlatformPeg.ts

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

1818
import BasePlatform from "./BasePlatform";
19+
import defaultDispatcher from "./dispatcher/dispatcher";
20+
import { Action } from "./dispatcher/actions";
21+
import { PlatformSetPayload } from "./dispatcher/payloads/PlatformSetPayload";
1922

2023
/*
2124
* Holds the current instance of the `Platform` to use across the codebase.
@@ -29,23 +32,26 @@ import BasePlatform from "./BasePlatform";
2932
* object.
3033
*/
3134
export class PlatformPeg {
32-
platform: BasePlatform = null;
35+
private platform: BasePlatform = null;
3336

3437
/**
3538
* Returns the current Platform object for the application.
3639
* This should be an instance of a class extending BasePlatform.
3740
*/
38-
get() {
41+
public get() {
3942
return this.platform;
4043
}
4144

4245
/**
43-
* Sets the current platform handler object to use for the
44-
* application.
45-
* This should be an instance of a class extending BasePlatform.
46+
* Sets the current platform handler object to use for the application.
47+
* @param {BasePlatform} platform an instance of a class extending BasePlatform.
4648
*/
47-
set(plaf: BasePlatform) {
48-
this.platform = plaf;
49+
public set(platform: BasePlatform) {
50+
this.platform = platform;
51+
defaultDispatcher.dispatch<PlatformSetPayload>({
52+
action: Action.PlatformSet,
53+
platform,
54+
});
4955
}
5056
}
5157

src/dispatcher/actions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,10 @@ export enum Action {
315315
* Fired when the client was logged in. No additional payload information required.
316316
*/
317317
OnLoggedIn = "on_logged_in",
318+
319+
/**
320+
* Fired when the PlatformPeg gets a new platform set upon it, should only happen once per app load lifecycle.
321+
* Fires with the PlatformSetPayload.
322+
*/
323+
PlatformSet = "platform_set",
318324
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { Action } from "../actions";
18+
import { ActionPayload } from "../payloads";
19+
import BasePlatform from "../../BasePlatform";
20+
21+
export interface PlatformSetPayload extends ActionPayload {
22+
action: Action.PlatformSet;
23+
platform: BasePlatform;
24+
}

src/settings/handlers/PlatformSettingsHandler.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,49 @@ limitations under the License.
1616

1717
import SettingsHandler from "./SettingsHandler";
1818
import PlatformPeg from "../../PlatformPeg";
19+
import { SETTINGS } from "../Settings";
20+
import { SettingLevel } from "../SettingLevel";
21+
import defaultDispatcher from "../../dispatcher/dispatcher";
22+
import { ActionPayload } from "../../dispatcher/payloads";
23+
import { Action } from "../../dispatcher/actions";
1924

2025
/**
2126
* Gets and sets settings at the "platform" level for the current device.
2227
* This handler does not make use of the roomId parameter.
2328
*/
2429
export default class PlatformSettingsHandler extends SettingsHandler {
30+
private store: { [settingName: string]: any } = {};
31+
32+
constructor() {
33+
super();
34+
35+
defaultDispatcher.register(this.onAction);
36+
}
37+
38+
private onAction = (payload: ActionPayload) => {
39+
if (payload.action === Action.PlatformSet) {
40+
this.store = {};
41+
// Load setting values as they are async and `getValue` must be synchronous
42+
Object.entries(SETTINGS).forEach(([key, setting]) => {
43+
if (setting.supportedLevels.includes(SettingLevel.PLATFORM) && payload.platform.supportsSetting(key)) {
44+
payload.platform.getSettingValue(key).then(value => {
45+
this.store[key] = value;
46+
});
47+
}
48+
});
49+
}
50+
};
51+
2552
public canSetValue(settingName: string, roomId: string): boolean {
2653
return PlatformPeg.get().supportsSetting(settingName);
2754
}
2855

2956
public getValue(settingName: string, roomId: string): any {
30-
return PlatformPeg.get().getSettingValue(settingName);
57+
return this.store[settingName];
3158
}
3259

3360
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
61+
this.store[settingName] = newValue; // keep cache up to date for synchronous access
3462
return PlatformPeg.get().setSettingValue(settingName, newValue);
3563
}
3664

0 commit comments

Comments
 (0)