-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
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
Use change-emitter module to handle subscriptions #1729
Conversation
The obvious disadvantage is that if we have subscription-related bugs, they'll have to be resolved in a separate project. Potential upside is that if the package gets more libraries to use it (history, perhaps), we'll all benefit from its maintenance. |
for an example of something that should be addressed within this package see #1626 |
I think I’d be confident extracting it if we define a strict contract that it promises to satisfy. There is a thousand ways to implement a change emitter, and some constraints that Redux cares about might not be generally applicable so it’s easy to imagine them being changed as implementation details of a module like this, potentially leading to breakage in Redux. If, on the contrary, we keep the tests in Redux to prevent problems like this, I don’t see much of a point of keeping that code outside, as this increases rather than decreases the maintenance burden. |
Agreed.
Could you elaborate? I agree with the statement but I'm having trouble putting the contract that Redux follows into words. As far as I can tell, the important bits are
Is there anything else I'm missing? |
I think so but I forget these rules all the time. The important part is to take a naïve connect implementation like mount() {
this.mostRecentState = store.getState()
this.unsubscribe = store.subscribe(this.handleChange.bind(this))
}
unmount() {
this.unsubscribe()
this.unsubscribe = null
}
handleChange() {
if (this.unsubscribe) {
this.mostRecentState = store.getState()
}
} and make sure that |
Haha me too. I think regardless of whether we extract it, we need to do a better job documenting this contract, either via better tests or an actual document. I'll look into this sometime soon. |
We have some clarifications in |
Yeah this seems like the most important part, and yet we don't have a test for it :D The only thing we currently test is how often listeners are called, not what the state is at that time. |
We should rewrite the tests. 👍 |
(With a fake naïve |
Hey @acdlite, any updates on this? This will also be helpful in React Redux, so this could kill a lot of birds with one stone 👍 |
createStore
includes a really handy, bare-bones event subscription implementation that I found myself wanting in a few other projects. (E.g. here: acdlite/recompose#160). So I extracted it out into a separate module: https://github.com/acdlite/change-emitterWhen I tweeted about it, @tappleby pointed me to this issue (which, by coincidence, was closed just three hours ago :D) tappleby/redux-batched-subscribe#8.
This PR rewrites
createStore
to use change-emitter. Not sure if it's something we want to do but I figured we should discuss it.At the very least, I think it would be valuable to extract the subscription stuff into a separate file, even if it stays within the Redux project.