Conditional Reduction

There are times where you may wish to conditionally apply a reduction.

There can be many reasons for this. Take a simple example where you wish to bypass a reduction process upon determination that an action will not impact an entire branch of your state tree. In this example the conditional aspect is purely an optimization.

This can be accomplished through the conditionalReducer utility.

import * as Redux         from 'redux';
import * as AstxReduxUtil from 'astx-redux-util';
import x                  from '../appReducer/x';
import y                  from '../appReducer/y';

const reduceWidget = 
    // conditionally apply when action.type begins with 'widget.edit'
    (curState, action, originalReducerState) => action.type.startsWith('widget.edit'),

export default function widget(widget={}, action) {
  return reduceWidget(widget, action);

Here we only invoke the supplied reducer (Redux.combineReducers) when the action.type begins with 'widget.edit'. In our contrived example, our action types are organized with a federated namespace, so it is easy to isolate which actions will impact various parts of our state.

Note: Because we did not supply "elseReducerFn" to conditionalReducer (the third parameter), the default identity function is used for the else condition, in essence retaining the same state for a falsy directive.

More to Come: This example is merely intended to introduce you to the concept of conditional reduction. It is somewhat "contrived", allowing us to discuss the topic in isolation. In reality, this example may be inappropriate because the optimization is minimal, and it tends to make the code more brittle. Keep in mind, however, there are more legitimate reasons to apply conditional reduction ... we will see this in subsequent discussions.