Make settings as a clean architecture

This commit is contained in:
Shin'ya Ueoka 2019-05-11 08:04:01 +09:00
parent e76ca380f7
commit bacf83a320
16 changed files with 223 additions and 196 deletions

View file

@ -2,22 +2,18 @@ import InputComponent from './input';
import FollowComponent from './follow';
import MarkComponent from './mark';
import KeymapperComponent from './keymapper';
import * as settingActions from '../../actions/setting';
import * as messages from '../../../shared/messages';
import MessageListener from '../../MessageListener';
import * as blacklists from '../../../shared/blacklists';
import * as keys from '../../../shared/utils/keys';
import * as actions from '../../actions';
import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
import SettingUseCase from '../../usecases/SettingUseCase';
let addonEnabledUseCase = new AddonEnabledUseCase();
let settingUseCase = new SettingUseCase();
export default class Common {
private win: Window;
private store: any;
constructor(win: Window, store: any) {
const input = new InputComponent(win.document.body);
const follow = new FollowComponent(win);
@ -28,9 +24,6 @@ export default class Common {
input.onKey((key: keys.Key) => mark.key(key));
input.onKey((key: keys.Key) => keymapper.key(key));
this.win = win;
this.store = store;
this.reloadSettings();
new MessageListener().onBackgroundMessage(this.onMessage.bind(this));
@ -41,23 +34,22 @@ export default class Common {
case messages.SETTINGS_CHANGED:
return this.reloadSettings();
case messages.ADDON_TOGGLE_ENABLED:
addonEnabledUseCase.toggle();
return addonEnabledUseCase.toggle();
}
return undefined;
}
reloadSettings() {
async reloadSettings() {
try {
this.store.dispatch(settingActions.load())
.then((action: actions.SettingAction) => {
let enabled = !blacklists.includes(
action.settings.blacklist, this.win.location.href
);
if (enabled) {
addonEnabledUseCase.enable();
} else {
addonEnabledUseCase.disable();
}
});
let current = await settingUseCase.reload();
let disabled = blacklists.includes(
current.blacklist, window.location.href,
);
if (disabled) {
addonEnabledUseCase.disable();
} else {
addonEnabledUseCase.enable();
}
} catch (e) {
// Sometime sendMessage fails when background script is not ready.
console.warn(e);