1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.asEffect = exports.takem = undefined;
|
5 | exports.take = take;
|
6 | exports.put = put;
|
7 | exports.all = all;
|
8 | exports.race = race;
|
9 | exports.call = call;
|
10 | exports.apply = apply;
|
11 | exports.cps = cps;
|
12 | exports.fork = fork;
|
13 | exports.spawn = spawn;
|
14 | exports.join = join;
|
15 | exports.cancel = cancel;
|
16 | exports.select = select;
|
17 | exports.actionChannel = actionChannel;
|
18 | exports.cancelled = cancelled;
|
19 | exports.flush = flush;
|
20 | exports.getContext = getContext;
|
21 | exports.setContext = setContext;
|
22 | exports.takeEvery = takeEvery;
|
23 | exports.takeLatest = takeLatest;
|
24 | exports.throttle = throttle;
|
25 |
|
26 | var _utils = require('./utils');
|
27 |
|
28 | var _sagaHelpers = require('./sagaHelpers');
|
29 |
|
30 | var IO = (0, _utils.sym)('IO');
|
31 | var TAKE = 'TAKE';
|
32 | var PUT = 'PUT';
|
33 | var ALL = 'ALL';
|
34 | var RACE = 'RACE';
|
35 | var CALL = 'CALL';
|
36 | var CPS = 'CPS';
|
37 | var FORK = 'FORK';
|
38 | var JOIN = 'JOIN';
|
39 | var CANCEL = 'CANCEL';
|
40 | var SELECT = 'SELECT';
|
41 | var ACTION_CHANNEL = 'ACTION_CHANNEL';
|
42 | var CANCELLED = 'CANCELLED';
|
43 | var FLUSH = 'FLUSH';
|
44 | var GET_CONTEXT = 'GET_CONTEXT';
|
45 | var SET_CONTEXT = 'SET_CONTEXT';
|
46 |
|
47 | var TEST_HINT = '\n(HINT: if you are getting this errors in tests, consider using createMockTask from redux-saga/utils)';
|
48 |
|
49 | var effect = function effect(type, payload) {
|
50 | var _ref;
|
51 |
|
52 | return _ref = {}, _ref[IO] = true, _ref[type] = payload, _ref;
|
53 | };
|
54 |
|
55 | function take() {
|
56 | var patternOrChannel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '*';
|
57 |
|
58 | if (arguments.length) {
|
59 | (0, _utils.check)(arguments[0], _utils.is.notUndef, 'take(patternOrChannel): patternOrChannel is undefined');
|
60 | }
|
61 | if (_utils.is.pattern(patternOrChannel)) {
|
62 | return effect(TAKE, { pattern: patternOrChannel });
|
63 | }
|
64 | if (_utils.is.channel(patternOrChannel)) {
|
65 | return effect(TAKE, { channel: patternOrChannel });
|
66 | }
|
67 | throw new Error('take(patternOrChannel): argument ' + String(patternOrChannel) + ' is not valid channel or a valid pattern');
|
68 | }
|
69 |
|
70 | take.maybe = function () {
|
71 | var eff = take.apply(undefined, arguments);
|
72 | eff[TAKE].maybe = true;
|
73 | return eff;
|
74 | };
|
75 |
|
76 | var takem = exports.takem = (0, _utils.deprecate)(take.maybe, (0, _utils.updateIncentive)('takem', 'take.maybe'));
|
77 |
|
78 | function put(channel, action) {
|
79 | if (arguments.length > 1) {
|
80 | (0, _utils.check)(channel, _utils.is.notUndef, 'put(channel, action): argument channel is undefined');
|
81 | (0, _utils.check)(channel, _utils.is.channel, 'put(channel, action): argument ' + channel + ' is not a valid channel');
|
82 | (0, _utils.check)(action, _utils.is.notUndef, 'put(channel, action): argument action is undefined');
|
83 | } else {
|
84 | (0, _utils.check)(channel, _utils.is.notUndef, 'put(action): argument action is undefined');
|
85 | action = channel;
|
86 | channel = null;
|
87 | }
|
88 | return effect(PUT, { channel: channel, action: action });
|
89 | }
|
90 |
|
91 | put.resolve = function () {
|
92 | var eff = put.apply(undefined, arguments);
|
93 | eff[PUT].resolve = true;
|
94 | return eff;
|
95 | };
|
96 |
|
97 | put.sync = (0, _utils.deprecate)(put.resolve, (0, _utils.updateIncentive)('put.sync', 'put.resolve'));
|
98 |
|
99 | function all(effects) {
|
100 | return effect(ALL, effects);
|
101 | }
|
102 |
|
103 | function race(effects) {
|
104 | return effect(RACE, effects);
|
105 | }
|
106 |
|
107 | function getFnCallDesc(meth, fn, args) {
|
108 | (0, _utils.check)(fn, _utils.is.notUndef, meth + ': argument fn is undefined');
|
109 |
|
110 | var context = null;
|
111 | if (_utils.is.array(fn)) {
|
112 | var _fn = fn;
|
113 | context = _fn[0];
|
114 | fn = _fn[1];
|
115 | } else if (fn.fn) {
|
116 | var _fn2 = fn;
|
117 | context = _fn2.context;
|
118 | fn = _fn2.fn;
|
119 | }
|
120 | if (context && _utils.is.string(fn) && _utils.is.func(context[fn])) {
|
121 | fn = context[fn];
|
122 | }
|
123 | (0, _utils.check)(fn, _utils.is.func, meth + ': argument ' + fn + ' is not a function');
|
124 |
|
125 | return { context: context, fn: fn, args: args };
|
126 | }
|
127 |
|
128 | function call(fn) {
|
129 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
130 | args[_key - 1] = arguments[_key];
|
131 | }
|
132 |
|
133 | return effect(CALL, getFnCallDesc('call', fn, args));
|
134 | }
|
135 |
|
136 | function apply(context, fn) {
|
137 | var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
138 |
|
139 | return effect(CALL, getFnCallDesc('apply', { context: context, fn: fn }, args));
|
140 | }
|
141 |
|
142 | function cps(fn) {
|
143 | for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
144 | args[_key2 - 1] = arguments[_key2];
|
145 | }
|
146 |
|
147 | return effect(CPS, getFnCallDesc('cps', fn, args));
|
148 | }
|
149 |
|
150 | function fork(fn) {
|
151 | for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
152 | args[_key3 - 1] = arguments[_key3];
|
153 | }
|
154 |
|
155 | return effect(FORK, getFnCallDesc('fork', fn, args));
|
156 | }
|
157 |
|
158 | function spawn(fn) {
|
159 | for (var _len4 = arguments.length, args = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
|
160 | args[_key4 - 1] = arguments[_key4];
|
161 | }
|
162 |
|
163 | var eff = fork.apply(undefined, [fn].concat(args));
|
164 | eff[FORK].detached = true;
|
165 | return eff;
|
166 | }
|
167 |
|
168 | function join() {
|
169 | for (var _len5 = arguments.length, tasks = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
|
170 | tasks[_key5] = arguments[_key5];
|
171 | }
|
172 |
|
173 | if (tasks.length > 1) {
|
174 | return all(tasks.map(function (t) {
|
175 | return join(t);
|
176 | }));
|
177 | }
|
178 | var task = tasks[0];
|
179 | (0, _utils.check)(task, _utils.is.notUndef, 'join(task): argument task is undefined');
|
180 | (0, _utils.check)(task, _utils.is.task, 'join(task): argument ' + task + ' is not a valid Task object ' + TEST_HINT);
|
181 | return effect(JOIN, task);
|
182 | }
|
183 |
|
184 | function cancel() {
|
185 | for (var _len6 = arguments.length, tasks = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
|
186 | tasks[_key6] = arguments[_key6];
|
187 | }
|
188 |
|
189 | if (tasks.length > 1) {
|
190 | return all(tasks.map(function (t) {
|
191 | return cancel(t);
|
192 | }));
|
193 | }
|
194 | var task = tasks[0];
|
195 | if (tasks.length === 1) {
|
196 | (0, _utils.check)(task, _utils.is.notUndef, 'cancel(task): argument task is undefined');
|
197 | (0, _utils.check)(task, _utils.is.task, 'cancel(task): argument ' + task + ' is not a valid Task object ' + TEST_HINT);
|
198 | }
|
199 | return effect(CANCEL, task || _utils.SELF_CANCELLATION);
|
200 | }
|
201 |
|
202 | function select(selector) {
|
203 | for (var _len7 = arguments.length, args = Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) {
|
204 | args[_key7 - 1] = arguments[_key7];
|
205 | }
|
206 |
|
207 | if (arguments.length === 0) {
|
208 | selector = _utils.ident;
|
209 | } else {
|
210 | (0, _utils.check)(selector, _utils.is.notUndef, 'select(selector,[...]): argument selector is undefined');
|
211 | (0, _utils.check)(selector, _utils.is.func, 'select(selector,[...]): argument ' + selector + ' is not a function');
|
212 | }
|
213 | return effect(SELECT, { selector: selector, args: args });
|
214 | }
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function actionChannel(pattern, buffer) {
|
220 | (0, _utils.check)(pattern, _utils.is.notUndef, 'actionChannel(pattern,...): argument pattern is undefined');
|
221 | if (arguments.length > 1) {
|
222 | (0, _utils.check)(buffer, _utils.is.notUndef, 'actionChannel(pattern, buffer): argument buffer is undefined');
|
223 | (0, _utils.check)(buffer, _utils.is.buffer, 'actionChannel(pattern, buffer): argument ' + buffer + ' is not a valid buffer');
|
224 | }
|
225 | return effect(ACTION_CHANNEL, { pattern: pattern, buffer: buffer });
|
226 | }
|
227 |
|
228 | function cancelled() {
|
229 | return effect(CANCELLED, {});
|
230 | }
|
231 |
|
232 | function flush(channel) {
|
233 | (0, _utils.check)(channel, _utils.is.channel, 'flush(channel): argument ' + channel + ' is not valid channel');
|
234 | return effect(FLUSH, channel);
|
235 | }
|
236 |
|
237 | function getContext(prop) {
|
238 | (0, _utils.check)(prop, _utils.is.string, 'getContext(prop): argument ' + prop + ' is not a string');
|
239 | return effect(GET_CONTEXT, prop);
|
240 | }
|
241 |
|
242 | function setContext(props) {
|
243 | (0, _utils.check)(props, _utils.is.object, (0, _utils.createSetContextWarning)(null, props));
|
244 | return effect(SET_CONTEXT, props);
|
245 | }
|
246 |
|
247 | function takeEvery(patternOrChannel, worker) {
|
248 | for (var _len8 = arguments.length, args = Array(_len8 > 2 ? _len8 - 2 : 0), _key8 = 2; _key8 < _len8; _key8++) {
|
249 | args[_key8 - 2] = arguments[_key8];
|
250 | }
|
251 |
|
252 | return fork.apply(undefined, [_sagaHelpers.takeEveryHelper, patternOrChannel, worker].concat(args));
|
253 | }
|
254 |
|
255 | function takeLatest(patternOrChannel, worker) {
|
256 | for (var _len9 = arguments.length, args = Array(_len9 > 2 ? _len9 - 2 : 0), _key9 = 2; _key9 < _len9; _key9++) {
|
257 | args[_key9 - 2] = arguments[_key9];
|
258 | }
|
259 |
|
260 | return fork.apply(undefined, [_sagaHelpers.takeLatestHelper, patternOrChannel, worker].concat(args));
|
261 | }
|
262 |
|
263 | function throttle(ms, pattern, worker) {
|
264 | for (var _len10 = arguments.length, args = Array(_len10 > 3 ? _len10 - 3 : 0), _key10 = 3; _key10 < _len10; _key10++) {
|
265 | args[_key10 - 3] = arguments[_key10];
|
266 | }
|
267 |
|
268 | return fork.apply(undefined, [_sagaHelpers.throttleHelper, ms, pattern, worker].concat(args));
|
269 | }
|
270 |
|
271 | var createAsEffectType = function createAsEffectType(type) {
|
272 | return function (effect) {
|
273 | return effect && effect[IO] && effect[type];
|
274 | };
|
275 | };
|
276 |
|
277 | var asEffect = exports.asEffect = {
|
278 | take: createAsEffectType(TAKE),
|
279 | put: createAsEffectType(PUT),
|
280 | all: createAsEffectType(ALL),
|
281 | race: createAsEffectType(RACE),
|
282 | call: createAsEffectType(CALL),
|
283 | cps: createAsEffectType(CPS),
|
284 | fork: createAsEffectType(FORK),
|
285 | join: createAsEffectType(JOIN),
|
286 | cancel: createAsEffectType(CANCEL),
|
287 | select: createAsEffectType(SELECT),
|
288 | actionChannel: createAsEffectType(ACTION_CHANNEL),
|
289 | cancelled: createAsEffectType(CANCELLED),
|
290 | flush: createAsEffectType(FLUSH),
|
291 | getContext: createAsEffectType(GET_CONTEXT),
|
292 | setContext: createAsEffectType(SET_CONTEXT)
|
293 | }; |
\ | No newline at end of file |