Kind: global namespace
- reducer-patterns :
object
- .async :
object
- .ConditionalReducer(condition, onTrue, onFalse) ⇒
function
- .InitState(initializer) ⇒
taskFunction
- .PostprocessState(reducer, postprocessor) ⇒
function
- .SeriesReducer(...reducers) ⇒
function
- .SwitchReducer(...reducers) ⇒
function
- .ConditionalReducer(condition, onTrue, onFalse) ⇒
- .ConditionalReducer(condition, onTrue, onFalse) ⇒
function
- .InitState(initializer) ⇒
taskFunction
- .PostprocessState(reducer, postprocessor) ⇒
function
- .SeriesReducer(...reducers) ⇒
function
- .SwitchReducer(...reducers) ⇒
function
- .async :
Kind: static namespace of reducer-patterns
- .async :
object
- .ConditionalReducer(condition, onTrue, onFalse) ⇒
function
- .InitState(initializer) ⇒
taskFunction
- .PostprocessState(reducer, postprocessor) ⇒
function
- .SeriesReducer(...reducers) ⇒
function
- .SwitchReducer(...reducers) ⇒
function
- .ConditionalReducer(condition, onTrue, onFalse) ⇒
let ConditionalReducer = require('reducer-patterns/async/ConditionalReducer');
let state = {
a: 1,
b: 2,
};
let action1 = {
type: 'a',
payload: 2,
};
let action2 = {
type: 'b',
payload: 2,
};
let reducer = ConditionalReducer(
(state, action) => action.type === 'a',
(state, action) => ({
...state,
a: action.payload,
})
);
// prints action1 result { a: 2, b: 2 }
// action1 triggers a state update, since it has the correct type
console.log(
'action1 result',
reducer(state, action1)
);
// prints action2 result { a: 1, b: 2 }
// action2 is ignored, since it has the wrong type
console.log(
'action2 result',
reducer(state, action2)
);
ConditionalReducer
Kind: static method of async
Params
- condition
function
- a condition task. - onTrue
function
- reducer to run if true. - onFalse
function
- reducer to run if false.
InitState accepts an initializer function/value, and builds an initial state if none exists
Kind: static method of async
Params
- initializer
function
- a condition task.
PostprocessState
Kind: static method of async
Params
- reducer
function
- the reducer to wrap around. - postprocessor
function
- the postprocessor function to call after the reducer.
SeriesReducer accepts any number of reducers, and builds a new reducer that chains them together in series
Kind: static method of async
Params
- ...reducers
function
- all reducers.
Kind: static method of async
Params
- ...reducers
function
- all reducers.
let ConditionalReducer = require('reducer-patterns/ConditionalReducer');
let state = {
a: 1,
b: 2,
};
let action1 = {
type: 'a',
payload: 2,
};
let action2 = {
type: 'b',
payload: 2,
};
let reducer = ConditionalReducer(
(state, action) => action.type === 'a',
(state, action) => ({
...state,
a: action.payload,
})
);
// prints action1 result { a: 2, b: 2 }
// action1 triggers a state update, since it has the correct type
console.log(
'action1 result',
reducer(state, action1)
);
// prints action2 result { a: 1, b: 2 }
// action2 is ignored, since it has the wrong type
console.log(
'action2 result',
reducer(state, action2)
);
ConditionalReducer
Kind: static method of reducer-patterns
Params
- condition
function
- a condition task. - onTrue
function
- reducer to run if true. - onFalse
function
- reducer to run if false.
InitState accepts an initializer function/value, and builds an initial state if none exists
Kind: static method of reducer-patterns
Params
- initializer
function
- a condition task.
PostprocessState
Kind: static method of reducer-patterns
Params
- reducer
function
- the reducer to wrap around. - postprocessor
function
- the postprocessor function to call after the reducer.
SeriesReducer accepts any number of reducers, and builds a new reducer that chains them together in series
Kind: static method of reducer-patterns
Params
- ...reducers
function
- all reducers.
Kind: static method of reducer-patterns
Params
- ...reducers
function
- all reducers.