Another Flux implementation for Swift. It provides concept of "one-way data flow" with type-safe modules by Swift language.
- Create struct that implements protocol
Action
.
struct TodoActions {
struct Create : Action {
let title : String
}
}
- Create a Store class, which implement
Store
protocol with atypealiase State
- Use
Alt.getStore(YourStore.self)
to get a singleton instance of the store - Bind Actions to handers, where the store update it states.
- Invoke
self.emitChange()
to notify listeners about change of states inStore
class TodoStore : Store {
// define a typealias for the state of the model
typealias State = [Todo]
// and add a property of the State
var state : State!
// Set the initial state of the Store
static func getInitialState() -> State {
return []
}
required init() {
self.bindAction(TodoActions.Create.self, handler: self.onCreate)
// alternatively, bind action with a block
self.bindAction(TodoActions.Create.self) { [weak self] (action) -> () in
self?.state.append(Todo(title: action.title))
}
}
private func onCreate(action: TodoActions.Create) {
self.state.append(Todo(title: action.title))
self.emitChange()
}
}
- Register listeners to
store.state
change byStore.listen()
self.todoStore.listen { (state) -> (Void) in
self.tableView.reloadData()
}
TodoActions.Create(title: "New ToDo").dispatch()
- Swift 2.0
Alt is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Alt"
Francis Chong, francis@ignition.hk
Alt is available under the MIT license. See the LICENSE file for more info.