UNPKG

2.44 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.runSaga = runSaga;
5
6var _utils = require('./utils');
7
8var _proc = require('./proc');
9
10var _proc2 = _interopRequireDefault(_proc);
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14var RUN_SAGA_SIGNATURE = 'runSaga(storeInterface, saga, ...args)';
15var NON_GENERATOR_ERR = RUN_SAGA_SIGNATURE + ': saga argument must be a Generator function!';
16
17function runSaga(storeInterface, saga) {
18 for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
19 args[_key - 2] = arguments[_key];
20 }
21
22 var iterator = void 0;
23
24 if (_utils.is.iterator(storeInterface)) {
25 if (process.env.NODE_ENV === 'development') {
26 (0, _utils.log)('warn', 'runSaga(iterator, storeInterface) has been deprecated in favor of ' + RUN_SAGA_SIGNATURE);
27 }
28 iterator = storeInterface;
29 storeInterface = saga;
30 } else {
31 (0, _utils.check)(saga, _utils.is.func, NON_GENERATOR_ERR);
32 iterator = saga.apply(undefined, args);
33 (0, _utils.check)(iterator, _utils.is.iterator, NON_GENERATOR_ERR);
34 }
35
36 var _storeInterface = storeInterface,
37 subscribe = _storeInterface.subscribe,
38 dispatch = _storeInterface.dispatch,
39 getState = _storeInterface.getState,
40 context = _storeInterface.context,
41 sagaMonitor = _storeInterface.sagaMonitor,
42 logger = _storeInterface.logger,
43 onError = _storeInterface.onError;
44
45
46 var effectId = (0, _utils.uid)();
47
48 if (sagaMonitor) {
49 // monitors are expected to have a certain interface, let's fill-in any missing ones
50 sagaMonitor.effectTriggered = sagaMonitor.effectTriggered || _utils.noop;
51 sagaMonitor.effectResolved = sagaMonitor.effectResolved || _utils.noop;
52 sagaMonitor.effectRejected = sagaMonitor.effectRejected || _utils.noop;
53 sagaMonitor.effectCancelled = sagaMonitor.effectCancelled || _utils.noop;
54 sagaMonitor.actionDispatched = sagaMonitor.actionDispatched || _utils.noop;
55
56 sagaMonitor.effectTriggered({ effectId: effectId, root: true, parentEffectId: 0, effect: { root: true, saga: saga, args: args } });
57 }
58
59 var task = (0, _proc2.default)(iterator, subscribe, (0, _utils.wrapSagaDispatch)(dispatch), getState, context, { sagaMonitor: sagaMonitor, logger: logger, onError: onError }, effectId, saga.name);
60
61 if (sagaMonitor) {
62 sagaMonitor.effectResolved(effectId, task);
63 }
64
65 return task;
66}
\No newline at end of file