diff --git a/src/internal/runSaga.js b/src/internal/runSaga.js index 78bc34fac..80528b7a9 100644 --- a/src/internal/runSaga.js +++ b/src/internal/runSaga.js @@ -1,42 +1,8 @@ -import { sym, is, check, noop, warnDeprecated } from './utils' +import { is, check, noop } from './utils' import proc from './proc' -import emitter from './emitter' export const NOT_ITERATOR_ERROR = "runSaga must be called on an iterator" -/** - @deprecated - ATTENTION! this method can have some potential issues - For more infos, see issue https://github.com/yelouafi/redux-saga/issues/48 - - memoize the result of storeChannel. It avoids monkey patching the same store - multiple times unnecessarly. We need only one channel per store -**/ -const IO = sym('IO') -export function storeIO(store) { - - warnDeprecated(`storeIO is deprecated, to run Saga dynamically, use 'run' method of the middleware`) - - if(store[IO]) - return store[IO] - - const storeEmitter = emitter() - const _dispatch = store.dispatch - store.dispatch = action => { - const result = _dispatch(action) - storeEmitter.emit(action) - return result - } - - store[IO] = { - subscribe: storeEmitter.subscribe, - dispatch : store.dispatch, - getState : store.getState - } - - return store[IO] -} - export function runSaga( iterator, { diff --git a/test/runSaga.js b/test/runSaga.js index c85ee5f2d..4f666954f 100644 --- a/test/runSaga.js +++ b/test/runSaga.js @@ -1,22 +1,25 @@ import test from 'tape' -import { createStore } from 'redux' -import { runSaga, storeIO } from '../src' -import { take, select } from '../src/effects' -import { noop } from '../src/utils' +import { runSaga } from '../src' +import { fork, take, put, select } from '../src/effects' +import emitter from '../src/internal/emitter' -test('storeIO: memoize results', assert => { - assert.plan(1) - - const store = createStore(noop) +function storeLike(reducer, state) { + const em = emitter() - assert.equal(storeIO(store), storeIO(store), - 'storeChannel must memoize results by store' - ) + return { + subscribe: em.subscribe, + dispatch: (action) => { + state = reducer(state, action) + em.emit(action) + return action + }, + getState: () => state + } -}) +} test('runSaga', assert => { assert.plan(1) @@ -25,30 +28,36 @@ test('runSaga', assert => { function reducer(state = {}, action) { return action } + const store = storeLike(reducer, {}) const typeSelector = a => a.type + const task = runSaga(root(), store) - const store = createStore(reducer) + store.dispatch({type: 'ACTION-1'}) + store.dispatch({type: 'ACTION-2'}) - Promise.resolve(1) - .then(() => store.dispatch({type: 'ACTION-0'})) - .then(() => store.dispatch({type: 'ACTION-1'})) - .then(() => store.dispatch({type: 'ACTION-2'})) + function* root() { + yield [fork(fnA), fork(fnB)] + } - function* gen() { - actual.push( yield take('ACTION-0') ) - actual.push( yield select(typeSelector) ) + function* fnA() { actual.push( yield take('ACTION-1') ) actual.push( yield select(typeSelector) ) actual.push( yield take('ACTION-2') ) actual.push( yield select(typeSelector) ) + yield put({type: 'ACTION-3'}) } - const task = runSaga(gen(), storeIO(store)) + function* fnB() { + actual.push( yield take('ACTION-3') ) + actual.push( yield select(typeSelector) ) + } + + const expected = [ - {type: 'ACTION-0'}, 'ACTION-0', {type: 'ACTION-1'}, 'ACTION-1', - {type: 'ACTION-2'}, 'ACTION-2' + {type: 'ACTION-2'}, 'ACTION-2', + {type: 'ACTION-3'}, 'ACTION-3' ] task.done.then(() =>