@calvear/react-redux
Version:
Preconfigured Redux store initializer with Redux Saga, Redux Logger and Reselect for React SPA applications.
121 lines (104 loc) • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.dispatch = dispatch;
exports.selectPartition = selectPartition;
exports.takeAny = takeAny;
var _effects = require("redux-saga/effects");
var _utils = require("../utils");
var _marked = /*#__PURE__*/regeneratorRuntime.mark(dispatch),
_marked2 = /*#__PURE__*/regeneratorRuntime.mark(takeAny);
/**
* Dispatches an action.
*
* @dependency put from redux-saga.
*
* @param {string} type action type.
* @param {any} payload action payload.
*
* @yields {Array<any>}
*/
function dispatch(type, payload) {
return regeneratorRuntime.wrap(function dispatch$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return (0, _effects.put)({
type: type,
payload: payload
});
case 2:
case "end":
return _context.stop();
}
}
}, _marked);
}
/**
* Query a store partition by a partition definition.
*
* @dependency select from redux-saga.
*
* @param {any} partition partition definition with action types.
*
* @returns {any} partition.
*/
function selectPartition(partition) {
return (0, _effects.select)((0, _utils.createPartitionSelector)(partition));
}
/**
* Waits for any action type to occur n times.
*
* @dependency race, take and delay from redux-saga.
*
* @param {Array} types action types (from partitions definitions).
* @param {number} times times for wait each action type.
* @param {number} [timeout] timeout in milliseconds (0 no timeout).
*
* @yields {Array<any>}
*
* @returns {IterableIterator<any>} actions results.
*/
function takeAny(types) {
var times,
timeout,
results,
_args2 = arguments;
return regeneratorRuntime.wrap(function takeAny$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
times = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : 1;
timeout = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : 0;
// stores every result of action intercepted.
results = []; // waits for every dispatch (whether action belongs to types array).
case 3:
if (!(times-- > 0)) {
_context2.next = 11;
break;
}
_context2.t0 = results;
_context2.next = 7;
return (0, _effects.race)(types.reduce(function (awaiter, actionType) {
awaiter[actionType] = (0, _effects.take)(actionType);
return awaiter;
}, timeout === 0 ? {} : {
timeout: (0, _effects.delay)(timeout)
}));
case 7:
_context2.t1 = _context2.sent;
_context2.t0.push.call(_context2.t0, _context2.t1);
_context2.next = 3;
break;
case 11:
return _context2.abrupt("return", results);
case 12:
case "end":
return _context2.stop();
}
}
}, _marked2);
}
//# sourceMappingURL=saga.lib.js.map