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

How to return something when an Action is done? #13

Closed
SunlightDave opened this issue Aug 28, 2019 · 8 comments
Closed

How to return something when an Action is done? #13

SunlightDave opened this issue Aug 28, 2019 · 8 comments

Comments

@SunlightDave
Copy link

The RefreshIndicator's onRefresh wants a Future Function(), that returned Future must complete when the refresh operation is finished.

Before I would make the call to the API first and then when the response returns I would dispatch an (synchronous) Action and return null.

Now with ReduxAction that can reduce async, I move the API call inside the Action, but what would be the proper way to return something as soon as the Action is finished ?

StoreConnector<AppState, Overview>(
  converter: (store) => store.state.overview,
  builder: (context, overview) {
    return overview == null
      ? Center(child: CircularProgressIndicator())
      : RefreshIndicator(
        onRefresh: () {
          StoreProvider.of<AppState>(context, 'refresh').dispatch(GetOverview());
          return Future.delayed(Duration(seconds: 1));
          /// Before I did this:
          //return ApiService().getOverview().then((Overview ov) {
          //  StoreProvider.of<AppState>(context, 'refresh').dispatch(SetOverview(ov));
          //  return null;
          //}
        },
        child: ListView(...),
@marcglasberg
Copy link
Owner

I'm not sure I understand your question. But if you want an action to notify when it finished doing some stuff, you could create a Future and pass its completer to the action, as a parameter to its constructor. Then you return that Future. The action would complete the future as soon as it has finished doing its thing. Does that make sense?

@SunlightDave
Copy link
Author

SunlightDave commented Aug 28, 2019

Thank you for the help (I first tried to solve this with Events).

On another note, I'm not quite sure if this not something I should do or not.
Is it discouraged to dispatch Actions without putting them as Callbacks in a ViewModel?

If not, then a Function something like this in the StoreProvider would be great, to easily dispatch Actions:

static void dispatch<St>(BuildContext context, ReduxAction action, Object debug) {
  StoreProvider.of<St>(context, debug).dispatch(action);
}

@marcglasberg
Copy link
Owner

marcglasberg commented Aug 28, 2019

  1. Maybe a good idea is for me to create a dispatchGetFuture that already returns you a Future as soon as the action completes. You would use it for things like the RefreshIndicator that need a Future that tells you when the action completes.

  2. I don't think there is anything inherently wrong with getting the store and doing direct dispatches without the ViewModel or even without the StoreConnector, directly inside of the widget. However:
    • If you have the StoreConnector without the ViewModel it will rebuild everytime. The new ViewModel being equal to the previous one is what prevents the widget from rebuilding. So if you need the ViewModel anyway, do you actually gain anything by not creating the callbacks?
    • I think that would make more sense if you want to dispense with the StoreConnector altogether, and dispatch actions directly from the final widget . But when you do it with the StoreConnector you force yourself to cleanly separate the widget from the way it gets its data. So, it's better for clean code, and helps a lot with tests later. How would you test the RefreshIndicator if you don't have the Connector? You would have to create the store, and dispatch an action. And maybe you would have to use the StoreTester, and mock the action. However, if you use the Connector/Dumb-Widget architecture you just need to use a callback in your tests, which is much easier to setup.

In any case I don't claim to be an authority in all kinds of possible architecture designs with Redux or even with AsyncRedux. So, I may be wrong.

@marcglasberg
Copy link
Owner

#14

@marcglasberg
Copy link
Owner

#16

@marcglasberg
Copy link
Owner

This is now live: version: 1.3.8.

@marcglasberg
Copy link
Owner

Are @SunlightBro and @SunlightDave the same person? I did some changes in main_dispatch_future.dart, (including changing the file name), but you are the author. If you want you can list yourself as the author in the top file comment.

@SunlightBro
Copy link
Contributor

Yes same person,
just keep it the way its is.

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

3 participants