Cache localStorage objects for SettingsStore (#8366)

This commit is contained in:
Michael Telatynski 2022-04-20 09:44:06 +01:00 committed by GitHub
parent 65c74bd158
commit 859fdf7d51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 35 deletions

View file

@ -1,7 +1,7 @@
/*
Copyright 2017 Travis Ralston
Copyright 2019 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2019 - 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -16,17 +16,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import SettingsHandler from "./SettingsHandler";
import { MatrixClientPeg } from "../../MatrixClientPeg";
import { SettingLevel } from "../SettingLevel";
import { CallbackFn, WatchManager } from "../WatchManager";
import AbstractLocalStorageSettingsHandler from "./AbstractLocalStorageSettingsHandler";
/**
* Gets and sets settings at the "device" level for the current device.
* This handler does not make use of the roomId parameter. This handler
* will special-case features to support legacy settings.
*/
export default class DeviceSettingsHandler extends SettingsHandler {
export default class DeviceSettingsHandler extends AbstractLocalStorageSettingsHandler {
/**
* Creates a new device settings handler
* @param {string[]} featureNames The names of known features.
@ -43,15 +43,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
// Special case notifications
if (settingName === "notificationsEnabled") {
const value = localStorage.getItem("notifications_enabled");
const value = this.getItem("notifications_enabled");
if (typeof(value) === "string") return value === "true";
return null; // wrong type or otherwise not set
} else if (settingName === "notificationBodyEnabled") {
const value = localStorage.getItem("notifications_body_enabled");
const value = this.getItem("notifications_body_enabled");
if (typeof(value) === "string") return value === "true";
return null; // wrong type or otherwise not set
} else if (settingName === "audioNotificationsEnabled") {
const value = localStorage.getItem("audio_notifications_enabled");
const value = this.getItem("audio_notifications_enabled");
if (typeof(value) === "string") return value === "true";
return null; // wrong type or otherwise not set
}
@ -68,15 +68,15 @@ export default class DeviceSettingsHandler extends SettingsHandler {
// Special case notifications
if (settingName === "notificationsEnabled") {
localStorage.setItem("notifications_enabled", newValue);
this.setItem("notifications_enabled", newValue);
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
} else if (settingName === "notificationBodyEnabled") {
localStorage.setItem("notifications_body_enabled", newValue);
this.setItem("notifications_body_enabled", newValue);
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
} else if (settingName === "audioNotificationsEnabled") {
localStorage.setItem("audio_notifications_enabled", newValue);
this.setItem("audio_notifications_enabled", newValue);
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
}
@ -87,7 +87,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
delete settings["useIRCLayout"];
settings["layout"] = newValue;
localStorage.setItem("mx_local_settings", JSON.stringify(settings));
this.setObject("mx_local_settings", settings);
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
@ -95,7 +95,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
const settings = this.getSettings() || {};
settings[settingName] = newValue;
localStorage.setItem("mx_local_settings", JSON.stringify(settings));
this.setObject("mx_local_settings", settings);
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
@ -105,10 +105,6 @@ export default class DeviceSettingsHandler extends SettingsHandler {
return true; // It's their device, so they should be able to
}
public isSupported(): boolean {
return localStorage !== undefined && localStorage !== null;
}
public watchSetting(settingName: string, roomId: string, cb: CallbackFn) {
this.watchers.watchSetting(settingName, roomId, cb);
}
@ -118,9 +114,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
}
private getSettings(): any { // TODO: [TS] Type return
const value = localStorage.getItem("mx_local_settings");
if (!value) return null;
return JSON.parse(value);
return this.getObject("mx_local_settings");
}
// Note: features intentionally don't use the same key as settings to avoid conflicts
@ -132,7 +126,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
return false;
}
const value = localStorage.getItem("mx_labs_feature_" + featureName);
const value = this.getItem("mx_labs_feature_" + featureName);
if (value === "true") return true;
if (value === "false") return false;
// Try to read the next config level for the feature.
@ -140,7 +134,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
}
private writeFeature(featureName: string, enabled: boolean | null) {
localStorage.setItem("mx_labs_feature_" + featureName, `${enabled}`);
this.setItem("mx_labs_feature_" + featureName, `${enabled}`);
this.watchers.notifyUpdate(featureName, null, SettingLevel.DEVICE, enabled);
}
}