Skip to content

Instantly share code, notes, and snippets.

@atimca
Created May 18, 2020 12:58
Show Gist options
  • Save atimca/b4dd164edce591da92596a4e5320bd59 to your computer and use it in GitHub Desktop.
Save atimca/b4dd164edce591da92596a4e5320bd59 to your computer and use it in GitHub Desktop.
protocol Observer {
func stateWasChanged(with newState: State)
}
struct State {
var value: Int?
static func reduce(state: State, event: Event) -> State {
var state = state
switch event {
case .changeValue(let newValue):
state.value = newValue
}
return state
}
}
enum Event {
case changeValue(newValue: Int?)
}
class Store {
var state: State = State() {
didSet {
observers.forEach { observer in
observer.stateWasChanged(with: state)
}
}
}
var observers: [Observer] = []
func accept(event: Event) {
state = State.reduce(state: state, event: event)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment