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 2
protocol ActionType { }
struct InitialAction: ActionType { }
class Store<State> {
var state: State!
typealias Reducer = (state: State?, action: ActionType) -> State
final let reducer: Reducer
init(with reducer: Reducer) {
self.reducer = reducer
dispatch(InitialAction())
}
typealias Subscriber = (store: Store) -> ()
final var subscribers = [Subscriber]()
final func dispatch(action: ActionType) {
self.state = reducer(state: state, action: action)
subscribers.forEach {
$0(store: self)
}
}
final func subscribe(with subscriber: Subscriber) {
subscribers.append(subscriber)
subscriber(store: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment