tappleby / redux-batched-subscribe

store enhancer for https://github.com/reactjs/redux which allows batching subscribe notifications.
MIT License
504 stars 32 forks source link

Undesired Actions Notifications #4

Open pjago opened 8 years ago

pjago commented 8 years ago

Hi. I've designed a custom redux middleware in a attempt to really push all side-effects away from my view code by means of redux-saga. I am basically appending a FIFO "TaskReducer" to my single redux store, and handling any QUEUE actions with it:

//queue action creator
function queue( task ){
  return {
    type: 'QUEUE',
    payload: task
  };
};
//taskMiddleware
export default store => next => action => {
  const result = next(queue(action));
  return result;
}
//and a TaskReducer that merges repeated tasks in a meaningful way (e.g. a FIFO queue) ...

I find this way of delegating tasks quite convenient for performance reasons. However, since the QUEUE action plays as any other action in redux, any store listeners (e.g. hundreds of connected components) will eventually waste time on it, and even DevTools won't work properly since I am not interested in replaying side-effects...

So I was wondering if this situation is of any interest for this repository, as it seems to me that this is the correct place to implement a solution for "undesired action subscriptions".

Perhaps passing the dispatched action as an argument for the batch function would solve this? That way we could easily check for the action type and decide whether we should notify the listeners or not:

const store = batchedSubscribe((notify, action) => {
  if(action.type !== QUEUE)
    requestAnimationFrame(notify);
})(createStore)(reducers)

Anyways, is this worth pull requesting?

tappleby commented 8 years ago

I think this could be of some value, conditional notifications is a topic that has come up a few times; there is even @tshelburne package redux-skip-by-action who's code could be simplified by having something like this.

With that being said I do wonder if conditional subscriber notifications is a case of premature optimization, I could see it causing some difficult to debug situations. See rackt/redux#580 for a similar issue.

I dont mind adding support for this as I do believe why waste cycles if you don't need to, my only concern is it getting misused.

@gaearon do you have any thoughts on this?