npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-detector

v1.0.0

Published

Redux enhancer for pure detection of state changes.

Downloads

2,934

Readme

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> = (
  prevState: TState | undefined,
  nextState: TState | 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