Classes
The following classes are available globally.
-
Create a reducer inline with a block
Example
Create a reducer without defining a class/struct that implements
Reducer
let myReducer = BlockReducer(state: 1) { state, action in guard let newState = state as? Int else { return state } // cast the action to a specific type if action is SomeAction { return newState + 1 } return newState }
Note: Returning
See morenil
fromBlockReducer
signifies that the state did not change which will not inform the listenersDeclaration
Swift
public final class BlockReducer<StateType>: Reducer
-
Reducer that represents a combination of two reducers This reducers is not to be implemented by hand. You combine reducer by adding them with the
+
operator.let combinedReducers = Reducer1() + Reducer2() + Reducer3()
Example
Combine reducers
If your application has two screens, you can model that with two states, for example
TodoList
andSettings
states. We can then create two reducers for each of these states.let todoReducer = BlockReducer(state: TodoList.self) { state, action in guard let newState = state as? Int else { return state } if action is SomeAction { return newState + 1 } return newState } let settingsReducer = BlockReducer(state: Settings.self) { state, action in guard let newState = state as? Int else { return state } if action is SomeAction { return newState + 1 } return newState }
We can then combine these 2 reducers with:
let store = Suas.createStore( reducer: todoReducer + settingsReducer )
todoReducer
will handle theTodoList
key of state andsettingsReducer
will handle theSettings
portoon of the full Store stateCombine reducers with custom state key
If a store has a state with two keys
key1
andkey2
. We can register a reducer for each one of these keys.We create two reducers with
stateKey
s ofkey1
andkey2
let myReducer1 = BlockReducer(state: 1, key: "key1") { state, action in guard let newState = state as? Int else { return state } if action is SomeAction { return newState + 1 } return newState } let myReducer2 = BlockReducer(state: 1, key: "key2") { state, action in guard let newState = state as? Int else { return state } if action is SomeAction { return newState + 1 } return newState }
We can then combine these 2 reducers with:
let store = Suas.createStore( reducer: myReducer1 + myReducer2 )
See moremyReducer1
will handle thekey1
key of state andmyReducer2
will handle thekey2
key of stateDeclaration
Swift
public final class CombinedReducer: Reducer
-
Store that contains the application state, the reducer logic, the middleware and the listeners
The store contains four components:
- state: represents the application state. This state is partitioned into state keys. Each key can represents a full application screen, flow, or view controller.
- reducer represents the logic to update the state. A reducer mainly provides a function that updates the state for a paticular action.
- middleware: an object (or list of objects) that intercept an action and can enrich or alter it before finally dispatching it to the reducer.
- listener: a function that gets called when a state is changed.
Declaration
Swift
final public class Store
-
Create a middleware inline with a block.
Example
See morelet middleware = BlockMiddleware { action, getState, dispatch, next in // Read the state before any reducer changes it print("The old state is \(getState())") // Print the action print("The action is \(action)") // Continue the dispatching process..until the reducer reduces the action // Not calling `next` will prevent the action from reaching the reducer next?(action) // Read the state after any reducer changes it print("The new state is \(api?.state)") }
Declaration
Swift
public final class BlockMiddleware: Middleware