use input as store/reducer

jh-changes
Shin'ya Ueoka 7 years ago
parent c42ac8fac4
commit 6127fdc285
  1. 12
      src/actions/operation.js
  2. 45
      src/background/index.js
  3. 8
      src/reducers/background.js
  4. 10
      src/reducers/command.js
  5. 6
      src/reducers/index.js

@ -2,18 +2,18 @@ import operations from '../operations';
import * as tabs from '../background/tabs'; import * as tabs from '../background/tabs';
import * as zooms from '../background/zooms'; import * as zooms from '../background/zooms';
export function exec(operation, sender) { export function exec(operation, tab) {
switch (operation.type) { switch (operation.type) {
case operations.TABS_CLOSE: case operations.TABS_CLOSE:
return tabs.closeTab(sender.tab.id); return tabs.closeTab(tab.id);
case operations.TABS_REOPEN: case operations.TABS_REOPEN:
return tabs.reopenTab(); return tabs.reopenTab();
case operations.TABS_PREV: case operations.TABS_PREV:
return tabs.selectPrevTab(sender.tab.index, operation.count); return tabs.selectPrevTab(tab.index, operation.count);
case operations.TABS_NEXT: case operations.TABS_NEXT:
return tabs.selectNextTab(sender.tab.index, operation.count); return tabs.selectNextTab(tab.index, operation.count);
case operations.TABS_RELOAD: case operations.TABS_RELOAD:
return tabs.reload(sender.tab, operation.cache); return tabs.reload(tab, operation.cache);
case operations.ZOOM_IN: case operations.ZOOM_IN:
return zooms.zoomIn(); return zooms.zoomIn();
case operations.ZOOM_OUT: case operations.ZOOM_OUT:
@ -21,7 +21,7 @@ export function exec(operation, sender) {
case operations.ZOOM_NEUTRAL: case operations.ZOOM_NEUTRAL:
return zooms.neutral(); return zooms.neutral();
default: default:
return browser.tabs.sendMessage(sender.tab.id, { return browser.tabs.sendMessage(tab.id, {
type: 'require.content.operation', type: 'require.content.operation',
operation operation
}); });

@ -4,49 +4,52 @@ import * as operationActions from '../actions/operation';
import backgroundReducers from '../reducers/background'; import backgroundReducers from '../reducers/background';
import reducers from '../reducers'; import reducers from '../reducers';
import commandReducer from '../reducers/command'; import commandReducer from '../reducers/command';
import inputReducers from '../reducers/input';
import * as store from '../store' import * as store from '../store'
const backgroundStore = store.createStore(reducers, (e) => { const backgroundStore = store.createStore(reducers, (e) => {
console.error('Vim-Vixen:', e); console.error('Vim-Vixen:', e);
}); });
let inputState = inputReducers(undefined, {}); backgroundStore.subscribe(() => {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs.length > 0) {
return keyQueueChanged(tabs[0], backgroundStore.getState());
}
});
});
const keyQueueChanged = (sender, prevState, state) => { const keyQueueChanged = (sendToTab, state) => {
if (state.keys.length === 0) { if (state.input.keys.length === 0) {
return Promise.resolve(); return Promise.resolve();
} }
let prefix = keys.asKeymapChars(state.keys); let prefix = keys.asKeymapChars(state.input.keys);
let matched = Object.keys(keys.defaultKeymap).filter((keys) => { let matched = Object.keys(keys.defaultKeymap).filter((keys) => {
return keys.startsWith(prefix); return keys.startsWith(prefix);
}); });
if (matched.length == 0) { if (matched.length == 0) {
return handleMessage(inputActions.clearKeys(), sender); return handleMessage(inputActions.clearKeys(), sendToTab);
} else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) { } else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) {
return Promise.resolve(); return Promise.resolve();
} }
let action = keys.defaultKeymap[matched]; let action = keys.defaultKeymap[matched];
backgroundStore.dispatch(operationActions.exec(action, sender)); backgroundStore.dispatch(operationActions.exec(action, sendToTab));
return handleMessage(inputActions.clearKeys(), sender).then(() => { return handleMessage(inputActions.clearKeys(), sendToTab).then(() => {
return backgroundReducers(undefined, action, sender).then(() => { return backgroundReducers(undefined, action, sendToTab).then(() => {
return browser.tabs.sendMessage(sender.tab.id, action); return browser.tabs.sendMessage(sendToTab.id, action);
}); });
}); });
}; };
const handleMessage = (action, sender) => { const handleMessage = (action, sendToTab) => {
let nextInputState = inputReducers(inputState, action); backgroundStore.dispatch(action);
if (JSON.stringify(nextInputState) !== JSON.stringify(inputState)) {
let prevState = inputState; return backgroundReducers(undefined, action, sendToTab).then(() => {
inputState = nextInputState; return commandReducer(undefined, action, sendToTab).then(() => {
return keyQueueChanged(sender, prevState, inputState); return browser.tabs.sendMessage(sendToTab.id, action);
}
return backgroundReducers(undefined, action, sender).then(() => {
return commandReducer(undefined, action, sender).then(() => {
return browser.tabs.sendMessage(sender.tab.id, action);
}); });
}); });
}; };
browser.runtime.onMessage.addListener(handleMessage); browser.runtime.onMessage.addListener((action, sender) => {
handleMessage(action, sender.tab);
});

