redux-pack
Version:
Sensible promise handling and middleware for redux
81 lines (62 loc) • 3.29 kB
JavaScript
var _templateObject=_taggedTemplateLiteral(['\n The handler for action ',' had a ',' property defined, but this is not \n a valid key for a redux-pack handler. Valid keys are: ','\n '],['\n The handler for action ',' had a ',' property defined, but this is not \n a valid key for a redux-pack handler. Valid keys are: ','\n ']),_templateObject2=_taggedTemplateLiteral(['\n The ',' handler for action ',' is expected to return a new state object.\n '],['\n The ',' handler for action ',' is expected to return a new state object.\n ']),_templateObject3=_taggedTemplateLiteral(['\n The ',' handler for action ',' is expected to be a function, \n but found ',' instead.\n '],['\n The ',' handler for action ',' is expected to be a function, \n but found ',' instead.\n ']),_templateObject4=_taggedTemplateLiteral(['\n You used redux-pack\'s `handle(...)` function on the action ',', however, it\n doesn\'t appear to be an action that was dispatched by redux-pack. This is likely an error.\n '],['\n You used redux-pack\'s \\`handle(...)\\` function on the action ',', however, it\n doesn\'t appear to be an action that was dispatched by redux-pack. This is likely an error.\n ']);var _deline=require('deline');var _deline2=_interopRequireDefault(_deline);
var _invariant=require('invariant');var _invariant2=_interopRequireDefault(_invariant);
var _constants=require('./constants');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _taggedTemplateLiteral(strings,raw){return Object.freeze(Object.defineProperties(strings,{raw:{value:Object.freeze(raw)}}));}
var VALID_KEYS={
start:true,
success:true,
failure:true,
finish:true,
always:true};
function verifyHandlers(handlers,action){
Object.keys(handlers).forEach(function(key){
(0,_invariant2.default)(VALID_KEYS[key],(0,_deline2.default)(_templateObject,
action.type,key,
Object.keys(VALID_KEYS)));
});
}
function safeMap(state,fn,action,name){
switch(typeof fn){
case'function':{
var result=fn(state,action);
(0,_invariant2.default)(result!==undefined,(0,_deline2.default)(_templateObject2,
name,action.type));
return result;
}
case'undefined':
return state;
default:
(0,_invariant2.default)(false,(0,_deline2.default)(_templateObject3,
name,action.type,
typeof fn));
return state;}
}
function handle(startingState,action,handlers){
if(process.env.NODE_ENV==='development'){
verifyHandlers(handlers,action);
}var
meta=action.meta;
var lifecycle=meta?meta[_constants.KEY.LIFECYCLE]:null;
if(lifecycle==null){
(0,_invariant2.default)(false,(0,_deline2.default)(_templateObject4,
action.type));
return startingState;
}
var state=startingState;
switch(lifecycle){
case _constants.LIFECYCLE.START:
state=safeMap(state,handlers.start,action,'start');
break;
case _constants.LIFECYCLE.SUCCESS:
state=safeMap(state,handlers.success,action,'success');
state=safeMap(state,handlers.finish,action,'finish');
break;
case _constants.LIFECYCLE.FAILURE:
state=safeMap(state,handlers.failure,action,'failure');
state=safeMap(state,handlers.finish,action,'finish');
break;
default:
break;}
state=safeMap(state,handlers.always,action,'always');
return state;
}
module.exports=handle;