Functions
The following functions are available globally.
-
Combines two reducers. The combined reducer calls each of its internal reducer for a each paricular state key.
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 func +<R1: Reducer, R2: Reducer>(lhs: R1, rhs: R2) -> CombinedReducer
-
Combines two middlewares. The combined middleware creates a chain of middleware. When calling next on the first middleware it progresses to the next one. The final middlware’s next function calls the reducer dispatch will causes a state change.
Example
Combining two logging middlewares
let middleware1 = BlockMiddleware { action, getState, dispatch, next in // do some middleware stuff } let middleware2 = BlockMiddleware { action, getState, dispatch, next in // do some middleware stuff }
We can then combine these 2 middlewares with
+
operator as:let store = Suas.createStore( reducer: someReducer, middleware: middleware1 + middleware2 )
Declaration
Swift
public func +(lhs: Middleware, rhs: Middleware) -> Middleware