alexeieleusis / greencat

A port of Redux(https://github.com/reactjs/redux) to Dart, including Redux Thunk(https://github.com/gaearon/redux-thunk) and a simple Logger.
Apache License 2.0
48 stars 4 forks source link

Confusing store update stream. #20

Open GRouslan opened 7 years ago

GRouslan commented 7 years ago

Store fires a state on each update, but the fired state can be older than current when delivered. Maybe should be fired a function to get a state or something else?

alexeieleusis commented 7 years ago

Agreed, but that might be because you are using async operations. I think there is not much that can be done about it, the good thing is that the stream can be sync if you need it to be, it makes testing easier also. See https://github.com/alexeieleusis/greencat/blob/master/lib/src/store.dart#L36

Does that fix your issue?

GRouslan commented 7 years ago

Yes, I need an async stream. I did not paid much attention and used the state from function parameter. So now I'm getting current state my self, but it is not too beautiful. It's a pity dart stream can not be void.

alexeieleusis commented 7 years ago

Do you have a snippet that reproduces the problem you are having with a description of what is that you want? That way would be easier to understand what is the shortcoming in greencat.

GRouslan commented 7 years ago
void onStateUpdate(AppState state) {
    state = StoreHolder.getStore().state;
    if (state.updates.isEmpty) return;
    for (RCommandSimple mine in state.updates) {
      mine();
    }
    StoreHolder.getStore().dispatch(clearUpdates());
  }

This is a function to subscribe to store update. I wanted to use state from function parameter, but it is may be too old. So I need to get state from somewhere else.

alexeieleusis commented 7 years ago

Let me see if I understand correctly:

onStateUpdate is the onData parameter when you subscribe, i.e. store.stream.listen?

alexeieleusis commented 7 years ago

If that's the case, sounds like you would be willing to lose some events in the stream and that what you need is some sort of throttling.

GRouslan commented 7 years ago

onStateUpdate is the onData parameter when you subscribe, i.e. store.stream.listen?

Yes, you are correct.

I meant that in original redux the listener function has no arguments, and you should invoke getState() to get a state from the "single source of truth": subscribelistener

alexeieleusis commented 7 years ago

I see, here you can still use it but since streams are the preferred way to send signals in dart, I exposed that in the store. But the code in your examples probably is more faithful to the approach you describe, I think both are OK as long as they provide the info you need. The stream will notify of all the changes in the order they happened.