Skip to content

Instantly share code, notes, and snippets.

@raxityo
Created July 30, 2020 14:02
Show Gist options
  • Save raxityo/a99cfec3249af58e6d0cea49441f614a to your computer and use it in GitHub Desktop.
Save raxityo/a99cfec3249af58e6d0cea49441f614a to your computer and use it in GitHub Desktop.
import RxCocoa
import RxRelay
import RxSwift
struct Node {}
struct Item {}
protocol ReduxState {
associatedtype Action: ReduxAction
func new(action: Action) -> Self
}
protocol ReduxAction {}
struct State {
static let relay = BehaviorRelay<State>(value: State(home: .initial))
let home: Home
func new(action: ReduxAction) -> State {
if let action = action as? Home.Action {
return copy(home: home.new(action: action))
}
return self
}
private func copy(home: Home? = nil) -> State {
State(home: home ?? self.home)
}
private init(home: Home) {
self.home = home
}
}
struct Initial: ReduxAction {}
func dispatch(_ action: ReduxAction) {
Actions.actions.accept(action)
}
struct Actions {
fileprivate static let actions = BehaviorRelay<ReduxAction>(value: Initial())
static func startSubscription() {
_ = Actions.actions
.observeOn(backgroundScheduler)
.map { action in
let newState = State.relay.value.new(action: action)
State.relay.accept(newState)
}
.subscribe()
}
}
struct Home: ReduxState {
static let initial = Home(nods: [], items: [])
let nods: [Node]
let items: [Item]
private init(nods: [Node], items: [Item]) {
self.nods = nods
self.items = items
}
func new(action: Action) -> Home {
switch action {
case let .addNewNode(node):
return Home(nods: nods + [node], items: items)
}
}
enum Action: ReduxAction {
case addNewNode(node: Node)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment