redux-saga - how to deal with API method that invokes callback many times

Viewed 731

I have external API that has a method like this watchForEvents(callback), where callback should be a function function(result, error) {...}. Callback invoked many times - one time for the each event produced by the API. Method watchForEvents is non-blocking.

I have a React.JS app that uses redux-saga. I can not figure out how to deal with such API.

If I invoke API method and make delay in my saga - everything works. I.e. yield call(delay, 10000);. But without delay saga swallows all invocations of the callback.

What can I do with this?

2 Answers

I had some experience on this one, and what I did was to create an event emitter that would emit an event for every invoke on my callback function. And then on redux-saga, it's just a matter of creating an event channel around that event emitter.

I have created a sample gist of my approach, with my use case before. Hope it could help: https://gist.github.com/aamiel16/75c1cd3fd1d9ef873a88dcf4dd86e00d

If there are any questions around it, I would be happy to follow up an answer on them.

Related