From 2c40d239f0962f1442f357c0697e4c8aa2eafd31 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 10 Sep 2017 17:27:55 +0900 Subject: [PATCH] background command from reducer --- src/actions/index.js | 11 ++++++++++- src/background/index.js | 32 ++++---------------------------- src/background/key-queue.js | 19 ++++++++++--------- src/reducers/background.js | 29 ++++++++++++++++++++++------- src/shared/actions.js | 23 ----------------------- 5 files changed, 46 insertions(+), 68 deletions(-) diff --git a/src/actions/index.js b/src/actions/index.js index 8f22193..bc44fec 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -4,5 +4,14 @@ export default { CONSOLE_SHOW_ERROR: 'vimvixen.console.show.error', CONSOLE_HIDE: 'vimvixen.console.hide', - BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions' + BACKGROUND_REQUEST_COMPLETIONS: 'vimvixen.background.request.completions', + + TABS_CLOSE: 'tabs.close', + TABS_REOPEN: 'tabs.reopen', + TABS_PREV: 'tabs.prev', + TABS_NEXT: 'tabs.next', + TABS_RELOAD: 'tabs.reload', + ZOOM_IN: 'zoom.in', + ZOOM_OUT: 'zoom.out', + ZOOM_NEUTRAL: 'zoom.neutral', }; diff --git a/src/background/index.js b/src/background/index.js index a80d1ea..cb7a3ff 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,6 +1,5 @@ import * as actions from '../shared/actions'; import * as tabs from './tabs'; -import * as zooms from './zooms'; import KeyQueue from './key-queue'; import backgroundReducers from '../reducers/background'; @@ -15,36 +14,13 @@ const keyPressHandle = (request, sender) => { return Promise.resolve(); } - if (actions.isBackgroundAction(action.type)) { - return doBackgroundAction(sender, action); - } else if (actions.isContentAction(action.type)) { + if (actions.isContentAction(action.type)) { return browser.tabs.sendMessage(sender.tab.id, action); + } else { + return backgroundReducers(undefined, action, sender); } - return Promise.resolve(); }; -const doBackgroundAction = (sender, action) => { - switch(action.type) { - case actions.TABS_CLOSE: - return tabs.closeTab(sender.tab.id); - case actions.TABS_REOPEN: - return tabs.reopenTab(); - case actions.TABS_PREV: - return tabs.selectPrevTab(sender.tab.index, action.count); - case actions.TABS_NEXT: - return tabs.selectNextTab(sender.tab.index, action.count); - case actions.TABS_RELOAD: - return tabs.reload(sender.tab, actions.cache); - case actions.ZOOM_IN: - return zooms.zoomIn(); - case actions.ZOOM_OUT: - return zooms.zoomOut(); - case actions.ZOOM_NEUTRAL: - return zooms.neutral(); - } - return Promise.resolve(); -} - const normalizeUrl = (string) => { try { return new URL(string).href @@ -88,5 +64,5 @@ browser.runtime.onMessage.addListener((request, sender) => { }); browser.runtime.onMessage.addListener((action, sender) => { - return backgroundReducers(undefined, action, sender.tab.id); + return backgroundReducers(undefined, action, sender); }); diff --git a/src/background/key-queue.js b/src/background/key-queue.js index b2f5a34..25f1d24 100644 --- a/src/background/key-queue.js +++ b/src/background/key-queue.js @@ -1,3 +1,4 @@ +import newActions from '../actions'; import * as actions from '../shared/actions'; const DEFAULT_KEYMAP = { @@ -17,15 +18,15 @@ const DEFAULT_KEYMAP = { '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 }, + 'd': { type: newActions.TABS_CLOSE }, + 'u': { type: newActions.TABS_REOPEN }, + 'h': { type: newActions.TABS_PREV, count: 1 }, + 'l': { type: newActions.TABS_NEXT, count: 1 }, + 'r': { type: newActions.TABS_RELOAD, cache: false }, + 'R': { type: newActions.TABS_RELOAD, cache: true }, + 'zi': { type: newActions.ZOOM_IN }, + 'zo': { type: newActions.ZOOM_OUT }, + 'zz': { type: newActions.ZOOM_NEUTRAL }, 'f': { type: actions.FOLLOW_START, newTab: false }, 'F': { type: actions.FOLLOW_START, newTab: true }, 'H': { type: actions.HISTORY_PREV }, diff --git a/src/reducers/background.js b/src/reducers/background.js index eccc8ca..d7d7860 100644 --- a/src/reducers/background.js +++ b/src/reducers/background.js @@ -1,8 +1,9 @@ import * as tabs from '../background/tabs'; +import * as zooms from '../background/zooms'; import * as consoleActions from '../actions/console'; import actions from '../actions'; -const doCompletion = (command, keywords, sendto) => { +const doCompletion = (command, keywords, sender) => { if (command === 'buffer') { return tabs.getCompletions(keywords).then((tabs) => { let items = tabs.map((tab) => { @@ -18,20 +19,34 @@ const doCompletion = (command, keywords, sendto) => { items: items }; return browser.tabs.sendMessage( - sendto, + sender, consoleActions.setCompletions([completions])); }); } return Promise.resolve(); }; - - -export default function reducer(state, action = {}, sendto) { - // TODO hide sendto object +export default function reducer(state, action = {}, sender) { + // TODO hide sender object switch (action.type) { case actions.BACKGROUND_REQUEST_COMPLETIONS: - return doCompletion(action.command, action.keywords, sendto); + return doCompletion(action.command, action.keywords, sender.tab.id); + case actions.TABS_CLOSE: + return tabs.closeTab(sender.tab.id); + case actions.TABS_REOPEN: + return tabs.reopenTab(); + case actions.TABS_PREV: + return tabs.selectPrevTab(sender.tab.index, action.count); + case actions.TABS_NEXT: + return tabs.selectNextTab(sender.tab.index, action.count); + case actions.TABS_RELOAD: + return tabs.reload(sender.tab, action.cache); + case actions.ZOOM_IN: + return zooms.zoomIn(); + case actions.ZOOM_OUT: + return zooms.zoomOut(); + case actions.ZOOM_NEUTRAL: + return zooms.neutral(); default: return Promise.resolve(); } diff --git a/src/shared/actions.js b/src/shared/actions.js index 7151dd1..5163cf3 100644 --- a/src/shared/actions.js +++ b/src/shared/actions.js @@ -1,11 +1,6 @@ export const CMD_OPEN = 'cmd.open'; export const CMD_TABS_OPEN = 'cmd.tabs.open'; export const CMD_BUFFER = 'cmd.buffer'; -export const TABS_CLOSE = 'tabs.close'; -export const TABS_REOPEN = 'tabs.reopen'; -export const TABS_PREV = 'tabs.prev'; -export const TABS_NEXT = 'tabs.next'; -export const TABS_RELOAD = 'tabs.reload'; export const SCROLL_LINES = 'scroll.lines'; export const SCROLL_PAGES = 'scroll.pages'; export const SCROLL_TOP = 'scroll.top'; @@ -15,20 +10,6 @@ export const SCROLL_RIGHT= 'scroll.right'; export const FOLLOW_START = 'follow.start'; export const HISTORY_PREV = 'history.prev'; export const HISTORY_NEXT = 'history.next'; -export const ZOOM_IN = 'zoom.in'; -export const ZOOM_OUT = 'zoom.out'; -export const ZOOM_NEUTRAL = 'zoom.neutral'; - -const BACKGROUND_ACTION_SET = new Set([ - TABS_CLOSE, - TABS_REOPEN, - TABS_PREV, - TABS_NEXT, - TABS_RELOAD, - ZOOM_IN, - ZOOM_OUT, - ZOOM_NEUTRAL -]); const CONTENT_ACTION_SET = new Set([ CMD_OPEN, @@ -45,10 +26,6 @@ const CONTENT_ACTION_SET = new Set([ HISTORY_NEXT ]); -export const isBackgroundAction = (action) => { - return BACKGROUND_ACTION_SET.has(action); -}; - export const isContentAction = (action) => { return CONTENT_ACTION_SET.has(action); };