Classes
The following classes are available globally.
-
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
SuasMonitormac app. That can be done with:- Implement
SuasEncodablein your Actions and States.MonitorMiddlewarewill use this protocol to convert the action and state to [String: Any] - OR implement
stateEncoderandactionEncoder:MonitorMiddlewarewill call these callbacks passing the state and action. The callbacks inturn have to return a[String: Any]
MonitorMiddlewareEncoding behaviour:- First, MonitorMiddleware will try to cast the state or action to
SuasEncodable. If your state/action implement theSuasEncodableprotocol, 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 thestateEncoderandactionEncoder. 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
SuasEncodablein your state types without writing code.SuasEncodableuses Swfit’sEcondableprotocol behind the scene.Examples
Implementing SuasEncodable manually (pre swift 3.2)
Types that implement
SuasEncodablewill be transmitted to the Suas Monitor desktop appstruct 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
SuasEncodablein your types in swift 3.2 and newerstruct 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
See morelet 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)Declaration
Swift
public class MonitorMiddleware: Middleware - Implement
View on GitHub
Classes Reference