Skip to content

Commit

Permalink
nightmode: Don't change global state on load
Browse files Browse the repository at this point in the history
The multicasted functions were invoked on all tabs, also those where
nightmode was not running (d2x pages).
  • Loading branch information
larsjohnsen committed Mar 16, 2018
1 parent c7d477b commit 17c2796
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/modules/nightMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,23 @@ module.loadDynamicOptions = () => {
// To avoid the flash of unstyled content, the very first thing we should do is get a hold
// of the document object and add necessary classes...
if (typeof localStorage === 'object' && localStorage.getItem(localStorageKey)) {
// No need to check the background - we're in night mode for sure.
enableNightMode();
addNightmodeClass();
}
};

module.beforeLoad = () => {
// If night mode is enabled, set a localStorage token so that in the future,
// we can add the res-nightmode class to the page prior to page load.
if (isNightModeOn()) {
enableNightMode();
} else {
disableNightMode();
addNightmodeClass();
// Set a localStorage token so that in the future we can add nightmode to the page prior to page load.
localStorage.setItem(localStorageKey, 'true');
}
};

module.always = () => {
if (!Modules.isEnabled(module)) {
// Failsafe to disable night mode if the module is disabled
disableNightMode();
if (!Modules.isRunning(module)) {
// Remove nightmode if the localStorage token was incorrect
localStorage.removeItem(localStorageKey);
removeNightmodeClass();
}
};

Expand Down Expand Up @@ -407,16 +405,17 @@ const className = () => {
}
};

const addNightmodeClass = () => BodyClasses.add(className());
const removeNightmodeClass = () => BodyClasses.remove(className());

const enableNightMode = multicast(() => {
Options.set(module, 'nightModeOn', true);
localStorage.setItem(localStorageKey, 'true');
BodyClasses.add(className());
addNightmodeClass();
updateNightSwitch(true);
}, { name: 'enableNightMode' });

const disableNightMode = multicast(() => {
Options.set(module, 'nightModeOn', false);
localStorage.removeItem(localStorageKey);
BodyClasses.remove(className());
removeNightmodeClass();
updateNightSwitch(false);
}, { name: 'disableNightMode' });

0 comments on commit 17c2796

Please sign in to comment.