Melodux
A super simple Redux implementation in Swift.
Usage
- Define a
struct
which defines the shape of your state. - Define an
enum
which defines the actions available to your reducers and middleware.- These
case
s shouldlet
the arguments they take.
- These
- Define your app’s initial state that conforms to your struct above.
- Define a reducer
- if you want to spread your logic into categories use
Store.composeReducers
.
- if you want to spread your logic into categories use
- Optional, define middlewares and register with `store.registerMiddlewares([])
- Profit.
## Full example
struct TestStateShape {
var name: String
var version: String
}
enum TestStateActions {
case SetName(String)
case SetVersion(String)
}
func rootReducer(action: TestStateActions, state: TestStateShape) -> TestStateShape {
switch (action) {
case .SetName(let name):
return TestStateShape(name: name, version: state.version)
case .SetVersion(let version):
return TestStateShape(name: state.name, version: version)
}
}
let initialState = TestStateShape(name: "", version: "")
let store = Melodux.Store<TestStateActions, TestStateShape>(initialState: initialState, rootReducer: rootReducer)
store.dispatch(action: .SetName("Melodux"), background: false)
I use this as an @EnvironmentObject
in my Swift app “Melodist”, a cross platform musical tablature editor.