Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MQTT.pdf

GDG Cherkasy
January 26, 2019
32

 MQTT.pdf

GDG Cherkasy

January 26, 2019
Tweet

More Decks by GDG Cherkasy

Transcript

  1. final class Store<State: StateProtocol>: Dispatcher { private var state: State

    { didSet { self.observers.forEach { $0.perform(with: self.state) } } } init(state: State) { self.state = state } func dispatch(actions: Action) { queue.async { self.state = actions.reduce(self.state) { state, action in let newState = reduce(state: state, action) self.middleware.forEach { $0.perform(with: (state, action, newState)) } return newState } } } ..... }
  2. import Foundation public protocol StateProtocol: Encodable { static var initial:

    Self { get } static func reduce(state: Self, action: Action) -> Self } public struct State: StateProtocol { public let networkState: NetworkState public static let initial: State = .init( networkState: .initial ) public static func reduce(state: State, action: Action) -> State { return State( networkState: NetworkState.reduce(state: state.networkState, action: action) ) } } public struct NetworkState: StateProtocol { let isOffline: Bool public static let initial = NetworkState(isOffline: false) public static func reduce(state: NetworkState, action: Action) -> NetworkState { switch action { case is EnterOfflineMode: return .init(isOffline: true) case is SuccessAction: return .init(isOffline: false) default: return state } } }
  3. import UIKit class WeatherTableViewController: UITableViewController { /*implementation*/ } extension WeatherTableViewController

    { struct Props: Codable { let refreshState: RefreshState; enum RefreshState: AutoCodable { case inactive(activate: Command) case active } let groups: [Group]; struct Group: Codable { let title: String var patients: [ReassignUserTableViewCell.Props] } static let initial = Props( refreshState: .inactive(activate: .nop), groups: [] ) } } class ReassignUserTableViewCell: UITableViewCell { /*implementation*/ } extension ReassignUserTableViewCell { struct Props: Codable { let effectiveDate: Date let phrase: String let icon: UIImage let temperature: WeatherFeature<Double> let realFeel: WeatherFeature<Double> let windSpeed: WeatherFeature<Double> let windDirection: String let select: CommandWith<UIViewController> } }
  4. ACTIONS public struct BeginRefreshingWeather: Action {} public struct DidRefreshWeather: Action

    { let forecast: [Weather] } public struct DidFailRefreshingWeather: FailedAction: {} •
  5. import Foundation class MQTT { func publish<T>(in channel: String) ->

    CommandWith<T> where T: Encodable { return CommandWith { message in guard let data = try? JSONEncoder().encode(message) else { return } self.client.publish(data, topic: channel, qos: 0, retain: true) } } } let mqtt = MQTT() let store = Store<State>(state: .initial) store.observe(with: mqtt.publish(in: "store/state")) STORE B
  6. let vc = WeatherTableViewController() let render = CommandWith(action: vc.render).dispatched(on: .main)

    let publish: CommandWith<WeatherTableViewController.Props> = mqtt.publish(in: "props/WeatherTableViewController") let presenter = WeatherTableViewPresenter( store: store, render: render.then(publish.perform) ) P UI Props B