1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00
vimr/VimR/Types.swift
2017-03-29 20:17:45 +02:00

117 lines
2.0 KiB
Swift

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
import RxSwift
typealias ActionEmitter = Emitter<Any>
class Emitter<T> {
let observable: Observable<T>
init() {
self.observable = self.subject.asObservable().observeOn(scheduler)
}
func emit(_ action: T) {
self.subject.onNext(action)
}
deinit {
self.subject.onCompleted()
}
fileprivate let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated)
fileprivate let subject = PublishSubject<T>()
}
class StateActionPair<S, A> {
let modified: Bool
let state: S
let action: A
init(state: S, action: A, modified: Bool = true) {
self.modified = modified
self.state = state
self.action = action
}
}
class UuidAction<A>: CustomStringConvertible {
let uuid: String
let payload: A
var description: String {
return "UuidAction(uuid: \(uuid), payload: \(String(reflecting: payload)))"
}
init(uuid: String, action: A) {
self.uuid = uuid
self.payload = action
}
}
class UuidState<S>: CustomStringConvertible {
let uuid: String
let payload: S
var description: String {
return "UuidState(uuid: \(uuid), payload: \(String(reflecting: payload)))"
}
init(uuid: String, state: S) {
self.uuid = uuid
self.payload = state
}
}
class Marked<T>: CustomStringConvertible {
let mark: Token
let payload: T
var description: String {
return "Marked<\(mark) -> \(self.payload)>"
}
convenience init(_ payload: T) {
self.init(mark: Token(), payload: payload)
}
init(mark: Token, payload: T) {
self.mark = mark
self.payload = payload
}
func hasDifferentMark(as other: Marked<T>) -> Bool {
return self.mark != other.mark
}
}
protocol Reducer {
associatedtype Element
func reduce(_ source: Observable<Element>) -> Observable<Element>
}
protocol Service {
associatedtype Element
func apply(_: Element)
}
protocol UiComponent {
associatedtype StateType
init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType)
}