UNPKG

3.4 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.default = sagaMiddlewareFactory;
5
6var _utils = require('./utils');
7
8var _channel = require('./channel');
9
10var _runSaga = require('./runSaga');
11
12function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
13
14function sagaMiddlewareFactory() {
15 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
16
17 var _ref$context = _ref.context,
18 context = _ref$context === undefined ? {} : _ref$context,
19 options = _objectWithoutProperties(_ref, ['context']);
20
21 var sagaMonitor = options.sagaMonitor,
22 logger = options.logger,
23 onError = options.onError;
24
25
26 if (_utils.is.func(options)) {
27 if (process.env.NODE_ENV === 'production') {
28 throw new Error('Saga middleware no longer accept Generator functions. Use sagaMiddleware.run instead');
29 } else {
30 throw new Error('You passed a function to the Saga middleware. You are likely trying to start a Saga by directly passing it to the middleware. This is no longer possible starting from 0.10.0. To run a Saga, you must do it dynamically AFTER mounting the middleware into the store.\n Example:\n import createSagaMiddleware from \'redux-saga\'\n ... other imports\n\n const sagaMiddleware = createSagaMiddleware()\n const store = createStore(reducer, applyMiddleware(sagaMiddleware))\n sagaMiddleware.run(saga, ...args)\n ');
31 }
32 }
33
34 if (logger && !_utils.is.func(logger)) {
35 throw new Error('`options.logger` passed to the Saga middleware is not a function!');
36 }
37
38 if (process.env.NODE_ENV === 'development' && options.onerror) {
39 throw new Error('`options.onerror` was removed. Use `options.onError` instead.');
40 }
41
42 if (onError && !_utils.is.func(onError)) {
43 throw new Error('`options.onError` passed to the Saga middleware is not a function!');
44 }
45
46 if (options.emitter && !_utils.is.func(options.emitter)) {
47 throw new Error('`options.emitter` passed to the Saga middleware is not a function!');
48 }
49
50 function sagaMiddleware(_ref2) {
51 var getState = _ref2.getState,
52 dispatch = _ref2.dispatch;
53
54 var sagaEmitter = (0, _channel.emitter)();
55 sagaEmitter.emit = (options.emitter || _utils.ident)(sagaEmitter.emit);
56
57 sagaMiddleware.run = _runSaga.runSaga.bind(null, {
58 context: context,
59 subscribe: sagaEmitter.subscribe,
60 dispatch: dispatch,
61 getState: getState,
62 sagaMonitor: sagaMonitor,
63 logger: logger,
64 onError: onError
65 });
66
67 return function (next) {
68 return function (action) {
69 if (sagaMonitor && sagaMonitor.actionDispatched) {
70 sagaMonitor.actionDispatched(action);
71 }
72 var result = next(action); // hit reducers
73 sagaEmitter.emit(action);
74 return result;
75 };
76 };
77 }
78
79 sagaMiddleware.run = function () {
80 throw new Error('Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware');
81 };
82
83 sagaMiddleware.setContext = function (props) {
84 (0, _utils.check)(props, _utils.is.object, (0, _utils.createSetContextWarning)('sagaMiddleware', props));
85 _utils.object.assign(context, props);
86 };
87
88 return sagaMiddleware;
89}
\No newline at end of file