trphoenix
2018-11-28 117664259ffca2f16ec47c5672012dfb6704b5ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
part of redux_remote_devtools;
 
/// Interface for custom action encoding logic
abstract class ActionEncoder {
  const ActionEncoder();
 
  // Converts an action into a string suitable for sending to devtools
  String encode(dynamic action);
}
 
/// An action encoder that converts an action to stringified JSON
class JsonActionEncoder extends ActionEncoder {
  const JsonActionEncoder() : super();
 
  /// Encodes an action as stringified JSON
  ///
  /// Uses the form:
  ///
  /// {
  ///   "type": "TYPE"
  ///   "payload": jsonEncode(action)
  /// }
  ///
  /// Action type is set to be the class name for class based
  /// actions, or an enum value for enum actions
  String encode(dynamic action) {
    try {
      return jsonEncode({'type': getActionType(action), 'payload': action});
    } on Error {
      return jsonEncode({'type': getActionType(action)});
    }
  }
 
  /// Gets a type name for the action, based on the class name or value
  String getActionType(dynamic action) {
    if (action.toString().contains('Instance of')) {
      return action.runtimeType.toString();
    }
    return action.toString();
  }
}