Diego Gallegos
Diego Gallegos

Reputation: 1752

Action Channel in redux-saga calls repeated actions

I am trying to run several request sequentially with actionChannel the issue is that the action and its payload its being retrieved several times (take) when I expect only to be retrieved once. This is a reproducible example:

function* requestSequentially(action) {
  console.log('action', action); // contains type and payload
  const chan = yield actionChannel(action.type)
  let index = 0;
  while (true) {
    const ac = yield take(chan)
    yield index += 1
    console.log('channel', ac, index);
  }
}

The behavior is as follow

channel <action> 3
channel <action> 2
channel <action> 1

If 4th call, well you can imagine it start from 4 then go down to 1.

EDIT: Saga is exported into rootSaga as: takeEvery(SAVE_SEQUENTIALLY, requestSequentially), ...

and action: export const action = payload => ({ type: SAVE_SEQUENTIALLY, payload })

How can I make actionChannel to only take the action once?

Upvotes: 1

Views: 2311

Answers (1)

Martin Kadlec
Martin Kadlec

Reputation: 4985

I might be misunderstanding what you are trying to do but to me it seems the problem is the combination of takeEvery and the actionChannel+take, because both of them are waiting for the same action type.

I think instead of takeEvery you should just fork the requestSequentially saga with given type:

yield fork(requestSequentially, SAVE_SEQUENTIALLY)

And then use the given type for the actionChannel:

function* requestSequentially(type) {
  const chan = yield actionChannel(type)
  let index = 0;
  while (true) {
    const ac = yield take(chan)
    yield index += 1
    console.log('channel', ac, index);
  }
}

Upvotes: 1

Related Questions