Improve handling of change listener on settings, fix bug that prevented callback from triggering
This commit is contained in:
parent
a5685fa420
commit
c4914b4e5b
@ -15,17 +15,12 @@ class Settings {
|
|||||||
constructor(options) {
|
constructor(options) {
|
||||||
// Options: activeSettings, updateCallback, logger
|
// Options: activeSettings, updateCallback, logger
|
||||||
this.logger = options.logger;
|
this.logger = options.logger;
|
||||||
const activeSettings = options.activeSettings;
|
this.onSettingsChanged = options.onSettingsChanged;
|
||||||
const updateCallback = options.updateCallback;
|
this.active = options.activeSettings ?? undefined;
|
||||||
|
|
||||||
this.active = activeSettings ? activeSettings : undefined;
|
|
||||||
this.default = ExtensionConf;
|
this.default = ExtensionConf;
|
||||||
this.default['version'] = this.getExtensionVersion();
|
this.default['version'] = this.getExtensionVersion();
|
||||||
this.useSync = false;
|
this.useSync = false;
|
||||||
this.version = undefined;
|
this.version = undefined;
|
||||||
this.updateCallback = updateCallback;
|
|
||||||
|
|
||||||
const ths = this;
|
|
||||||
|
|
||||||
if (currentBrowser.firefox) {
|
if (currentBrowser.firefox) {
|
||||||
browser.storage.onChanged.addListener((changes, area) => {this.storageChangeListener(changes, area)});
|
browser.storage.onChanged.addListener((changes, area) => {this.storageChangeListener(changes, area)});
|
||||||
@ -39,19 +34,20 @@ class Settings {
|
|||||||
return;
|
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);
|
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) {
|
// if (changes['uwSettings'] && changes['uwSettings'].newValue) {
|
||||||
this.logger.log('info', 'settings',"[Settings::<storage/on change>] new settings object:", JSON.parse(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);
|
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 {
|
try {
|
||||||
updateCallback(ths);
|
this.onSettingsChanged();
|
||||||
|
this.logger.log('info', 'settings', '[Settings] Update callback finished.')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.log('error', 'settings', "[Settings] CALLING UPDATE CALLBACK FAILED.")
|
this.logger.log('error', 'settings', "[Settings] CALLING UPDATE CALLBACK FAILED. Reason:", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,11 @@ class UW {
|
|||||||
this.vuexStore = {};
|
this.vuexStore = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reloadSettings() {
|
||||||
|
this.logger.log('info', 'debug', 'Things happened in the popup. Will reload extension settings.');
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
async init(){
|
async init(){
|
||||||
if (Debug.debug) {
|
if (Debug.debug) {
|
||||||
console.log("[uw::main] loading configuration ...");
|
console.log("[uw::main] loading configuration ...");
|
||||||
@ -112,7 +117,10 @@ class UW {
|
|||||||
|
|
||||||
if (!this.settings) {
|
if (!this.settings) {
|
||||||
var ths = this;
|
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();
|
await this.settings.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user