Conversation
Security InsightsNo security relevant content was detected by automated scans. Action Items
Questions or Comments? Reach out on Slack: #support-infosec. |
There was a problem hiding this comment.
.... Im second guessing these changes and kinda thinking maybe we just have a simple section that is a bit more to the point...
Integration With DevTools
In order to display the properties of Dart based Actions and State in the DevTools they must implement a toJson method.
toJson can be manually added to the classes, or added with the help of something like the json_serializable package.
State Example:
class FooState {
bool foo = true;
Map<string, dynamic> toJson() => {'foo': foo};
}When converted, the result of the toJson will be used to represent the entire state.
{
"foo": true
}Action Example:
class FooAction {
bool foo = false;
Map<string, dynamic> toJson() => {'foo': foo};
}When converted, the Class name will be the type property and toJson will become the payload property.
{
"type": "FooAction",
"payload": {
"foo": false
}
}For more encoding details check out the redux_remote_devtools package.
| #### Deep dive | ||
|
|
||
| To be able to view and interact with your custom store within the Redux Devtools, your state and actions need to be | ||
| serializable. This can be implemented manually or with the help of the [json_serializable](https://pub.dev/packages/json_serializable) library. |
There was a problem hiding this comment.
| serializable. This can be implemented manually or with the help of the [json_serializable](https://pub.dev/packages/json_serializable) library. | |
| serializable. This can be implemented manually via a `toJson` method or with the help of the [json_serializable](https://pub.dev/packages/json_serializable) library. |
|
|
||
| ___Encoding Actions and State___ | ||
|
|
||
| In the Javascript world, Redux follows a convention that your redux state is a plain Javascript Object, and actions are also Javascript objects that have a type property. The JS Redux Devtools expect this. However, Redux.dart tries to take advantage of the strong typing available in Dart. To make Redux.dart work with the JS devtools, we need to convert actions and state instances to JSON before sending. |
There was a problem hiding this comment.
| In the Javascript world, Redux follows a convention that your redux state is a plain Javascript Object, and actions are also Javascript objects that have a type property. The JS Redux Devtools expect this. However, Redux.dart tries to take advantage of the strong typing available in Dart. To make Redux.dart work with the JS devtools, we need to convert actions and state instances to JSON before sending. | |
| The Redux Devtools anticipate JS Objects. However, because Redux.dart uses Dart's strong typing, which doesn't map 1 to 1 with JS, so in order to see Dart properties in the DevTools, we need to convert the actions and state to JSON. To do this `jsonEncode` is used before sending to the DevTools, it defaults to calling `.toJson()` on the object. |
| Remember that the primary reason for using devtools is to allow the developer to reason about what the app is doing. Therefore, exact conversion is not strictly necessary - it's more important for what appears in devtools to be meaningful to the developer. | ||
|
|
||
| ___Enconding Strategy for Actions___ | ||
| We use the class name as the action type for class based actions. For enum typed actions, we use the value of the action. For example: |
There was a problem hiding this comment.
| We use the class name as the action type for class based actions. For enum typed actions, we use the value of the action. For example: | |
| The class name automatically gets used as the Action's `type` for class based actions. For enum typed actions, the value of the Action is used. For example: |
| We also want to send the action properties over to devtools. To do this, we attempt to `jsonEncode` the entire Action, and | ||
| attach this JSON data as a payload property. For example: |
There was a problem hiding this comment.
| We also want to send the action properties over to devtools. To do this, we attempt to `jsonEncode` the entire Action, and | |
| attach this JSON data as a payload property. For example: | |
| To include the Action's properties in the devtools `jsonEncode` is performed, and the resulting JSON is attached as the `payload` property. For example: |
| ```json lines | ||
| { | ||
| "type": "ClassAction", | ||
| "payload": { | ||
| "someValue": 5 // or whatever someValue was set to | ||
| }, | ||
| } | ||
| ``` |
There was a problem hiding this comment.
| ```json lines | |
| { | |
| "type": "ClassAction", | |
| "payload": { | |
| "someValue": 5 // or whatever someValue was set to | |
| }, | |
| } | |
| ``` | |
| ```json lines | |
| { | |
| "type": "ClassAction", | |
| "payload": { | |
| "someValue": "whatever someValue was set to" | |
| }, | |
| } |
| class ClassAction { | ||
| int someValue; | ||
|
|
||
| toJson() => {'someValue': this.someValue}; |
There was a problem hiding this comment.
| toJson() => {'someValue': this.someValue}; | |
| toJson() => {'someValue': someValue}; |
|
|
||
| #### TL;DR | ||
|
|
||
| Ensure that your `Action` and `State` implement a `toJson` method. |
There was a problem hiding this comment.
| Ensure that your `Action` and `State` implement a `toJson` method. | |
| Ensure that your `Action`s and `State` implement a `toJson` method. |
| [//]: # (___Overriding these strategies___) | ||
|
|
||
| [//]: # () | ||
| [//]: # (The strategy described above should work for most cases. However, if you want to do something different, you can replace the ActionEncoder and StateEncoder with your own implementations:) | ||
|
|
||
| [//]: # () | ||
| [//]: # (var remoteDevtools = RemoteDevToolsMiddleware('192.168.1.52:8000', actionEncoder: MyCoolActionEncoder);) |
There was a problem hiding this comment.
| [//]: # (___Overriding these strategies___) | |
| [//]: # () | |
| [//]: # (The strategy described above should work for most cases. However, if you want to do something different, you can replace the ActionEncoder and StateEncoder with your own implementations:) | |
| [//]: # () | |
| [//]: # (var remoteDevtools = RemoteDevToolsMiddleware('192.168.1.52:8000', actionEncoder: MyCoolActionEncoder);) |
kealjones-wk
left a comment
There was a problem hiding this comment.
@greglittlefield-wf could you also take a look at this and see if you have any feedback?
| class FooAction { | ||
| bool foo = false; | ||
|
|
||
| Map<String,dynamic> toJson() => {'foo':foo}; |
There was a problem hiding this comment.
| Map<String,dynamic> toJson() => {'foo':foo}; | |
| Map<String, dynamic> toJson() => {'foo':foo}; |
|
|
||
| In order to display the properties of Dart based `Action`s and `State` in the DevTools they must implement a `toJson` method. | ||
|
|
||
| `toJson` can be manually added to the classes, or added with the help of something like the the [json_serializable](https://pub.dev/packages/json_serializable) library. |
There was a problem hiding this comment.
Maybe we should mention that all values need to be json encodable if a value contains objects that are not directly encodable to JSON (a value that is not a number, boolean, string, null, list or a map with string keys) we will attempt to call toJson on it.
greglittlefield-wf
left a comment
There was a problem hiding this comment.
One comment and some nits, otherwise LGTM!
| ```dart | ||
| enum FooAction { | ||
| ACTION_1, | ||
| ACTION_2; |
There was a problem hiding this comment.
#nit semicolons aren't valid here
| ACTION_2; | |
| ACTION_2, |
|
Skynet test results failed initially for this build but were approved by greg.littlefield |
|
@Workiva/release-management-p |
Motivation
There was a support question on how to properly integrate with the devtools for a module. It appears that these steps don't exist here yet.
Changes
Release Notes
Review
See CONTRIBUTING.md for more details on review types (+1 / QA +1 / +10) and code review process.
Please review:
QA Checklist
Merge Checklist
While we perform many automated checks before auto-merging, some manual checks are needed: