Skip to content

Commit

Permalink
removed deprecated StoreIO
Browse files Browse the repository at this point in the history
  • Loading branch information
yelouafi committed Mar 30, 2016
1 parent 19879d9 commit fffce31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 58 deletions.
36 changes: 1 addition & 35 deletions src/internal/runSaga.js
Original file line number Diff line number Diff line change
@@ -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,
{
Expand Down
55 changes: 32 additions & 23 deletions test/runSaga.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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(() =>
Expand Down

0 comments on commit fffce31

Please sign in to comment.