I'm trying to use redux-saga to connect events from PouchDB to my React.js application, but I'm struggling to figure out how to connect events emitted from PouchDB to my Saga. Since the event uses a callback function (and I can't pass it a generator), I can't use yield put()
inside the callback, it gives weird errors after ES2015 compilation (using Webpack).
So here's what I'm trying to accomplish, the part that doesn't work is inside replication.on('change' (info) => {})
.
function * startReplication (wrapper) {
while (yield take(DATABASE_SET_CONFIGURATION)) {
yield call(wrapper.connect.bind(wrapper))
// Returns a promise, or false.
let replication = wrapper.replicate()
if (replication) {
replication.on('change', (info) => {
yield put(replicationChange(info))
})
}
}
}
export default [ startReplication ]
I had the same problem also using PouchDB and found the answers provided extremely useful and interesting. However there are many ways to do the same thing in PouchDB and I dug around a little and found a different approach which maybe easier to reason about.
If you don't attach listeners to the
db.change
request then it returns any change data directly to the caller and addingcontinuous: true
to the option will cause to issue a longpoll and not return until some change has happened. So the same result can be achieved with the followingThere is one thing that I haven't got to the bottom. If I replace the
for
loop withchange.results.forEach((change)=>{...})
then I get an invalid syntax error on theyield
. I'm assuming it's something to do with some clash in the use of iterators.Thanks to @Yassine Elouafi
I created short MIT licensed general channels implementation as redux-saga extension for TypeScript language based on solution by @Yassine Elouafi.
And example usage similar to redux-saga *takeEvery construction
We can use
eventChannel
of redux-sagaHere is my example
Please Note:
messages on an eventChannel are not buffered by default. If you want to process
message event
only one by one, you cannot use blocking call afterconst message = yield take(chan);
Or You have to provide a buffer to the eventChannel factory in order to specify buffering strategy for the channel (e.g. eventChannel(subscriber, buffer)). See redux-saga API docs for more info
The fundamental problem we have to solve is that event emitters are 'push-based', whereas sagas are 'pull-based'.
If you subscribe to an event like so:
replication.on('change', (info) => {})
,then the callback is executed whenever thereplication
event emitter decides to push a new value.With sagas, we need to flip the control around. It is the saga that must be in control of when it decides to respond to new change info being available. Put another way, a saga needs to pull the new info.
Below is an example of one way to achieve this:
There is a JSbin of the above example here: http://jsbin.com/cujudes/edit?js,console
You should also take a look at Yassine Elouafi's answer to a similar question: Can I use redux-saga's es6 generators as onmessage listener for websockets or eventsource?
As Nirrek explained it, when you need to connect to push data sources, you'll have to build an event iterator for that source.
I'd like to add that the above mechanism could be made reusable. So we don't have to recreate an event iterator for each different source.
The solution is to create a generic channel with
put
andtake
methods. You can call thetake
method from inside the Generator and connect theput
method to the listener interface of your data source.Here is a possible implementation. Note that the channel buffers messages if no one is waiting for them (e.g. the Generator is busy doing some remote call)
Then the above channel can be used anytime you want to listen to an external push data source. For your example