extraduce
Function to create reducer enhancers (higher order reducers) that allow to add pre- and post-processing, handling arrays of actions, filtering and transforming of actions and state.
Features
- Batch processing of actions by creating special
extraAction
. - Skip processing of actions that do not conform to some conditions (
filterAction
andskipFalsyAction
options). - Prepare actions for processing (
prepareAction
andtransformAction
options). - Run some code and modify state before and/or after source reducer (
prereduce
andpostreduce
options). - Pass additional arguments to reducer besides state and action (
reducerExtraArgs
option). - Monitor and save all processed actions and outcome states for debug purposes.
- Call a function at the start and the end of each processing step (
onStepStart
andonStepEnd
options).
Table of contents
↑
InstallationNode
npm install extraduce
AMD, <script>
Use dist/extraduce.umd.development.js
or dist/extraduce.umd.production.min.js
(minified version).
↑
UsageECMAScript 6+
;
Node
const extraduce = ;
AMD
;
<script>
↑
Examplesconst initState = value: 0; { const payload = action; } const processStep = extraduceProcessStep;const enhancedReducer = extraduce; const store = Redux; store; console; // state: { value: -4 }
See additional examples in tests.
↑
APIextraduce(sourceReducer, settings?): Function
Wraps passed reducer to add pre- and post-processing.
Arguments:
sourceReducer: Function
- Source reducer that should be wrapped.settings: object
- Optional settings to customize wrapping operation and features of created reducer.settings.data: any
(optional) - Any supplementary data that should be available in processing.settings.filterAction: Function
(optional) - Function that should be called to test whether current action is valid and should be passed for reducing.settings.historySize: number
(optional) - Size of list of fulfilled operations (processed actions).0
by default.settings.onStepEnd: Function
(optional) - Function that should be called at the end of each processing step.settings.onStepStart: Function
(optional) - Function that should be called at the start of each processing step.settings.postreduce: Function
(optional) - Function that should be called after source (wrapped) reducer.settings.postreduceValueIsState: boolean
(optional) - Whether result ofpostreduce
function should be used as new state in further processing.settings.prepareAction: Function
(optional) - Function that should be called to prepare current action for further processing before filtering.settings.prereduce: Function
(optional) - Function that should be called before source (wrapped) reducer.settings.prereduceValueIsState: boolean
(optional) - Whether result ofprereduce
function should be used as new state in further processing.settings.processActionArray: boolean
(optional) - Whether an array that is passed as action should be treated as list of actions that should be processed separately.true
by default.settings.reducerExtraArgs: any | any[]
(optional) - Supplementary arguments that should be passed into source (wrapped) reducer besides state and action. Or a function that returns such arguments.settings.skipFalsyAction: boolean
(optional) - Whether processing of an action that has a falsy value should be skipped.true
by default.settings.transformAction: Function
(optional) - Function that should be called to transform current valid action after filtering.
Returns new created reducer that can be used instead of source reducer.
getExtraActionType(): string
Returns type of action that is used to wrap several actions to be processed.
setExtraActionType(actionType: string): void
Changes type of action that is used to wrap several actions to be processed.
extraAction(...actionList: any[]): object
Creates action that is used to wrap several actions to be processed.
isExtraAction(value: any): boolean
Checks whether passed value represents an action that wraps several actions to be processed.
See docs
for details.
↑
Related projects↑
ContributingIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
↑
LicenseCopyright (c) 2020 Denis Sikuler
Licensed under the MIT license.