Last active
October 5, 2016 10:21
-
-
Save NicholasTD07/f7c6f2fb7863ca4ad85c9b9a6dfa1dd3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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