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-latch

v0.2.1

Published

Redux library for controlling action creator execution

Downloads

22

Readme

redux-latch

A Redux library for restricting and synchronising action-creator execution

build status

Overview

A common pattern in Redux is using flags to signal that an asynchronous action-creator is currently executing or has been executed, the flags are used to prevent action-creators running simultaneously or to prevent them from running multiple times. redux-latch provides a set of higher-order actions which enable you to handle these situations more declaratively.

As an example say you have an action-creator which you want to only run once after the user has logged in, it might look something like this:

function loadUserPermissions(username) {
   return (dispatch, getState) => {
      const userStore = getState().userStore;
       
      if (userStore.permissionsLoading || userStore.permissions)
         return;
         
      dispatch({ type: USER_PERMISSIONS_LOADING, payload: true });
      
      return fetchUserPermissions()
         .then(permissions => {
            dispatch({ type: SET_USER_PERMISSIONS, payload: permissions });
            dispatch({ type: USER_PERMISSIONS_LOADING, payload: false });
         })
   }
}

Using the createEnsure higher-order action provided by redux-latch you can remove the ad-hoc flags from the store, and simplify the action-creator so that it simply performs the fetch:

function loadUserPermissions(username) {
   return (dispatch, getState) => {      
      api.fetchUserPermissions()
         .then(permissions => {
            dispatch({ type: SET_USER_PERMISSIONS, payload: permissions });
         })
   }
}

Calling createEnsure on this action-creator will return a new action-creator which wraps the original and ensures that it is only ever executed once.

import { createEnsure } from 'redux-latch';
const ensureUserPermissions = createEnsure(loadUserPermissions);    

Then in your code, just use the ensureUserPermissions action-creator to trigger the fetch instead. redux-latch maintains all of its state inside the redux store, so isomorphism and time-travelling are both handled.

Usage

You need to initialise the latch reducer where redux-latch will hold its state, by default redux-latch will expect this to be located at state.latches, however this can be overridden.

import {latchReducer} from 'redux-latch';

const combined = combineReducers({
   latches: latchReducer,
   products: productReducer,
   users: userReducer
});

redux-latch also depends on the redux-thunk middleware being present.

Higher-Order Actions

createLatch

Will enhance the action-creator so that it is never called more than N times. You can also specify the number of concurrent exections allowed. The function returns a Latch object with an execute method to trigger the action-creator and an invalidate method to reset the latch.

function createLatch(actionCreator: ActionCreator, options?: LatchOptions): Latch;

interface Latch {
	execute: ActionCreator;
	invalidate: (...args) => void;
}

interface LatchOptions {
   displayName?: string;
   stateSelector?: (state: any) => LatchState;
   keySelector?: (...args) => any[];
   max?: number;
   maxConcurrent?: number;
}

createEnsure

Just returns the execute methods of the latch. Useful if you never want to call invalidate.