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-cancelable-request

v1.0.0

Published

This package will help you to understand how to create a system in your redux application to handle a canceled request. This repository is open to contribution. Do not hesitate to contact me.

Downloads

26

Readme

Redux Cancelable Requests

This package will help you to understand how to create a system in your redux application to handle a canceled request. This repository is open to contribution. Do not hesitate to contact me.

Purpose

This package will help but it need to be implemented carrefully. There is many ways to cancel or ignore a request. The purpose here is to ignore the requests dispatched with a specific state and process the action canceled differently. It's like a toolbox wich can be implemented differently depending of your project.

Now each time you will dispatch an action to canceled, the future action success or failed received, will be catched and will update the action with a new property canceled.

How to use it ?

Each SUCCESS or FAILURE actions dispatched will now receive a new argument to add manually in the app. Each actions need now an id to identify here on your redux management. This identifier is used to cancel your request in the process. It can be a string, an id, or anything. The generateId tool is here to helps you to give you a speed hash of your id. You can use it with:

import { generateId } from 'redux-cancelable-requests';

const idRequest = generateId(typeInit);
dispatch({
  type: typeSuccess,
  payload: responseJson,
  idRequest,
});

After that, you need to configure your store to handle the reducer wich will permit to save the requests to cancel.

import { _canceledRequest } from 'redux-cancelable-requests';
// Then, add _canceledRequest to your combine reducers used by your store

Your reducer saved, you can dispatch manually the same id in another function to lauch your cancel request.

import { cancelRequestDispatch } from 'redux-cancelable-requests';

// Example:
const idToCancel = "FETCH_ALL_FORMS_INIT";
// The idToCancel need to be the same that the original id provided by
// your SUCCESS or FAILURE action.
cancelRequestDispatch(dispatch, idToCancel);
// After that dispatch, we save the idToCancel to the new reducer.

You can now add the middleware wich will evaluate the future requests and update the next idRequest with the same id launched.

import { cancelRequestMiddleware } from 'redux-cancelable-requests';

// Example with other middlewares
export const store = createStore(
  rootReducer,
  {},
  composeWithDevTools(applyMiddleware(
    networkMiddleware,
    cancelRequestMiddleware,
    thunk
  )),
);

Now each time you will dispatch an action to canceled, the future action success or failed received, will be catched and will update the action with a new property canceled.

// FUTURE ACTION NOW CANCELED
{
  type: 'FETCH_ALL_FORMS_SUCCESS',
  payload: {...},
  canceled: true,
}

// Now in your reducer you can easily access to this property to
// handle correctly the state wanted.
case 'FETCH_ALL_FORMS_SUCCESS':
  if(action.canceled){
    // do something
  }

After the success or the failure of your action, the action is automatically removed in the stack of requests canceled.