Last active
November 26, 2015 16:43
-
-
Save yonekawa/55cbd88c4a2742f64bb8 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 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