consome as store/reducers

This commit is contained in:
Shin'ya Ueoka 2017-09-14 22:04:42 +09:00
parent 6127fdc285
commit 83cb277ba2
17 changed files with 171 additions and 236 deletions

View file

@ -1,55 +1,81 @@
import * as keys from './keys';
import * as inputActions from '../actions/input';
import * as operationActions from '../actions/operation';
import backgroundReducers from '../reducers/background';
import * as commandActions from '../actions/command';
import * as consoleActions from '../actions/console';
import reducers from '../reducers';
import commandReducer from '../reducers/command';
import messages from '../messages';
import * as store from '../store'
let prevInput = [];
const backgroundStore = store.createStore(reducers, (e) => {
console.error('Vim-Vixen:', e);
});
backgroundStore.subscribe(() => {
let currentInput = backgroundStore.getState().input
if (JSON.stringify(prevInput) === JSON.stringify(currentInput)) {
return
}
prevInput = currentInput;
if (currentInput.keys.length === 0) {
return;
}
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs.length > 0) {
return keyQueueChanged(tabs[0], backgroundStore.getState());
}
});
});
backgroundStore.subscribe(() => {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs.length > 0) {
return browser.tabs.sendMessage(tabs[0].id, {
type: 'state.changed',
state: backgroundStore.getState()
});
}
});
});
const keyQueueChanged = (sendToTab, state) => {
if (state.input.keys.length === 0) {
return Promise.resolve();
}
let prefix = keys.asKeymapChars(state.input.keys);
let matched = Object.keys(keys.defaultKeymap).filter((keys) => {
return keys.startsWith(prefix);
});
if (matched.length == 0) {
return handleMessage(inputActions.clearKeys(), sendToTab);
backgroundStore.dispatch(inputActions.clearKeys());
return Promise.resolve();
} else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) {
return Promise.resolve();
}
let action = keys.defaultKeymap[matched];
backgroundStore.dispatch(operationActions.exec(action, sendToTab));
return handleMessage(inputActions.clearKeys(), sendToTab).then(() => {
return backgroundReducers(undefined, action, sendToTab).then(() => {
return browser.tabs.sendMessage(sendToTab.id, action);
});
});
backgroundStore.dispatch(inputActions.clearKeys());
return browser.tabs.sendMessage(sendToTab.id, action);
};
const handleMessage = (action, sendToTab) => {
backgroundStore.dispatch(action);
return backgroundReducers(undefined, action, sendToTab).then(() => {
return commandReducer(undefined, action, sendToTab).then(() => {
return browser.tabs.sendMessage(sendToTab.id, action);
});
});
return browser.tabs.sendMessage(sendToTab.id, action);
};
browser.runtime.onMessage.addListener((action, sender) => {
handleMessage(action, sender.tab);
});
browser.runtime.onMessage.addListener((message) => {
switch (message.type) {
case messages.CONSOLE_BLURRED:
backgroundStore.dispatch(consoleActions.hide());
break;
case messages.CONSOLE_ENTERED:
backgroundStore.dispatch(commandActions.exec(message.text));
break;
case messages.CONSOLE_CHANGEED:
backgroundStore.dispatch(commandActions.complete(message.text));
break;
}
});