diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-16 23:32:19 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-09-16 23:32:19 +0900 |
commit | c5529958d53146c8c6826673abe6431a19f1924d (patch) | |
tree | 4952c7ac1ded91d52fd6e424c229022b61b67aa3 /src/background | |
parent | b2cddcd69b4ae06770d66808624fc43f3dcbcb0e (diff) | |
parent | ae394e28c0cbc8710d4937238c97328afddbca0f (diff) |
Merge branch 'more-redux'
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/index.js | 86 | ||||
-rw-r--r-- | src/background/keys.js | 60 |
2 files changed, 88 insertions, 58 deletions
diff --git a/src/background/index.js b/src/background/index.js index e72cab0..ef1b881 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,45 +1,75 @@ import * as keys from './keys'; import * as inputActions from '../actions/input'; -import backgroundReducers from '../reducers/background'; -import commandReducer from '../reducers/command'; -import inputReducers from '../reducers/input'; +import * as operationActions from '../actions/operation'; +import * as commandActions from '../actions/command'; +import * as consoleActions from '../actions/console'; +import reducers from '../reducers'; +import messages from '../messages'; +import * as store from '../store' -let inputState = inputReducers(undefined, {}); +let prevInput = []; +const backgroundStore = store.createStore(reducers, (e, sender) => { + console.error('Vim-Vixen:', e); + if (sender) { + backgroundStore.dispatch(consoleActions.showError(e.message), sender); + } +}); +backgroundStore.subscribe((sender) => { + let currentInput = backgroundStore.getState().input + if (JSON.stringify(prevInput) === JSON.stringify(currentInput)) { + return + } + prevInput = currentInput; -const keyQueueChanged = (sender, prevState, state) => { - if (state.keys.length === 0) { - return Promise.resolve(); + if (currentInput.keys.length === 0) { + return; + } + if (sender) { + return keyQueueChanged(backgroundStore.getState(), sender); } +}); +backgroundStore.subscribe((sender) => { + if (sender) { + return browser.tabs.sendMessage(sender.tab.id, { + type: messages.STATE_UPDATE, + state: backgroundStore.getState() + }); + } +}); - let prefix = keys.asKeymapChars(state.keys); +const keyQueueChanged = (state, sender) => { + 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(), sender); + backgroundStore.dispatch(inputActions.clearKeys(), sender); + return Promise.resolve(); } else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) { return Promise.resolve(); } let action = keys.defaultKeymap[matched]; - return handleMessage(inputActions.clearKeys(), sender).then(() => { - return backgroundReducers(undefined, action, sender).then(() => { - return browser.tabs.sendMessage(sender.tab.id, action); - }); - }); + backgroundStore.dispatch(operationActions.exec(action, sender.tab), sender); + backgroundStore.dispatch(inputActions.clearKeys(), sender); }; -const handleMessage = (action, sender) => { - let nextInputState = inputReducers(inputState, action); - if (JSON.stringify(nextInputState) !== JSON.stringify(inputState)) { - let prevState = inputState; - inputState = nextInputState; - return keyQueueChanged(sender, prevState, inputState); - } - return backgroundReducers(undefined, action, sender).then(() => { - return commandReducer(undefined, action, sender).then(() => { - return browser.tabs.sendMessage(sender.tab.id, action); - }); - }); -}; +const handleMessage = (message, sender) => { + switch (message.type) { + case messages.KEYDOWN: + return backgroundStore.dispatch(inputActions.keyPress(message.code, message.ctrl), sender); + case messages.CONSOLE_BLURRED: + return backgroundStore.dispatch(consoleActions.hide(), sender); + case messages.CONSOLE_ENTERED: + return backgroundStore.dispatch(commandActions.exec(message.text), sender); + case messages.CONSOLE_CHANGEED: + return backgroundStore.dispatch(commandActions.complete(message.text), sender); + } +} -browser.runtime.onMessage.addListener(handleMessage); +browser.runtime.onMessage.addListener((message, sender) => { + try { + handleMessage(message, sender); + } catch (e) { + backgroundStore.dispatch(consoleActions.showError(e.message), sender); + } +}); diff --git a/src/background/keys.js b/src/background/keys.js index 0ce53fa..8d75aba 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -1,35 +1,35 @@ -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 }, - 'k': { type: actions.SCROLL_LINES, count: -1 }, - 'j': { type: actions.SCROLL_LINES, count: 1 }, - '<C-E>': { type: actions.SCROLL_LINES, count: -1 }, - '<C-Y>': { type: actions.SCROLL_LINES, count: 1 }, - '<C-U>': { type: actions.SCROLL_PAGES, count: -0.5 }, - '<C-D>': { type: actions.SCROLL_PAGES, count: 0.5 }, - '<C-B>': { type: actions.SCROLL_PAGES, count: -1 }, - '<C-F>': { type: actions.SCROLL_PAGES, count: 1 }, - 'gg': { type: actions.SCROLL_TOP }, - 'G': { type: actions.SCROLL_BOTTOM }, - '0': { type: actions.SCROLL_LEFT }, - '$': { type: actions.SCROLL_RIGHT }, - 'd': { type: actions.TABS_CLOSE }, - 'u': { type: actions.TABS_REOPEN }, - 'h': { type: actions.TABS_PREV, count: 1 }, - 'l': { type: actions.TABS_NEXT, count: 1 }, - 'r': { type: actions.TABS_RELOAD, cache: false }, - 'R': { type: actions.TABS_RELOAD, cache: true }, - 'zi': { type: actions.ZOOM_IN }, - 'zo': { type: actions.ZOOM_OUT }, - 'zz': { type: actions.ZOOM_NEUTRAL }, - 'f': { type: actions.FOLLOW_START, newTab: false }, - 'F': { type: actions.FOLLOW_START, newTab: true }, - 'H': { type: actions.HISTORY_PREV }, - 'L': { type: actions.HISTORY_NEXT }, + ':': { 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 }, + '<C-Y>': { type: operations.SCROLL_LINES, count: 1 }, + '<C-U>': { type: operations.SCROLL_PAGES, count: -0.5 }, + '<C-D>': { type: operations.SCROLL_PAGES, count: 0.5 }, + '<C-B>': { type: operations.SCROLL_PAGES, count: -1 }, + '<C-F>': { type: operations.SCROLL_PAGES, count: 1 }, + 'gg': { type: operations.SCROLL_TOP }, + 'G': { type: operations.SCROLL_BOTTOM }, + '0': { type: operations.SCROLL_LEFT }, + '$': { type: operations.SCROLL_RIGHT }, + 'd': { type: operations.TABS_CLOSE }, + 'u': { type: operations.TABS_REOPEN }, + 'h': { type: operations.TABS_PREV, count: 1 }, + 'l': { type: operations.TABS_NEXT, count: 1 }, + 'r': { type: operations.TABS_RELOAD, cache: false }, + 'R': { type: operations.TABS_RELOAD, cache: true }, + 'zi': { type: operations.ZOOM_IN }, + 'zo': { type: operations.ZOOM_OUT }, + 'zz': { type: operations.ZOOM_NEUTRAL }, + 'f': { type: operations.FOLLOW_START, newTab: false }, + 'F': { type: operations.FOLLOW_START, newTab: true }, + 'H': { type: operations.HISTORY_PREV }, + 'L': { type: operations.HISTORY_NEXT }, } const asKeymapChars = (keys) => { |