redux-saga-routines
Version:
Routines for redux-saga also useful with redux-form
70 lines (58 loc) • 2.33 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _marked = /*#__PURE__*/_regeneratorRuntime.mark(handleRoutinePromiseAction),
_marked2 = /*#__PURE__*/_regeneratorRuntime.mark(routinePromiseWatcherSaga);
import { takeEvery, take, race, put, call, all } from 'redux-saga/effects';
import { ROUTINE_PROMISE_ACTION } from './constants';
var getPayload = function getPayload(data) {
return data && data.payload || data;
};
export function handleRoutinePromiseAction(action) {
var payload, _action$meta, routine, noSuccessPayload, _action$meta$defer, resolve, reject, _yield$all, _yield$all$, success, failure;
return _regeneratorRuntime.wrap(function handleRoutinePromiseAction$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
payload = action.payload, _action$meta = action.meta, routine = _action$meta.routine, noSuccessPayload = _action$meta.noSuccessPayload, _action$meta$defer = _action$meta.defer, resolve = _action$meta$defer.resolve, reject = _action$meta$defer.reject;
_context.next = 3;
return all([race({
success: take(routine.SUCCESS),
failure: take(routine.FAILURE)
}), put(routine.trigger(payload))]);
case 3:
_yield$all = _context.sent;
_yield$all$ = _yield$all[0];
success = _yield$all$.success;
failure = _yield$all$.failure;
if (!success) {
_context.next = 12;
break;
}
_context.next = 10;
return noSuccessPayload ? call(resolve) : call(resolve, getPayload(success));
case 10:
_context.next = 14;
break;
case 12:
_context.next = 14;
return call(reject, getPayload(failure));
case 14:
case "end":
return _context.stop();
}
}
}, _marked);
}
export default function routinePromiseWatcherSaga() {
return _regeneratorRuntime.wrap(function routinePromiseWatcherSaga$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return takeEvery(ROUTINE_PROMISE_ACTION, handleRoutinePromiseAction);
case 2:
case "end":
return _context2.stop();
}
}
}, _marked2);
}