diff options
author | Jason <jason.daurus@gmail.com> | 2016-06-16 12:20:32 +0800 |
---|---|---|
committer | Jason <jason.daurus@gmail.com> | 2016-06-17 05:08:05 +0800 |
commit | 99b2b8633db4fa3fa21d43f3e6a27694b00908e4 (patch) | |
tree | 93af977c2bcec8d9cae2d0e6098131f921e07a37 /web/src/js/ducks | |
parent | 1fc2db85fa339f9b134d45c15d2ad4cf3d681070 (diff) | |
download | mitmproxy-99b2b8633db4fa3fa21d43f3e6a27694b00908e4.tar.gz mitmproxy-99b2b8633db4fa3fa21d43f3e6a27694b00908e4.tar.bz2 mitmproxy-99b2b8633db4fa3fa21d43f3e6a27694b00908e4.zip |
[web] settings store
Diffstat (limited to 'web/src/js/ducks')
-rw-r--r-- | web/src/js/ducks/index.js | 2 | ||||
-rw-r--r-- | web/src/js/ducks/settings.js | 81 |
2 files changed, 83 insertions, 0 deletions
diff --git a/web/src/js/ducks/index.js b/web/src/js/ducks/index.js index fee4d792..ffde1a64 100644 --- a/web/src/js/ducks/index.js +++ b/web/src/js/ducks/index.js @@ -2,12 +2,14 @@ import {combineReducers} from 'redux' import eventLog from './eventLog' import websocket from './websocket' import flows from './flows' +import settings from './settings' import ui from './ui' const rootReducer = combineReducers({ eventLog, websocket, flows, + settings, ui }) diff --git a/web/src/js/ducks/settings.js b/web/src/js/ducks/settings.js new file mode 100644 index 00000000..8513c2f1 --- /dev/null +++ b/web/src/js/ducks/settings.js @@ -0,0 +1,81 @@ +import { StoreCmds } from '../actions' + +export const WS_MSG_TYPE = 'settings' +export const WS_MSG_CMD_RESET = 'reset' +export const WS_MSG_CMD_UPDATE = 'update' + +export const BEGIN_FETCH = 'SETTINGS_BEGIN_FETCH' +export const FETCH_SETTINGS = 'SETTINGS_FETCH_SETTINGS' +export const FETCH_ERROR = 'SETTINGS_FETCH_ERROR' +export const RECV_WS_MSG = 'SETTINGS_RECV_WS_MSG' + +const defaultState = { settings: {}, pendings: null, req: null } + +function reduceData(data, action) { + switch (action.cmd) { + + case WS_MSG_CMD_RESET: + return action.data || {} + + case WS_MSG_CMD_UPDATE: + return _.merge({}, data.settings, action.data) + + default: + return data + } +} + +export default function reduce(state = defaultState, action) { + switch (action.type) { + + case BEGIN_FETCH: + return { ...state, pendings: [], req: action.req } + + case FETCH_SETTINGS: + const pendings = state.pendings || [] + return { ...state, pendings: null, settings: pendings.reduce(reduceData, data) } + + case RECV_WS_MSG: + if (state.pendings) { + return { ...state, pendings: state.pendings.concat(action) } + } + return { ...state, settings: reduceData(state.settings, action) } + + default: + return state + } +} + +export function fetch() { + return dispatch => { + const req = $.getJSON('/' + this.type) + .done(msg => dispatch(reset(msg.data))) + .fail(error => dispatch(handleFetchError(error))); + + dispatch({ type: BEGIN_FETCH, req }) + + return req + } +} + +export function handleWsMsg(msg) { + return (dispatch, getState) => { + if (msg.cmd === STORE_CMDS_RESET) { + const req = getState().settings.req + if (req) { + req.abort() + } + return dispatch(reset(msg.data)) + } + dispatch({ type: RECV_WS_MSG, cmd: msg.cmd, data: msg.data }) + } +} + +export function reset(data) { + return { type: FETCH_SETTINGS, data } +} + +export function handleFetchError(error) { + console.error(error) + return { type: FETCH_ERROR, error } +} |