redux-saga
Version:
Saga middleware for Redux to handle Side Effects
268 lines (226 loc) • 8.77 kB
JavaScript
exports.__esModule = true;
exports.asEffect = exports.takem = exports.detach = undefined;
exports.take = take;
exports.put = put;
exports.all = all;
exports.race = race;
exports.call = call;
exports.apply = apply;
exports.cps = cps;
exports.fork = fork;
exports.spawn = spawn;
exports.join = join;
exports.cancel = cancel;
exports.select = select;
exports.actionChannel = actionChannel;
exports.cancelled = cancelled;
exports.flush = flush;
exports.getContext = getContext;
exports.setContext = setContext;
var _utils = /*#__PURE__*/require('./utils');
var IO = /*#__PURE__*/(0, _utils.sym)('IO');
var TAKE = 'TAKE';
var PUT = 'PUT';
var ALL = 'ALL';
var RACE = 'RACE';
var CALL = 'CALL';
var CPS = 'CPS';
var FORK = 'FORK';
var JOIN = 'JOIN';
var CANCEL = 'CANCEL';
var SELECT = 'SELECT';
var ACTION_CHANNEL = 'ACTION_CHANNEL';
var CANCELLED = 'CANCELLED';
var FLUSH = 'FLUSH';
var GET_CONTEXT = 'GET_CONTEXT';
var SET_CONTEXT = 'SET_CONTEXT';
var TEST_HINT = '\n(HINT: if you are getting this errors in tests, consider using createMockTask from redux-saga/utils)';
var effect = function effect(type, payload) {
var _ref;
return _ref = {}, _ref[IO] = true, _ref[type] = payload, _ref;
};
var detach = exports.detach = function detach(eff) {
(0, _utils.check)(asEffect.fork(eff), _utils.is.object, 'detach(eff): argument must be a fork effect');
eff[FORK].detached = true;
return eff;
};
function take() {
var patternOrChannel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '*';
if (arguments.length) {
(0, _utils.check)(arguments[0], _utils.is.notUndef, 'take(patternOrChannel): patternOrChannel is undefined');
}
if (_utils.is.pattern(patternOrChannel)) {
return effect(TAKE, { pattern: patternOrChannel });
}
if (_utils.is.channel(patternOrChannel)) {
return effect(TAKE, { channel: patternOrChannel });
}
throw new Error('take(patternOrChannel): argument ' + String(patternOrChannel) + ' is not valid channel or a valid pattern');
}
take.maybe = function () {
var eff = take.apply(undefined, arguments);
eff[TAKE].maybe = true;
return eff;
};
var takem = /*#__PURE__*/exports.takem = (0, _utils.deprecate)(take.maybe, /*#__PURE__*/(0, _utils.updateIncentive)('takem', 'take.maybe'));
function put(channel, action) {
if (arguments.length > 1) {
(0, _utils.check)(channel, _utils.is.notUndef, 'put(channel, action): argument channel is undefined');
(0, _utils.check)(channel, _utils.is.channel, 'put(channel, action): argument ' + channel + ' is not a valid channel');
(0, _utils.check)(action, _utils.is.notUndef, 'put(channel, action): argument action is undefined');
} else {
(0, _utils.check)(channel, _utils.is.notUndef, 'put(action): argument action is undefined');
action = channel;
channel = null;
}
return effect(PUT, { channel: channel, action: action });
}
put.resolve = function () {
var eff = put.apply(undefined, arguments);
eff[PUT].resolve = true;
return eff;
};
put.sync = /*#__PURE__*/(0, _utils.deprecate)(put.resolve, /*#__PURE__*/(0, _utils.updateIncentive)('put.sync', 'put.resolve'));
function all(effects) {
return effect(ALL, effects);
}
function race(effects) {
return effect(RACE, effects);
}
function getFnCallDesc(meth, fn, args) {
(0, _utils.check)(fn, _utils.is.notUndef, meth + ': argument fn is undefined');
var context = null;
if (_utils.is.array(fn)) {
var _fn = fn;
context = _fn[0];
fn = _fn[1];
} else if (fn.fn) {
var _fn2 = fn;
context = _fn2.context;
fn = _fn2.fn;
}
if (context && _utils.is.string(fn) && _utils.is.func(context[fn])) {
fn = context[fn];
}
(0, _utils.check)(fn, _utils.is.func, meth + ': argument ' + fn + ' is not a function');
return { context: context, fn: fn, args: args };
}
function call(fn) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return effect(CALL, getFnCallDesc('call', fn, args));
}
function apply(context, fn) {
var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
return effect(CALL, getFnCallDesc('apply', { context: context, fn: fn }, args));
}
function cps(fn) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
return effect(CPS, getFnCallDesc('cps', fn, args));
}
function fork(fn) {
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
}
return effect(FORK, getFnCallDesc('fork', fn, args));
}
function spawn(fn) {
for (var _len4 = arguments.length, args = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
args[_key4 - 1] = arguments[_key4];
}
return detach(fork.apply(undefined, [fn].concat(args)));
}
function join() {
for (var _len5 = arguments.length, tasks = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
tasks[_key5] = arguments[_key5];
}
if (tasks.length > 1) {
return all(tasks.map(function (t) {
return join(t);
}));
}
var task = tasks[0];
(0, _utils.check)(task, _utils.is.notUndef, 'join(task): argument task is undefined');
(0, _utils.check)(task, _utils.is.task, 'join(task): argument ' + task + ' is not a valid Task object ' + TEST_HINT);
return effect(JOIN, task);
}
function cancel() {
for (var _len6 = arguments.length, tasks = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
tasks[_key6] = arguments[_key6];
}
if (tasks.length > 1) {
return all(tasks.map(function (t) {
return cancel(t);
}));
}
var task = tasks[0];
if (tasks.length === 1) {
(0, _utils.check)(task, _utils.is.notUndef, 'cancel(task): argument task is undefined');
(0, _utils.check)(task, _utils.is.task, 'cancel(task): argument ' + task + ' is not a valid Task object ' + TEST_HINT);
}
return effect(CANCEL, task || _utils.SELF_CANCELLATION);
}
function select(selector) {
for (var _len7 = arguments.length, args = Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) {
args[_key7 - 1] = arguments[_key7];
}
if (arguments.length === 0) {
selector = _utils.ident;
} else {
(0, _utils.check)(selector, _utils.is.notUndef, 'select(selector,[...]): argument selector is undefined');
(0, _utils.check)(selector, _utils.is.func, 'select(selector,[...]): argument ' + selector + ' is not a function');
}
return effect(SELECT, { selector: selector, args: args });
}
/**
channel(pattern, [buffer]) => creates an event channel for store actions
**/
function actionChannel(pattern, buffer) {
(0, _utils.check)(pattern, _utils.is.notUndef, 'actionChannel(pattern,...): argument pattern is undefined');
if (arguments.length > 1) {
(0, _utils.check)(buffer, _utils.is.notUndef, 'actionChannel(pattern, buffer): argument buffer is undefined');
(0, _utils.check)(buffer, _utils.is.buffer, 'actionChannel(pattern, buffer): argument ' + buffer + ' is not a valid buffer');
}
return effect(ACTION_CHANNEL, { pattern: pattern, buffer: buffer });
}
function cancelled() {
return effect(CANCELLED, {});
}
function flush(channel) {
(0, _utils.check)(channel, _utils.is.channel, 'flush(channel): argument ' + channel + ' is not valid channel');
return effect(FLUSH, channel);
}
function getContext(prop) {
(0, _utils.check)(prop, _utils.is.string, 'getContext(prop): argument ' + prop + ' is not a string');
return effect(GET_CONTEXT, prop);
}
function setContext(props) {
(0, _utils.check)(props, _utils.is.object, (0, _utils.createSetContextWarning)(null, props));
return effect(SET_CONTEXT, props);
}
var createAsEffectType = function createAsEffectType(type) {
return function (effect) {
return effect && effect[IO] && effect[type];
};
};
var asEffect = exports.asEffect = {
take: /*#__PURE__*/createAsEffectType(TAKE),
put: /*#__PURE__*/createAsEffectType(PUT),
all: /*#__PURE__*/createAsEffectType(ALL),
race: /*#__PURE__*/createAsEffectType(RACE),
call: /*#__PURE__*/createAsEffectType(CALL),
cps: /*#__PURE__*/createAsEffectType(CPS),
fork: /*#__PURE__*/createAsEffectType(FORK),
join: /*#__PURE__*/createAsEffectType(JOIN),
cancel: /*#__PURE__*/createAsEffectType(CANCEL),
select: /*#__PURE__*/createAsEffectType(SELECT),
actionChannel: /*#__PURE__*/createAsEffectType(ACTION_CHANNEL),
cancelled: /*#__PURE__*/createAsEffectType(CANCELLED),
flush: /*#__PURE__*/createAsEffectType(FLUSH),
getContext: /*#__PURE__*/createAsEffectType(GET_CONTEXT),
setContext: /*#__PURE__*/createAsEffectType(SET_CONTEXT)
};
;