Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.0.5 fork #3836

Closed
wants to merge 4 commits into from
Closed

4.0.5 fork #3836

wants to merge 4 commits into from

Conversation

MosheZemah
Copy link

Allow configure the store to notify listeners (store's subscribers) only once per frame and not on every dispatch.
For example let say I have 10 dispatches on the same frame (i.e. one function that calls 5 dispatches and each one of them calls another dispatch). Instead of running the listener on every dispatch, I want to be able to run them only once after all dispatches are over (for performance).

@markerikson
Copy link
Contributor

markerikson commented Aug 3, 2020

Appreciate the PR, but there's no need to modify the core for this. Multiple addons already offer this functionality, like redux-batched-subscribe and redux-batch. You can also use the React-Redux batch() API to help with this:

https://redux.js.org/faq/performance#how-can-i-reduce-the-number-of-store-update-events

https://react-redux.js.org/api/batch

@markerikson markerikson closed this Aug 3, 2020
@netlify
Copy link

netlify bot commented Aug 3, 2020

Deploy preview for redux-docs ready!

Built with commit 43dbc6f

https://deploy-preview-3836--redux-docs.netlify.app

@MosheZemah
Copy link
Author

Hi @markerikson , thanks for your quick response. Using the addons you've mentioned can I skip calling the listeners completely? Do I need to do it on every place explicitly in the code?
My solution offer doing it globally for all dispatches. Is this something I can accomplish with the addons?
Thanks!

@markerikson
Copy link
Contributor

Store enhancers like redux-batch and redux-batched-subscribe wrap the store at creation time, so they apply globally.

The React-Redux batch() API is something you would use yourself in specific locations, such as dispatching multiple actions as a result of an async thunk call.

@MosheZemah
Copy link
Author

Hey @markerikson, I looked at the redux_batched_subscribe as you suggested. It seems like they override the internal logic (subscribe) of redux, and it seems like they've copied from an old branch of redux. It also means that if I'll use their package, I no longer receive updates of the logic of subscribe/unsubscribe if you change it in the future. (i.e. they are missing the memory cleanup that was made in 4.0.5 after unsubscribe.
It was better if redux has an API instead of overriding internal logic.

Maybe we should have an option to pass a middleware that will wrap notify subscribers.
My PR allow users to continue get updates from the redux package without overriding any internal logic.

@markerikson
Copy link
Contributor

The point of the enhancer API is to allow addons to override and provide their own implementations of the core store API ({dispatch, subscribe, getState, replaceReducer}):

https://blog.isquaredsoftware.com/2017/09/presentation-might-need-redux-ecosystem/

That contract does not let enhancers see the internals of the existing APIs from the lower-level store being wrapped.

In addition, the actual store implementation knows nothing about "frames". The core store.dispatch contract is that it sequentially calls all subscribers, synchronously.

We're not modifying the core store implementation for this use case.

Given how rarely the store API actually changes, I don't see a need to try to provide an override point just for enhancers to pick up any hypothetical future tweaks to the dispatching logic. That's especially true since the point of an enhancer is that it's going to provide its own implementation of dispatch or subscribe, without needing to know anything about how the lower API is implemented.

The addons I've linked already allow you to solve your use case, in varying ways. You should probably also read my post A Comparison of Redux Batching Techniques, which discusses when it might make sense to use each of those approaches.

For reference, there were some discussions back in 2016-ish about hypothetical modifications to the enhancer definition to allow splitting that logic into smaller overridable pieces, but those PRs stalled and we eventually closed them due to lack of progress and lack of actual interest. See these prior issues/PRs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants