Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Pass saga args through to subspaced sagas #63

Merged
merged 2 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/redux-subspace-saga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"redux-subspace": "^2.1.0"
},
"peerDependencies": {
"redux-saga": "^0.15.0"
"redux-saga": "^0.16.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
Expand Down
11 changes: 5 additions & 6 deletions packages/redux-subspace-saga/src/sagas/provideStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

import { setContext } from 'redux-saga/effects'

const provideStore = (store, options) => (saga) => {
return function* () {
yield setContext({ store })
yield setContext({ sagaMiddlewareOptions: options })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no reason to make seperate calls to setContext here.

yield* saga()
}
const provideStore = (store, options) => saga => {
return function*(...args) {
yield setContext({ store, sagaMiddlewareOptions: options })
yield* saga(...args)
}
}

export default provideStore
65 changes: 32 additions & 33 deletions packages/redux-subspace-saga/src/sagas/subspaced.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,52 @@ import { subspace } from 'redux-subspace'
import provideStore from './provideStore'

const emitter = () => {
const subscribers = []
const subscribers = []

function subscribe(sub) {
subscribers.push(sub)
return () => {
subscribers.splice(subscribers.indexOf(sub), 1)
}
function subscribe(sub) {
subscribers.push(sub)
return () => {
subscribers.splice(subscribers.indexOf(sub), 1)
}
}

function emit(item) {
const arr = subscribers.slice()
for (var i = 0, len = arr.length; i < len; i++) {
arr[i](item)
}
function emit(item) {
const arr = subscribers.slice()
for (var i = 0, len = arr.length; i < len; i++) {
arr[i](item)
}
}

return {
subscribe,
emit,
}
return {
subscribe,
emit
}
}

const subspaced = (mapState, namespace) => {
const subspaceDecorator = subspace(mapState, namespace)

const subspaceDecorator = subspace(mapState, namespace)
return saga => {
return function*(...args) {
const parentStore = yield getContext('store')
const sagaMiddlewareOptions = yield getContext('sagaMiddlewareOptions')

return (saga) => {
return function* wrappedSaga(...args) {
const parentStore = yield getContext('store')
const sagaMiddlewareOptions = yield getContext('sagaMiddlewareOptions')
const sagaEmitter = emitter()

const sagaEmitter = emitter()

const store = {
...sagaMiddlewareOptions,
...subspaceDecorator(parentStore),
subscribe: sagaEmitter.subscribe,
}
const store = {
...sagaMiddlewareOptions,
...subspaceDecorator(parentStore),
subscribe: sagaEmitter.subscribe
}

runSaga(store, provideStore(store, sagaMiddlewareOptions)(saga), ...args)
runSaga(store, provideStore(store, sagaMiddlewareOptions)(saga), ...args)

yield takeEvery('*', function* (action) {
store.processAction(action, sagaEmitter.emit)
yield
})
}
yield takeEvery('*', function*(action) {
store.processAction(action, sagaEmitter.emit)
yield
})
}
}
}

export default subspaced
Loading