How to take multiple actions dispatched with blocking call
Asked Answered
E

1

6

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 ?

Estovers answered 21/11, 2019 at 11:3 Comment(2)
I think you should read up on this redux-saga.js.org/docs/advanced/Channels.html , especially the last section where it uses channel and buffers to hold incoming requests. Something like this, const actionChannel = yield call(channel, buffers.expanding()); I've dealt a quite similar situation like yours but mine was with uploading a number of blocks to server. I thought this would be the same approach.Spinning
Yes that is exaclty what I needed ! I don't think I will need buffer but actionChannel is exactly it thanks, the first answer I got is exactly that ;)Counterrevolution
I
3

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);
        }
    }
}
Inainability answered 21/11, 2019 at 11:14 Comment(1)
Thank you very much this is exactly what is was looking for!Counterrevolution

© 2022 - 2024 — McMap. All rights reserved.