[web] promot reduce

This commit is contained in:
Jason 2016-06-16 17:21:57 +08:00
parent 7fb0836bfe
commit cedac98b70
2 changed files with 21 additions and 18 deletions

View File

@ -101,9 +101,9 @@
"use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(exports,"__esModule",{value:!0});var _redux=require("redux"),_eventLog=require("./eventLog"),_eventLog2=_interopRequireDefault(_eventLog),_websocket=require("./websocket"),_websocket2=_interopRequireDefault(_websocket),_flows=require("./flows"),_flows2=_interopRequireDefault(_flows),_settings=require("./settings"),_settings2=_interopRequireDefault(_settings),_ui=require("./ui"),_ui2=_interopRequireDefault(_ui),rootReducer=(0,_redux.combineReducers)({eventLog:_eventLog2["default"],websocket:_websocket2["default"],flows:_flows2["default"],settings:_settings2["default"],ui:_ui2["default"]});exports["default"]=rootReducer; "use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(exports,"__esModule",{value:!0});var _redux=require("redux"),_eventLog=require("./eventLog"),_eventLog2=_interopRequireDefault(_eventLog),_websocket=require("./websocket"),_websocket2=_interopRequireDefault(_websocket),_flows=require("./flows"),_flows2=_interopRequireDefault(_flows),_settings=require("./settings"),_settings2=_interopRequireDefault(_settings),_ui=require("./ui"),_ui2=_interopRequireDefault(_ui),rootReducer=(0,_redux.combineReducers)({eventLog:_eventLog2["default"],websocket:_websocket2["default"],flows:_flows2["default"],settings:_settings2["default"],ui:_ui2["default"]});exports["default"]=rootReducer;
},{"./eventLog":32,"./flows":33,"./settings":35,"./ui":36,"./websocket":39,"redux":"redux"}],35:[function(require,module,exports){ },{"./eventLog":32,"./flows":33,"./settings":35,"./ui":36,"./websocket":39,"redux":"redux"}],35:[function(require,module,exports){
"use strict";function reduceData(e,t){switch(t.cmd){case WS_MSG_CMD_RESET:return t.data||{};case WS_MSG_CMD_UPDATE:return _.merge({},e.settings,t.data);default:return e}}function reduce(){var e=arguments.length<=0||void 0===arguments[0]?defaultState:arguments[0],t=arguments[1];switch(t.type){case BEGIN_FETCH:return _extends({},e,{pendings:[],req:t.req});case FETCH_SETTINGS:var r=e.pendings||[];return _extends({},e,{pendings:null,settings:r.reduce(reduceData,data)});case RECV_WS_MSG:return e.pendings?_extends({},e,{pendings:e.pendings.concat(t)}):_extends({},e,{settings:reduceData(e.settings,t)});default:return e}}function fetch(){var e=this;return function(t){var r=$.getJSON("/"+e.type).done(function(e){return t(reset(e.data))}).fail(function(e){return t(handleFetchError(e))});return t({type:BEGIN_FETCH,req:r}),r}}function handleWsMsg(e){return function(t,r){if(e.cmd!==STORE_CMDS_RESET)return t({type:RECV_WS_MSG,cmd:e.cmd,data:e.data});var n=r().settings.req;n&&n.abort(),t(reset(e.data))}}function reset(e){return{type:FETCH_SETTINGS,data:e}}function handleFetchError(e){return console.error(e),{type:FETCH_ERROR,error:e}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.RECV_WS_MSG=exports.FETCH_ERROR=exports.FETCH_SETTINGS=exports.BEGIN_FETCH=exports.WS_MSG_CMD_UPDATE=exports.WS_MSG_CMD_RESET=exports.WS_MSG_TYPE=void 0;var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e};exports["default"]=reduce,exports.fetch=fetch,exports.handleWsMsg=handleWsMsg,exports.reset=reset,exports.handleFetchError=handleFetchError;var _actions=require("../actions"),WS_MSG_TYPE=exports.WS_MSG_TYPE="settings",WS_MSG_CMD_RESET=exports.WS_MSG_CMD_RESET="reset",WS_MSG_CMD_UPDATE=exports.WS_MSG_CMD_UPDATE="update",BEGIN_FETCH=exports.BEGIN_FETCH="SETTINGS_BEGIN_FETCH",FETCH_SETTINGS=exports.FETCH_SETTINGS="SETTINGS_FETCH_SETTINGS",FETCH_ERROR=exports.FETCH_ERROR="SETTINGS_FETCH_ERROR",RECV_WS_MSG=exports.RECV_WS_MSG="SETTINGS_RECV_WS_MSG",defaultState={settings:{},pendings:null,req:null}; "use strict";function reduce(){var e=arguments.length<=0||void 0===arguments[0]?defaultState:arguments[0],t=arguments[1];switch(t.type){case BEGIN_FETCH:return _extends({},e,{pendings:[],req:t.req});case FETCH_SETTINGS:var r=e.pendings||[];return _extends({},e,{pendings:null,settings:r.reduce(reduceData,data)});case RECV_WS_MSG:return e.pendings?_extends({},e,{pendings:e.pendings.concat(t)}):_extends({},e,{settings:reduceData(e.settings,t)});default:return e}}function reduceData(e,t){switch(t.cmd){case WS_MSG_CMD_RESET:return t.data||{};case WS_MSG_CMD_UPDATE:return _.merge({},e.settings,t.data);default:return e}}function fetch(){var e=this;return function(t){var r=$.getJSON("/"+e.type).done(function(e){return t(reset(e.data))}).fail(function(e){return t(handleFetchError(e))});return t({type:BEGIN_FETCH,req:r}),r}}function handleWsMsg(e){return function(t,r){if(e.cmd!==STORE_CMDS_RESET)return t({type:RECV_WS_MSG,cmd:e.cmd,data:e.data});var n=r().settings.req;n&&n.abort(),t(reset(e.data))}}function reset(e){return{type:FETCH_SETTINGS,data:e}}function handleFetchError(e){return function(t,r){t((0,_eventLog.addLogEntry)(e.stack||e.message||e)),t({type:FETCH_ERROR,error:e})}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.RECV_WS_MSG=exports.FETCH_ERROR=exports.FETCH_SETTINGS=exports.BEGIN_FETCH=exports.WS_MSG_CMD_UPDATE=exports.WS_MSG_CMD_RESET=exports.WS_MSG_TYPE=void 0;var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e};exports["default"]=reduce,exports.fetch=fetch,exports.handleWsMsg=handleWsMsg,exports.reset=reset,exports.handleFetchError=handleFetchError;var _actions=require("../actions"),_eventLog=require("./eventLog"),WS_MSG_TYPE=exports.WS_MSG_TYPE="settings",WS_MSG_CMD_RESET=exports.WS_MSG_CMD_RESET="reset",WS_MSG_CMD_UPDATE=exports.WS_MSG_CMD_UPDATE="update",BEGIN_FETCH=exports.BEGIN_FETCH="SETTINGS_BEGIN_FETCH",FETCH_SETTINGS=exports.FETCH_SETTINGS="SETTINGS_FETCH_SETTINGS",FETCH_ERROR=exports.FETCH_ERROR="SETTINGS_FETCH_ERROR",RECV_WS_MSG=exports.RECV_WS_MSG="SETTINGS_RECV_WS_MSG",defaultState={settings:{},pendings:null,req:null};
},{"../actions":1}],36:[function(require,module,exports){ },{"../actions":1,"./eventLog":32}],36:[function(require,module,exports){
"use strict";function reducer(){var e=arguments.length<=0||void 0===arguments[0]?defaultState:arguments[0],t=arguments[1];switch(t.type){case SET_ACTIVE_MENU:return _extends({},e,{activeMenu:t.activeMenu});case _flows.SELECT_FLOW:var r=t.flowId&&!t.currentSelection,n=!t.flowId&&t.currentSelection;return r?_extends({},e,{activeMenu:"Flow"}):n&&"Flow"===e.activeMenu?_extends({},e,{activeMenu:"Start"}):e;default:return e}}function setActiveMenu(e){return{type:SET_ACTIVE_MENU,activeMenu:e}}Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e};exports["default"]=reducer,exports.setActiveMenu=setActiveMenu;var _flows=require("./flows"),SET_ACTIVE_MENU="SET_ACTIVE_MENU",defaultState={activeMenu:"Start"}; "use strict";function reducer(){var e=arguments.length<=0||void 0===arguments[0]?defaultState:arguments[0],t=arguments[1];switch(t.type){case SET_ACTIVE_MENU:return _extends({},e,{activeMenu:t.activeMenu});case _flows.SELECT_FLOW:var r=t.flowId&&!t.currentSelection,n=!t.flowId&&t.currentSelection;return r?_extends({},e,{activeMenu:"Flow"}):n&&"Flow"===e.activeMenu?_extends({},e,{activeMenu:"Start"}):e;default:return e}}function setActiveMenu(e){return{type:SET_ACTIVE_MENU,activeMenu:e}}Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e};exports["default"]=reducer,exports.setActiveMenu=setActiveMenu;var _flows=require("./flows"),SET_ACTIVE_MENU="SET_ACTIVE_MENU",defaultState={activeMenu:"Start"};
},{"./flows":33}],37:[function(require,module,exports){ },{"./flows":33}],37:[function(require,module,exports){

View File

@ -1,4 +1,5 @@
import { StoreCmds } from '../actions' import { StoreCmds } from '../actions'
import { addLogEntry } from './eventLog'
export const WS_MSG_TYPE = 'settings' export const WS_MSG_TYPE = 'settings'
export const WS_MSG_CMD_RESET = 'reset' export const WS_MSG_CMD_RESET = 'reset'
@ -11,20 +12,6 @@ export const RECV_WS_MSG = 'SETTINGS_RECV_WS_MSG'
const defaultState = { settings: {}, pendings: null, req: null } 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) { export default function reduce(state = defaultState, action) {
switch (action.type) { switch (action.type) {
@ -46,6 +33,20 @@ export default function reduce(state = defaultState, action) {
} }
} }
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 function fetch() { export function fetch() {
return dispatch => { return dispatch => {
const req = $.getJSON('/' + this.type) const req = $.getJSON('/' + this.type)
@ -76,6 +77,8 @@ export function reset(data) {
} }
export function handleFetchError(error) { export function handleFetchError(error) {
console.error(error) return (dispatch, getState) => {
return { type: FETCH_ERROR, error } dispatch(addLogEntry(error.stack || error.message || error))
dispatch({ type: FETCH_ERROR, error })
}
} }