UNPKG

ruddy

Version:

Modularized state-management tools for modern front-end applications. Manage dispatched messages in a clean and predictable way for either small or large scale projects

163 lines (156 loc) 8.61 kB
import _default22 from 'ramda/src/ifElse'; import _default21 from 'ramda/src/tryCatch'; import _default20 from 'ramda/src/adjust'; import _default19 from 'ramda/src/pipe'; import _default18 from 'ramda/src/identity'; import _default17 from 'ramda/src/F'; import _default16 from 'ramda/src/always'; import _default15 from 'ramda/src/T'; import _default14 from 'ramda/src/test'; import _default13 from 'ramda/src/pathEq'; import _default12 from 'ramda/src/cond'; import _default11 from 'ramda/src/pick'; import _default10 from 'ramda/src/evolve'; import _default9 from 'ramda/src/is'; import _default8 from 'ramda/src/pathSatisfies'; import _default7 from 'ramda/src/when'; import _default6 from 'ramda/src/objOf'; import _default5 from 'ramda/src/unless'; import _default4 from 'ramda/src/__'; import _default3 from 'ramda/src/merge'; import _default2 from 'ramda/src/compose'; import _default from 'ramda/src/curry'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; import { makeShaper } from 'shapey'; import { isPlainObj, isPromise } from '../util/is'; /** * A slightly opinionated way to handle successful effects and creating a new * Action to be dispatched. This default success handler assumes you're alright * with appending _SUCCESS to the original action type, and merging the return object * (from your effect function) into the new Action. However, if your effect does NOT * return an object, that return value will be merged onto the new Action onto a prop * called "payload". The other assumption it makes is that if you have suffixed your * original Action with _REQUEST or _EFFECT, that will be removed. A (somewhat) standard * pattern in the Redux community is to suffix _SUCCESS for effects that succeed * and either _ERROR or _FAIL for those that failed. You can always override * with your own success handler by providing it when you create an effect in * the ducks. * * @func * @sig String|{k: v} -> {k: v} -> {k: v} * @param {String|Object} result The result returned from your effect function * @param {Object} action The original Redux action that triggered the effect * @returns {Object} A new Action object that contains the "payload" that was * caught when the effect was created (unless your effect function returned an * object, in which case it will be merged onto this) */ export var defaultSuccessHandler = _default(function (result, action) { return _default2(_default3(_default4, _extends({}, _default5(isPlainObj, _default6('payload'))(result))), _default7(_default8(_default9(String), ['type']), _default2(_default10({ type: function type(t) { return t.replace(/_REQUEST$/i, '').replace(/_EFFECT$/i, '') + '_SUCCESS'; } }), _default11(['type']))))(action); }); /** * A slightly opinionated way to handle catching an exception and creating a new * Action to be dispatched. This default error handler assumes you're alright * with appending _ERROR to the original action type, and pruning out all of the * other fields from the original action and setting only an "error" prop on the * new Action. The other assumption it makes is that if you have suffixed your * original Action with _REQUEST or _EFFECT, that will be removed. A (somewhat) standard * pattern in the Redux community is to suffix _SUCCESS for effects that succeed * and either _ERROR or _FAIL for those that failed. You can always override * with your own error handler by providing it when you create an effect in * the ducks. * * @func * @sig String|{k: v} -> {k: v} -> {k: v} * @param {String|Object} error An error that was caught when the effect was * triggered * @param {Object} action The original Redux action that triggered the effect * @returns {Object} A new Action object that contains the "error" that was * caught when the effect was created */ export var defaultErrorHandler = _default(function (error, action) { return _default2(_default3({ error: _default5(isPlainObj, String)(error) }), _default7(_default8(_default9(String), ['type']), _default2(_default10({ type: function type(t) { return t.replace(/_REQUEST$/i, '').replace(/_EFFECT$/i, '') + '_ERROR'; } }), _default11(['type']))))(action); }); /** * Makes a predicate function out of either a String, RegExp, or a Function, * which can then be applied to an Object that contains a "type" property. * This is meant to be used to evaluate if a Redux action matches the predicate. * If neither a String, RegExp, nor Function is supplied here, then the result * will be to give back a function that always returns false. * * @func * @sig String|RegExp|({k: v} -> Boolean) -> ({k: v} -> Boolean) * @param {String|RegExp|Function} pattern A String to be exactly matched to an * Object's "type" property, OR a Regular expression to be matched against it, * OR a function to be used to match against any custom criteria on that Object * @returns {Function} A predicate function to be applied later to an Object * (ideally, one containing a "type" property) */ export var makePredicate = _default12([[_default9(String), _default13(['type'])], [_default9(RegExp), _default2(_default8(_default4, ['type']), _default14)], [_default9(Function), function (fn) { return _default2(Boolean, fn); }], [_default15, _default16(_default17)]]); /** * Creates a function that handles the effect result (either success or failure) * * @func * @sig (a -> b) -> (a -> b) -> (a -> b) * @param {Function} defaultHandler The fall through handler function to be used * in case the handler passed into this function is null/undefined * @param {String|Object|Function} handler The handler function or the * String/Object to be turned into a handler function (if String/Object, it will * be turned into a Shapey spec-mapping function) * @returns {Function} A success OR error handler function to be applied after * the effect is finished */ export var makeResponseHandler = _default(function (defaultHandler, handler) { return _default12([[_default9(String), _default2(makeShaper, _default6('type'))], [isPlainObj, makeShaper], [_default9(Function), _default18], [_default15, _default16(defaultHandler)]])(handler); }); /** * Creates a robust effect handler from a predicate, an effect creating * function, as well as a success and error handler. This curried function can * then be applied safely to any Action that matches the predicate (if the * Action does NOT match, this is simply an identity function - which returns * the Action as-is). * * @func * @param {String|RegExp|Function} pattern A String to be exactly matched to an * Object's "type" property, OR a Regular expression to be matched against it, * OR a function to be used to match against any custom criteria on that Object * @param {Function} effectHandler A custom function that creates some kind of * normal (but "impure") effect which may succeed or it may fail * @param {Function} successHandler A function that receives the input of the * effect creating function and creates a new Action containing its result * @param {Function} errorHandler A function that receives the caught exception * from the effect creating function and creates a new Action from it * @returns {Object} Either the original action (if it didn't match the * predicate) or a new Action that represents the succes of the effect or * alternatively it's failure */ export var makeEffectHandler = _default(function (effect, action) { return _default19(_default20(makePredicate, 0), function (_ref) { var pattern = _ref[0], effectHandler = _ref[1], successHandler = _ref[2], errorHandler = _ref[3]; return [makePredicate(pattern), effectHandler, makeResponseHandler(defaultSuccessHandler, successHandler), makeResponseHandler(defaultErrorHandler, errorHandler)]; }, function (_ref2) { var predicate = _ref2[0], effectHandler = _ref2[1], successHandler = _ref2[2], errorHandler = _ref2[3]; return _default7(predicate, _default21(_default19(effectHandler, _default22(isPromise, function (promise) { return promise.then(function (res) { return successHandler(res, action); }).catch(function (err) { return errorHandler(err, action); }); }, function (res) { return successHandler(res, action); })), function (ex) { return errorHandler(ex, action); }))(action); })(effect); });