@ -2,7 +2,7 @@ import * as tabs from '../background/tabs';
import * as consoleActions from '../actions/console'; import * as consoleActions from '../actions/console';
import actions from '../actions'; import actions from '../actions';
const doCompletion = (command, keywords, sender) => { const doCompletion = (command, keywords, tabId) => {
if (command === 'buffer') { if (command === 'buffer') {
return tabs.getCompletions(keywords).then((tabs) => { return tabs.getCompletions(keywords).then((tabs) => {
let items = tabs.map((tab) => { let items = tabs.map((tab) => {
@ -18,18 +18,18 @@ const doCompletion = (command, keywords, sender) => {
items: items items: items
}; };
return browser.tabs.sendMessage( return browser.tabs.sendMessage(
sender, tabId,
consoleActions.setCompletions([completions])); consoleActions.setCompletions([completions]));
}); });
} }
return Promise.resolve(); return Promise.resolve();
}; };
export default function reducer(state, action = {}, sender) { export default function reducer(state, action = {}, sendToTab) {
// TODO hide sender object // TODO hide sender object
switch (action.type) { switch (action.type) {
case actions.BACKGROUND_REQUEST_COMPLETIONS: case actions.BACKGROUND_REQUEST_COMPLETIONS:
return doCompletion(action.command, action.keywords, sender.tab.id); return doCompletion(action.command, action.keywords, sendToTab.id);
default: default:
return Promise.resolve(); return Promise.resolve();
} }

@ -1,23 +1,23 @@
import * as tabs from '../background/tabs'; import * as tabs from '../background/tabs';
import actions from '../actions'; import actions from '../actions';
const cmdBuffer = (sender, arg) => { const cmdBuffer = (tab, arg) => {
if (isNaN(arg)) { if (isNaN(arg)) {
return tabs.selectByKeyword(sender.tab, arg); return tabs.selectByKeyword(tab, arg);
} else { } else {
let index = parseInt(arg, 10) - 1; let index = parseInt(arg, 10) - 1;
return tabs.selectAt(index); return tabs.selectAt(index);
} }
} }
export default function reducer(state, action, sender) { export default function reducer(state, action, sendToTab) {
switch (action.type) { switch (action.type) {
case actions.COMMAND_OPEN_URL: case actions.COMMAND_OPEN_URL:
return browser.tabs.update(sender.tab.id, { url: action.url }); return browser.tabs.update(sendToTab.id, { url: action.url });
case actions.COMMAND_TABOPEN_URL: case actions.COMMAND_TABOPEN_URL:
return browser.tabs.create({ url: action.url }); return browser.tabs.create({ url: action.url });
case actions.COMMAND_BUFFER: case actions.COMMAND_BUFFER:
return cmdBuffer(sender, action.keywords); return cmdBuffer(sendToTab, action.keywords);
default: default:
return Promise.resolve(); return Promise.resolve();
} }

@ -1,7 +1,11 @@
import inputReducer from '../reducers/input';
const defaultState = { const defaultState = {
input: inputReducer(undefined, {})
}; };
export default function reducer(state = defaultState/*, action = {}*/) { export default function reducer(state = defaultState, action = {}) {
return Object.assign({}, state, { return Object.assign({}, state, {
input: inputReducer(state.input, action)
}); });
} }