Yet another Redux written in Swift
Add this repo as a dependency in your Package.swift
file, for example:
import PackageDescription
let package = Package(
name: "Example",
dependencies: [
.Package(url: "https://github.com/NicholasTD07/TDRedux.swift.git",
majorVersion: 2),
]
)
github "NicholasTD07/TDRedux.swift" ~> 2.0
pod 'TDRedux.swift', '~> 2.0'
A Store holds a State. A State is the entire internal state of an application, which could be anything.
You can
- create a
Store
with a Reducer - dispatch Actions to change a Store's State
- subscribe to changes happens to the State of a Store
Read more: Redux in Swift Part 1 - Store and State
A Reducer is a function which takes an optional State and an Action and returns a new State, defined as the following:
class Store<State> {
typealias Reducer = (State?, Action) -> State
}
Read more: Redux in Swift Part 1 - Reducer and Action
An Action describes what happened while it may carry some data, e.g. AddTodo(text: "buy milk")
or ToggleTodo(atIndex: 1)
Action
type is defined as
typealias Action = Any
Read more: Redux in Swift Part 1 - Reducer and Action
A helper function which wraps Reducers that take non-optional State
and specific type of Action
, so that: No more type-casting in hand written Reducers anymore. For example:
typealias CounterState = Int
enum CounterActions {
case increase
case decrease
}
let counterReducer: (CounterState, CounterActions) = /* ... */
let wrappedReducer: (CounterState?, Any) = Reducer(initialState: 0, reducer: counterReducer)
let counterStore = Store<CounterState>.init(with: wrappedReducer)
A helper function which combines an array of Reducers into one. For example:
typealias State = /* ... */
let arranOfReducers: [(State?, Any)] = [ /* ... */ ]
let combinedReducers: (State?, Any) = combineReducers(arranOfReducers)
It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.
Store
and every property and methods in Store
are defined as final
.
Let me know if you think the Store
need to be subclassed and overridden.
I am not too familiar with this concept in Redux and also I am not sure whether there are potential issues in my implementation or not.
TDRedux is released under the MIT license. See LICENSE for details.