redux-detector
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Redux Detector

Redux enhancer for pure detection of state changes 👀

npm version build Status coverage Status tested with jest code style: prettier commitizen friendly

Table of Contents 📋

Installation 📦

Redux Detector requires Redux 3.1.0 or later.

npm install --save redux-detector

This assumes that you’re using npm package manager with a module bundler like Webpack to consume ES6 or CommonJS modules.

To enable Redux Detector, use createDetectorEnhancer:

import { createStore } from "redux";
import { createDetectorEnhancer } from "redux-detector";
import { rootReducer } from "./store/reducer/rootReducer";
import { rootDetector } from "./store/detector/rootDetector";
 
const store = createStore(rootReducer, createDetectorEnhancer(rootDetector));

Concept 💡

Redux Detector enhancer allows you to detect state changes in the Redux.

The Detector is a pure function which accepts previous and next state and returns something for given states transition.

type Detector<TState, TResult> = (
  prevStateTState | undefined,
  nextStateTState | undefined
) => TResult;

The Actions Detector is a Detector which returns action, list of actions or nothing. Returned actions are automatically dispatched by the enhancer.

import { Detector } from "redux-detector";
import { Action } from "redux";
 
type ActionsDetector<TState, TAction extends Action> = Detector<
  TState,
  TAction | TAction[] | void
>;

Another type of the detector is the Condition Detector which returns boolean values.

import { Detector } from "redux-detector";
 
type ConditionDetector<TState> = Detector<TState, boolean>;

These two types of detectors have different responsibility:

  • Condition Detectors describes a condition that we want to detect
  • Actions Detectors describes which action we want to dispatch

Thanks to its functional nature and purity, detectors are easy to test. They don't break Single Source of Truth principle as the input is only previous and next state.

Basics 👈

Let's start simply - implement a condition detector that checks if number of login attempts exceeded 3.

export const exceededLoginAttemptsLimit = (prevState, nextState) =>
  prevState.attempts <= 3 && nextState.attempts > 3;

We can make above example more generic - prevState.attempts <= 3 is the same as !(prevState.attempts > 3). That means that we check if some condition is not truthy for the previous state but is truthy for the next state. This kind of transition can be handled by the changedToTruthy function.

import { changedToTruthy } from "redux-detector";
 
export const exceededLoginAttemptsLimit = changedToTruthy(
  state => state.attempts > 3
);

Redux Detector library provides other useful functions to model condition detectors - please check the API documentation to learn more.

The next step is to use an action detector to dispatch an action when the limit became exceeded. To do so, we will use composeIf function.

import { composeIf, changedToTruthy } from "redux-detector";
import { blockUser } from "../action/userAction";
 
const blockUserDetector = composeIf(
  changedToTruthy(state => state.attempts > 3),
  () => blockUser()
);

The createDetectorEnhancer function accepts only one detector, so we have to compose all detectors to the one rootDetector.

import { composeDetectors } from "redux-detector";
import { blockUserDetector } from "./userDetector";
// other detectors...
import { companyDetector } from "./companyDetector";
 
export const rootDetector = composeDetectors(
  blockUserDetector,
  companyDetector
);

And that's all - redux-detector will dispatch blockUser() when login attempts exceeded 3 🎉

API reference 📖

For more detailed documentation, please check API reference.

Code splitting ✂️

Redux Detector provides replaceDetector method on DetectableStore interface (store created by Redux Detector). It's similar to replaceReducer - it changes detector and dispatches { type: '@@detector/INIT' }.

Typings 📐

If you are using TypeScript, typings are provided in the npm package. This library doesn't provide Flow typings.

License

MIT

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.0601latest
1.0.0-rc.80beta

Version History

VersionDownloads (Last 7 Days)Published
1.0.0601
1.0.0-rc.80
1.0.0-rc.70
1.0.0-rc.60
1.0.0-rc.50
1.0.0-rc.40
1.0.0-rc.30
1.0.0-rc.20
1.0.0-rc.10
0.8.012
0.7.00
0.6.10
0.6.0-10
0.6.0-00
0.5.30
0.5.20
0.5.10
0.5.00

Package Sidebar

Install

npm i redux-detector

Weekly Downloads

613

Version

1.0.0

License

MIT

Unpacked Size

43.3 kB

Total Files

30

Last publish

Collaborators

  • piotr-oles