Skip to content

Instantly share code, notes, and snippets.

@NicholasTD07
Last active October 5, 2016 10:21
Show Gist options
  • Save NicholasTD07/f7c6f2fb7863ca4ad85c9b9a6dfa1dd3 to your computer and use it in GitHub Desktop.
Save NicholasTD07/f7c6f2fb7863ca4ad85c9b9a6dfa1dd3 to your computer and use it in GitHub Desktop.
// Swift 3
protocol ActionType { }
struct InitialAction: ActionType { }
class Store<State> {
var state: State!
typealias Reducer = (State?, ActionType) -> State
final let reducer: Reducer
init(with reducer: @escaping Reducer) {
self.reducer = reducer
dispatch(InitialAction())
}
typealias Subscriber = (Store) -> ()
final var subscribers = [Subscriber]()
final func dispatch(_ action: ActionType) {
self.state = reducer(state, action)
subscribers.forEach {
$0(self)
}
}
final func subscribe(with subscriber: @escaping Subscriber) {
subscribers.append(subscriber)
subscriber(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment