redux-saga: race between action and event channel

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



redux-saga: race between action and event channel



I want to race between a redux action and an event channel using redux-saga.


export function * createEventChannel ()
return eventChannel(emit =>
MyModule.addEventListener(MyModule.MY_EVENT, emit)
return () => MyModule.removeEventListner(MyModule.MY_EVENT, emit)
)


....

function * raceWithActionAndEvent()
const channel = yield call(createEventChannel)

// I want to race between Redux Action: 'MY_ACTION' and channel here




1 Answer
1



This should do it:


export function* raceWithActionAndEvent()
const channel = yield call(createEventChannel);
const winner = yield race(
channel: take(channel),
action: take(MY_ACTION),
);

if (winner.action)
// then the action was dispatched first

if (winner.channel)
// then the channel emitted first




In my opinion the code is quite readable. You set up a race between two takes and act on whichever wins.


race


take



Please note,createEventChannel doesn't need to be a generator function (like you have in the original question)


createEventChannel






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard