Skip to content

Instantly share code, notes, and snippets.

@yonekawa
Created November 26, 2015 15:46
Show Gist options
  • Save yonekawa/fae9bd25940dfc0c6942 to your computer and use it in GitHub Desktop.
Save yonekawa/fae9bd25940dfc0c6942 to your computer and use it in GitHub Desktop.
import SwiftFlux
struct CounterActions {
struct Plus: Action {
typealias Payload = Int
let number: Int
func invoke(dispatcher: Dispatcher) {
dispatcher.dispatch(self, result: Result(value: number))
}
}
struct Minus: Action {
typealias Payload = Int
let number: Int
func invoke(dispatcher: Dispatcher) {
dispatcher.dispatch(self, result: Result(value: number))
}
}
}
class CounterStore: ReduceStore<Int> {
override init() {
super.init()
self.reduce(CalculateActions.Plus.self) { (state, result) -> Int in
switch result {
case .Success(let number): return state + number
default: return state
}
}
self.reduce(CalculateActions.Minus.self) { (state, result) -> Int in
switch result {
case .Success(let number): return state - number
default: return state
}
}
}
override var initialState: Int {
return 0
}
}
let store = CounterStore()
store.eventEmitter.listen(.Changed) { () in
println(store.state)
}
ActionCreator.invoke(CalculateActions.Plus(number: 3))
ActionCreator.invoke(CalculateActions.Minus(number: 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment