Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take multiple actions dispatched with blocking call

I have a saga which is listening to an action. And when this action is dispatched it performs a blocking call.

The problem is that a lot of actions (same actions) are dispatched in the same time and my saga can't take all the actions. But I need to process each action synchronously.

I know this is a known problem in redux-saga documentation : My Saga is missing dispatched actions

But the fact is that I can't use a fork since I really need the previous call to be over before processing a new one.

Here is my code :

export function* readProducts() {
    while (true) {
        const {
            payload: { tags },
        } = yield take(RFID__ADD_PRODUCT);

        // sequential add of each item
        for (const tag of tags) {
            yield call(addProductViaRfid, tag);
        }
    }
}

Does anyone have a solution ?

like image 774
Mélissa A Avatar asked Nov 21 '19 11:11

Mélissa A


People also ask

Can I dispatch more than one action?

Well, no matter what you pass to dispatch , it is still a single action. Even if your action is an array of objects, or a function which can then create more action objects!

Can you dispatch two actions in Redux?

The effective result is the same: you can now dispatch multiple actions from one dispatch call. Here we have two examples using React Redux's connect decorator and React's JSX, the first using the multiAction action creator, the second using just an array. Note: These examples use a Stateless Functional Component.

Which Redux saga function is used to create dispatch side effects?

put is another effect provided by redux-saga which can be used to dispatch actions in a saga. So this instructs the middleware to dispatch an action COFFEE_DONE to the store.


1 Answers

You can create a buffering action channel and take actions from it.

export function* readProducts() {
    const addProductRequests = yield actionChannel(RFID__ADD_PRODUCT);
    while (true) {
        const {
            payload: { tags },
        } = yield take(addProductRequests);

        // sequential add of each item
        for (const tag of tags) {
            yield call(addProductViaRfid, tag);
        }
    }
}
like image 158
Yury Tarabanko Avatar answered Oct 30 '22 10:10

Yury Tarabanko