index reducer in background

jh-changes
Shin'ya Ueoka 7 years ago
parent fc90f78e26
commit 157ebaef9c
  1. 12
      src/background/components/background.js
  2. 6
      src/background/index.js
  3. 12
      src/background/reducers/index.js

@ -7,7 +7,6 @@ import * as commands from 'shared/commands';
export default class BackgroundComponent {
constructor(store) {
this.store = store;
this.setting = {};
browser.runtime.onMessage.addListener((message, sender) => {
try {
@ -21,11 +20,8 @@ export default class BackgroundComponent {
});
}
update() {
this.settings = this.store.getState();
}
onMessage(message, sender) {
let settings = this.store.getState().setting;
switch (message.type) {
case messages.BACKGROUND_OPERATION:
return this.store.dispatch(
@ -43,16 +39,16 @@ export default class BackgroundComponent {
type: messages.CONSOLE_HIDE_COMMAND,
});
case messages.CONSOLE_ENTERED:
return commands.exec(message.text, this.settings.value).catch((e) => {
return commands.exec(message.text, settings.value).catch((e) => {
return browser.tabs.sendMessage(sender.tab.id, {
type: messages.CONSOLE_SHOW_ERROR,
text: e.message,
});
});
case messages.SETTINGS_QUERY:
return Promise.resolve(this.store.getState().value);
return Promise.resolve(this.store.getState().setting.value);
case messages.CONSOLE_QUERY_COMPLETIONS:
return commands.complete(message.text, this.settings.value);
return commands.complete(message.text, settings.value);
case messages.SETTINGS_RELOAD:
this.store.dispatch(settingsActions.load());
return this.broadcastSettingsChanged();

@ -1,7 +1,7 @@
import * as settingsActions from 'settings/actions/setting';
import messages from 'shared/messages';
import BackgroundComponent from 'background/components/background';
import reducers from 'settings/reducers/setting';
import reducers from 'background/reducers';
import { createStore } from 'shared/store';
const store = createStore(reducers, (e, sender) => {
@ -13,9 +13,7 @@ const store = createStore(reducers, (e, sender) => {
});
}
});
// eslint-disable-next-line no-unused-vars
const backgroundComponent = new BackgroundComponent(store);
store.subscribe((sender) => {
backgroundComponent.update(sender);
});
store.dispatch(settingsActions.load());

@ -0,0 +1,12 @@
import settingReducer from 'settings/reducers/setting';
// Make setting reducer instead of re-use
const defaultState = {
setting: settingReducer(undefined, {}),
};
export default function reducer(state = defaultState, action = {}) {
return Object.assign({}, state, {
setting: settingReducer(state.setting, action),
});
}