MonitorMiddleware

public class MonitorMiddleware: Middleware

Middleware that transmits the state and actions to the Suas Monitor mac app Get the latest version from https://github.com/zendesk/Suas-monitor/releases

MonitorMiddleware needs to convert actions and states to [String: Any] so that it can transmit them to the SuasMonitor mac app. That can be done with:

  • Implement SuasEncodable in your Actions and States. MonitorMiddleware will use this protocol to convert the action and state to [String: Any]
  • OR implement stateEncoder and actionEncoder: MonitorMiddleware will call these callbacks passing the state and action. The callbacks inturn have to return a [String: Any]

MonitorMiddleware Encoding behaviour:

  • First, MonitorMiddleware will try to cast the state or action to SuasEncodable. If your state/action implement the SuasEncodable protocol, then Monitor will use that to convert them to JSON for transmission.
  • If the state or action does not implement SuasEncodable, the middleware will use the stateEncoder and actionEncoder. The middleware will call these blocks with the state/action to get back a dictionary. The converted dictionary will be used by the middleware for transmission to the desktop app.
  • Finally, if none of the above works. The middleware will use the state or action debug description as the string to transmit to the monitor desktop app.

Note about swift 3.2+

In Swift 3.2 and newer versions you can implement SuasEncodable in your state types without writing code. SuasEncodable uses Swfit’s Econdable protocol behind the scene.

Examples

Implementing SuasEncodable manually (pre swift 3.2)

Types that implement SuasEncodable will be transmitted to the Suas Monitor desktop app

struct MyState: SuasEncodable {
  let value: Int

  func toDictionary() -> [String : Any] {
    return [
      "value": value
    ]
  }
}

Implementing SuasEncodable automatically (swift 3.2 and newer)

There is no code needed to implement SuasEncodable in your types in swift 3.2 and newer

struct MyState: SuasEncodable {
  let value: Int
}

Use stateEncoder and actionEncoder

You can pass a stateEncoder and/or an actionEncoder to be used when converting states and/or actions to dictionaries for transmission

let middleware = MonitorMiddleware(
  stateEncoder: { state in
    if let state = state as? CounterState {
      // return dictionary
    }
    if let state = state as? OtherState {
      // return dictionary
    }
  },
  actionEncoder: { action in
    if let action = action as? IncrementAction {
      // return dictionary
    }
    if let action = action as? DecrementAction {
      // return dictionary
    }
  },
)

// Use the middleware
let store = Suas.createStore(reducer: TodoReducer(),
                             middleware: middleware)
  • Create a MonitorMiddleware

    Declaration

    Swift

    public convenience init(debugOnly: Bool = true,
                              stateEncoder: EncodingCallback<Any>? = nil,
                              actionEncoder: EncodingCallback<Action>? = nil)

    Parameters

    debugOnly

    start the monitor in debug mode only (optional, defaults starting the monitor in debug only configuration)

    stateEncoder

    (optional) callback that converts a state type to [String: Any].

    actionEncoder

    (optional) callback that converts an action type to [String: Any]

  • Declaration

    Swift

    public func onAction(action: Action,
                           getState: @escaping GetStateFunction,
                           dispatch: @escaping DispatchFunction,
                           next: @escaping NextFunction)

    Parameters

    action