manaflair / redux-batch

Enhance your Redux store to support batched actions
174 stars 12 forks source link

Question: Take one out of batch support? #9

Closed Noitidart closed 7 years ago

Noitidart commented 7 years ago

I am new to redux-saga so asked about batching puts and they recommended 3rd party lib. I then remembered redux-batch was one of the libs recommended by batching on redux docs - http://redux.js.org/docs/faq/Performance.html#performance-update-events - http://i.imgur.com/MuquVWw.png . I also remembered how I used it and loved it with thunks, and now I see it was designed for saga which is really cool

However I got a warning from the owner of the redux-saga repo he says:

https://github.com/redux-saga/redux-saga/issues/1161#issuecomment-327790395

Keep in mind that u probably often want to take 1 action out from the batch. You can do so by providing a custom emit function during redux-saga initialization like here

I don't understand that what that means, but I wanted to ask if redux-batch supports this out of the box, and if possible could you please explain how it supports this.

I thought if I run multiple puts then I can take from each put no?

arcanis commented 7 years ago

I'm not entirely sure what @Andarist meant by that, but I think that redux-batch should support pretty much everything you usually do with sagas. The only limitation I know of is dictated by Redux and is that multi-nested actions can sometimes be tricky (#8).

Andarist commented 7 years ago

I only meant that with redux-batch you dispatch a single action object containing multiple actions. However in most cases for take effect resolution you are interested in those contained actions unpacked.

Even without providing custom emit its possible to listen for a single action with something like:

const _action = yield take(ac => ac.type === 'BATCH' && ac.actions.some(batched => batched.type === 'MY_ACTION_TYPE'))
const action = _action.actions.find(ac => ac.type === 'MY_ACTION_TYPE')

but lets face it - its not handy. Therefore we have introduced a way to write custom emit function which can unpack batched action and emit single actions into the saga runtime. When used you can use take like normal:

const action = yield take('MY_ACTION_TYPE')
arcanis commented 7 years ago

@Andarist oh I see - the redux-batch in this repository is a bit different from the other packages with similar names in that it's an enhancer rather than a middleware. The conversion from batched action to multiple actions is completely transparent to the userland code, so you don't need to process it manually from inside sagas!

Noitidart commented 7 years ago

Thank you @Andarist for looking out for looking out for us for common pitfalls. I'm new to sagas and absolutely superbly loving it!

Thanks @arcanis for helping me check to make sure this batching lib is complete! :)