diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-14 22:04:42 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-16 21:08:18 +0900 |
commit | 83cb277ba2af2bc2f87ace1d97fa582a7043bcd5 (patch) | |
tree | 1ff5eb02fc6b7b12d0a1e825ba1fdb889a1e095b /src/background | |
parent | 6127fdc285bc430b48259bd6e90b69623b4e76cc (diff) |
consome as store/reducers
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/index.js | 60 | ||||
-rw-r--r-- | src/background/keys.js | 9 |
2 files changed, 47 insertions, 22 deletions
diff --git a/src/background/index.js b/src/background/index.js index d56fab8..d2cfeb4 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -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; + } +}); diff --git a/src/background/keys.js b/src/background/keys.js index 0f73bf0..8d75aba 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -1,11 +1,10 @@ -import actions from '../actions'; import operations from '../operations'; const defaultKeymap = { - ':': { type: actions.CMD_OPEN }, - 'o': { type: actions.CMD_TABS_OPEN, alter: false }, - 'O': { type: actions.CMD_TABS_OPEN, alter: true }, - 'b': { type: actions.CMD_BUFFER }, + ':': { type: operations.COMMAND_OPEN }, + 'o': { type: operations.COMMAND_TABS_OPEN, alter: false }, + 'O': { type: operations.COMMAND_TABS_OPEN, alter: true }, + 'b': { type: operations.COMMAND_BUFFER }, 'k': { type: operations.SCROLL_LINES, count: -1 }, 'j': { type: operations.SCROLL_LINES, count: 1 }, '<C-E>': { type: operations.SCROLL_LINES, count: -1 }, |