Improve handling of change listener on settings, fix bug that prevented callback from triggering

This commit is contained in:
Tamius Han 2020-02-09 01:16:46 +01:00
parent a5685fa420
commit c4914b4e5b
2 changed files with 21 additions and 17 deletions

View File

@ -15,17 +15,12 @@ class Settings {
constructor(options) {
// Options: activeSettings, updateCallback, logger
this.logger = options.logger;
const activeSettings = options.activeSettings;
const updateCallback = options.updateCallback;
this.active = activeSettings ? activeSettings : undefined;
this.onSettingsChanged = options.onSettingsChanged;
this.active = options.activeSettings ?? undefined;
this.default = ExtensionConf;
this.default['version'] = this.getExtensionVersion();
this.useSync = false;
this.version = undefined;
this.updateCallback = updateCallback;
const ths = this;
if (currentBrowser.firefox) {
browser.storage.onChanged.addListener((changes, area) => {this.storageChangeListener(changes, area)});
@ -39,19 +34,20 @@ class Settings {
return;
}
this.logger.log('info', 'settings', "[Settings::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:", changes, "storage area:", area);
if (changes['uwSettings'] && changes['uwSettings'].newValue) {
this.logger.log('info', 'settings',"[Settings::<storage/on change>] new settings object:", JSON.parse(changes.uwSettings.newValue));
}
// if (changes['uwSettings'] && changes['uwSettings'].newValue) {
// this.logger.log('info', 'settings',"[Settings::<storage/on change>] new settings object:", JSON.parse(changes.uwSettings.newValue));
// }
const parsedSettings = JSON.parse(changes.uwSettings.newValue);
if(changes['uwSettings'] && changes['uwSettings'].newValue) {
this.setActive(parsedSettings);
}
this.setActive(parsedSettings);
if(!parsedSettings.preventReload && this.updateCallback) {
this.logger.log('info', 'debug', 'Does parsedSettings.preventReload exist?', parsedSettings.preventReload, "Does callback exist?", !!this.onSettingsChanged);
if (!parsedSettings.preventReload && this.onSettingsChanged) {
try {
updateCallback(ths);
this.onSettingsChanged();
this.logger.log('info', 'settings', '[Settings] Update callback finished.')
} catch (e) {
this.logger.log('error', 'settings', "[Settings] CALLING UPDATE CALLBACK FAILED.")
this.logger.log('error', 'settings', "[Settings] CALLING UPDATE CALLBACK FAILED. Reason:", e)
}
}
}

View File

@ -44,6 +44,11 @@ class UW {
this.vuexStore = {};
}
reloadSettings() {
this.logger.log('info', 'debug', 'Things happened in the popup. Will reload extension settings.');
this.init();
}
async init(){
if (Debug.debug) {
console.log("[uw::main] loading configuration ...");
@ -112,7 +117,10 @@ class UW {
if (!this.settings) {
var ths = this;
this.settings = new Settings({updateCallback: (s) => {console.log("settings callback — ", s); ths.init()}, logger: this.logger});
this.settings = new Settings({
onSettingsChanged: () => this.reloadSettings(),
logger: this.logger
});
await this.settings.init();
}