MichaelMarner / dart-redux-remote-devtools

Remote Devtools for Dart & Flutter
https://pub.dartlang.org/packages/redux_remote_devtools
MIT License
52 stars 10 forks source link

Support for ACTION events #7

Closed kadza closed 5 years ago

kadza commented 5 years ago

Hi again,

it seems that dispatching an action from the dev tools app isn't supported. Do you plan to add it?

MichaelMarner commented 5 years ago

Time travel actions from devtools are supported if you are using a DevToolsStore for your store, from the redux_dev_tools package:

https://pub.dartlang.org/packages/redux_dev_tools

However you are right, sending arbitrary actions from devtools to the flutter app is currently not supported. I would be interested in adding this feature, but don't have a good approach yet.

The difficulty is that any actions from devtools will be a JSON object, and we would need to know how to convert the JSON to the correct Action objects to pass to redux. Happy to have the discussions on good ways to do that.

MichaelMarner commented 5 years ago

I think as a first step we could add an optional ActionDecoder parameter to our middleware that can be used to convert json to actions.

abstract class ActionDecoder {
  /// Converts a json object received from DevTools into an action
  dynamic decode(Map json);
}

At least adding this will allow Flutter app developers to provide their own implementation for their own actions.

kadza commented 5 years ago

It seems that in Flutter it isn't possible to create an instance of a type in runtime, so a developer must be responsible for JSON object to Action conversion.

The ActionDecoder will be fine for it.

One thing I don't like is that each new Action will have to be registered in the decoder. Maybe the next step will be to use the build runner to do it automatically https://github.com/dart-lang/build

MichaelMarner commented 5 years ago

Hey @kadza please have a look at this PR, if you have time:

https://github.com/MichaelMarner/dart-redux-remote-devtools/pull/10

Added an ActionDecoder that is responsible for converting a JSON payload into an action. The actual implementation is left up to the app developer, because you need to know about what Actions an app uses.

For example, you may do something like this:

class MyDecoder extends ActionDecoder {
  dynamic decode(dynamic payload) {
    final map = payload as Map<String, dynamic>;
    if (map['type'] == 'INCREMENT') {
      return IncrementAction();
    }
    else {
      return DecrementAction();
    }
  }
}