Created
May 18, 2020 12:58
-
-
Save atimca/b4dd164edce591da92596a4e5320bd59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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