improve store and reducers

jh-changes
Shin'ya Ueoka 7 years ago
parent 9dc02b2fd8
commit c42ac8fac4
  1. 8
      src/background/index.js
  2. 7
      src/reducers/index.js
  3. 21
      src/store/index.js

@ -2,12 +2,12 @@ import * as keys from './keys';
import * as inputActions from '../actions/input'; import * as inputActions from '../actions/input';
import * as operationActions from '../actions/operation'; import * as operationActions from '../actions/operation';
import backgroundReducers from '../reducers/background'; import backgroundReducers from '../reducers/background';
import reducers from '../reducers';
import commandReducer from '../reducers/command'; import commandReducer from '../reducers/command';
import inputReducers from '../reducers/input'; import inputReducers from '../reducers/input';
import * as store from '../store' import * as store from '../store'
const emptyReducer = (state) => state; const backgroundStore = store.createStore(reducers, (e) => {
const emptyStore = store.createStore(emptyReducer, (e) => {
console.error('Vim-Vixen:', e); console.error('Vim-Vixen:', e);
}); });
let inputState = inputReducers(undefined, {}); let inputState = inputReducers(undefined, {});
@ -27,9 +27,7 @@ const keyQueueChanged = (sender, prevState, state) => {
return Promise.resolve(); return Promise.resolve();
} }
let action = keys.defaultKeymap[matched]; let action = keys.defaultKeymap[matched];
emptyStore.dispatch(operationActions.exec(action, sender), (e) => { backgroundStore.dispatch(operationActions.exec(action, sender));
console.error('Vim-Vixen:', e);
});
return handleMessage(inputActions.clearKeys(), sender).then(() => { return handleMessage(inputActions.clearKeys(), sender).then(() => {
return backgroundReducers(undefined, action, sender).then(() => { return backgroundReducers(undefined, action, sender).then(() => {
return browser.tabs.sendMessage(sender.tab.id, action); return browser.tabs.sendMessage(sender.tab.id, action);

@ -0,0 +1,7 @@
const defaultState = {
};
export default function reducer(state = defaultState/*, action = {}*/) {
return Object.assign({}, state, {
});
}

@ -3,21 +3,38 @@ class Store {
this.reducer = reducer; this.reducer = reducer;
this.catcher = catcher; this.catcher = catcher;
this.state = this.reducer(undefined, {}); this.state = this.reducer(undefined, {});
this.subscribers = [];
} }
dispatch(action) { dispatch(action) {
if (action instanceof Promise) { if (action instanceof Promise) {
action.then((a) => { action.then((a) => {
this.state = this.reducer(this.state, a); this.transitNext(a);
}).catch(this.catcher) }).catch(this.catcher)
} else { } else {
try { try {
this.state = this.reducer(this.state, action); this.transitNext(action);
} catch (e) { } catch (e) {
this.catcher(e); this.catcher(e);
} }
} }
} }
getState() {
return this.state;
}
subscribe(callback) {
this.subscribers.push(callback);
}
transitNext(action) {
let newState = this.reducer(this.state, action);
if (JSON.stringify(this.state) !== JSON.stringify(newState)) {
this.state = newState;
this.subscribers.forEach(f => f.call())
}
}
} }
const empty = () => {}; const empty = () => {};