fluttercommunity / redux.dart

Redux for Dart
https://pub.dev/packages/redux
MIT License
519 stars 61 forks source link

Proposal - have dispatch return original action #26

Closed Paul-Todd closed 4 years ago

Paul-Todd commented 6 years ago

I am trying to port some promise based code from node and with redux thunks it was easy to return a promise from the action. What would be helpful would be if the dispatch method on the store returned the action then we could do something like:

With a LoginAction object add a Completer property and also a loginCompleted method that returns a future from the Completer. We intercept the LoginAction in the middleware and do some async stuff then call the completer to complete the action. This would keep the async flow in for example a button handler located in the button handler and allow integration with navigators and the ui in a predictable and easy manner.

class Action { final completer = new Completer();

loginCompleted() { return completer.future; } }

store.dispatch(action).loginCompleted().then((result) => print('logged in state $result');

The middleware will take the action and do some async stuff. when the middleware async operation completed the completer.complete will be called and the future completes.

brianegan commented 6 years ago

Interesting use-case, and understand why it would lead to more readable code.

@johnpryan Whatcha think? Should we do this? The change is simple, but would probably requires a major version bump since it's an API change.

johnpryan commented 6 years ago

This is an interesting idea - for now, I would like to put a pin in this for a 4.0 release, and include it with other breaking changes that may come up

Paul-Todd commented 6 years ago

Thanks guys. I am working on a medium.com article for firebase signin using this approach so it would make the code clearer but I will just reference this request then.

Do you want to close it or let me close it?

johnpryan commented 6 years ago

@Paul-Todd I think we should leave this open for now. Once this is fixed (pull requests welcome!) we could release it as a "pre-release" (4.0.0-alpha) so that you can use it right away.

tudor07 commented 5 years ago

Any updates on this ? @johnpryan

johnpryan commented 5 years ago

I haven't found time to work on this but if someone would like to claim it and make a PR that would be the fastest way.