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

Remove State snapshot from StoreSubscriber #8

Closed
kittinunf opened this issue Aug 31, 2016 · 14 comments
Closed

Remove State snapshot from StoreSubscriber #8

kittinunf opened this issue Aug 31, 2016 · 14 comments

Comments

@kittinunf
Copy link
Contributor

kittinunf commented Aug 31, 2016

Based on main redux documentation, listener function signature is subscribe(listener: () => void): Unsubscribe;

https://github.com/reactjs/redux/blob/master/docs/FAQ.md#how-do-i-subscribe-to-only-a-portion-of-the-state-can-i-get-the-dispatched-action-as-part-of-the-subscription

I propose we should match that in other words, we remove the state argument from StoreSubscriber

From

public interface StoreSubscriber<S> {
    void onStateChange(S state);
}

To

public interface StoreSubscriber<S> {
    void onStateChange();
}

Then it is responsibility of the call site to call store.state in order to get current state value.

@kittinunf kittinunf changed the title Remove State snapshot from StateSubscriber Remove State snapshot from StoreSubscriber Aug 31, 2016
@beyondeye
Copy link
Owner

beyondeye commented Aug 31, 2016

I am afraid this cannot be done. There reason is that Reduks engine is much more asynchronous than the original. multiple reduce/dispatch operation can run in parallel, and notification to subscribers happens independently (if yow want take a look at KovenantStore). This is actually a design choice

@kittinunf
Copy link
Contributor Author

kittinunf commented Aug 31, 2016

Wouldn't that make the change even more compelling? Call site could presume that the state that sent along with argument is current one, while the truth is it isn't.

The reason behind is that I update UI by using the argument from the subscriber block. Little did I know that the state has been changed behind my back to something else by dispatching another function. This makes my UI render the out-dated state instead of using the latest version which looks wrong to me.

@beyondeye
Copy link
Owner

If you are using rxJava or simplestore the behavior is what you expect to be.

@beyondeye
Copy link
Owner

So don't be afraid. Anyway I will look into it. Perhaps I need to change the design

@kittinunf
Copy link
Contributor Author

kittinunf commented Aug 31, 2016

I understand, just want to let you know that I was bitten hard by UI jerking reason is using the state argument from the subscriber block. When I moved to use store.state, everything is fine and working as expected. Perhaps, this is why main redux repo moving away from that version, as well. I remember they were using the (State) => void in the past.

@beyondeye
Copy link
Owner

Can you tell me more about what was happening in your UI? I would like to try to understand what was happening

@beyondeye
Copy link
Owner

for reference here is the discussion about it in redux
subscribe API with state as an argument · Issue #303 · reactjs/redux
reduxjs/redux#303

@beyondeye
Copy link
Owner

In brief: the main reason is to make the methods in Store API, subscribe() and getState() are orthogonal on purpose so if you “override” one in a store enhancer, you don’t need to override the other.

@kittinunf
Copy link
Contributor Author

But, redux.js still using the () => void, isn't it? I am still convinced that we should follow them. My problem with that as I mentioned to you is the state argument was an old copy version of my state.

@beyondeye
Copy link
Owner

I think we actually have two issues here:

  1. Reduks Store API vs original Redux Store API
  2. state changes debouncing
    Apparently by using getState you are effectively getting the debounced state changes. This is what you need in most cases, but even in the original redux this is not the default behaviour. There is a special store enhancer for that (https://github.com/tappleby/redux-batched-subscribe)

@beyondeye
Copy link
Owner

beyondeye commented Aug 31, 2016

About the first issue I agree with you. Just I need to think a bit to make it work with KovenantReduks.
About the second issue, need to decide if going Redux way (have a separate store enhancer) or add some built in options to debounce state changes

@beyondeye
Copy link
Owner

The problem with porting Redux is that the core API is relatively low level. It is not really useful without adding a lot of other stuff. This is the reason I added other things like Selectors to Reduks core. Then it becomes a little bit opinionated. Not to mention that kotlin and javascript are quite different in many aspects, which forces additional choices that can distance Reduks from the original. It is not easy to strike the right balance

@beyondeye
Copy link
Owner

solved in release 2.0.0b6

@kittinunf
Copy link
Contributor Author

💛

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

No branches or pull requests

2 participants