Created
November 26, 2015 15:46
-
-
Save yonekawa/fae9bd25940dfc0c6942 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
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