Skip to content

Instantly share code, notes, and snippets.

@yonekawa
Last active November 26, 2015 16:43
Show Gist options
  • Save yonekawa/55cbd88c4a2742f64bb8 to your computer and use it in GitHub Desktop.
Save yonekawa/55cbd88c4a2742f64bb8 to your computer and use it in GitHub Desktop.
import APIKit
import SwiftFlux
struct TodoActions {
struct Fetch {
typealias Payload = Todo
func invoke(dispatcher: Dispatcher) {
Session.sendRequest(TodoFetch(id: 1)) { (result) in
switch response {
case .Success(let todo):
dispatcher.dispatch(self, result: Result(value: todo))
case .Failure(let error):
dispatcher.dispatch(self, result: Result(error: error))
}
}
}
}
}
class TodoStore: Store {
enum TodoEvent {
case FetchSuccess
case FetchFailed
}
typealias Event = TodoEvent
let eventEmitter = EventEmitter<TodoStore>()
private var internalTodo: Todo?
var todo: Todo? {
return internalTodo
}
init() {
ActionCreator.dispatcher.register(TodoActions.Fetch.self) { (result) in
switch result {
case .Success(let todo):
self.internalTodo = todo
self.eventEmitter.emit(Event.FetchSuccess)
case .Failure(let error):
self.eventEmitter.emit(Event.FetchFailed)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment