redux-saga
Version:
Saga middleware for Redux to handle Side Effects
83 lines (66 loc) • 3.31 kB
JavaScript
function _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; }
import { is, check, object, createSetContextWarning } from './utils';
import { emitter } from './channel';
import { ident } from './utils';
import { runSaga } from './runSaga';
export default function sagaMiddlewareFactory() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _ref$context = _ref.context,
context = _ref$context === undefined ? {} : _ref$context,
options = _objectWithoutProperties(_ref, ['context']);
var sagaMonitor = options.sagaMonitor,
logger = options.logger,
onError = options.onError;
if (is.func(options)) {
if (process.env.NODE_ENV === 'production') {
throw new Error('Saga middleware no longer accept Generator functions. Use sagaMiddleware.run instead');
} else {
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 ');
}
}
if (logger && !is.func(logger)) {
throw new Error('`options.logger` passed to the Saga middleware is not a function!');
}
if (process.env.NODE_ENV === 'development' && options.onerror) {
throw new Error('`options.onerror` was removed. Use `options.onError` instead.');
}
if (onError && !is.func(onError)) {
throw new Error('`options.onError` passed to the Saga middleware is not a function!');
}
if (options.emitter && !is.func(options.emitter)) {
throw new Error('`options.emitter` passed to the Saga middleware is not a function!');
}
function sagaMiddleware(_ref2) {
var getState = _ref2.getState,
dispatch = _ref2.dispatch;
var sagaEmitter = emitter();
sagaEmitter.emit = (options.emitter || ident)(sagaEmitter.emit);
sagaMiddleware.run = runSaga.bind(null, {
context: context,
subscribe: sagaEmitter.subscribe,
dispatch: dispatch,
getState: getState,
sagaMonitor: sagaMonitor,
logger: logger,
onError: onError
});
return function (next) {
return function (action) {
if (sagaMonitor && sagaMonitor.actionDispatched) {
sagaMonitor.actionDispatched(action);
}
var result = next(action); // hit reducers
sagaEmitter.emit(action);
return result;
};
};
}
sagaMiddleware.run = function () {
throw new Error('Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware');
};
sagaMiddleware.setContext = function (props) {
check(props, is.object, createSetContextWarning('sagaMiddleware', props));
object.assign(context, props);
};
return sagaMiddleware;
}