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-saga-retry

v0.1.0

Published

Retry your redux sagas with zero-config

Downloads

2,534

Readme

redux-saga-retry

NPM Package Build Status Maintainability Test Coverage

Retry failed async sagas automatically!

// before
takeLatest('GET_COFFEE', getCoffee)

// after
takeLatest('GET_COFFEE', retry(getCoffee))

Just wrap your saga with the retry machine and It will run it again when a failure action happens.

Highlights

  • 🏖 Easy to use: Simply wrap your saga
  • 🔼 Incremental adoption: Just add to the sagas that need it
  • 🛠 Flexible and granular: Personalize the options to fit your needs
  • 🟦 Typescript ready: Nicely typed (but accepting improvements too)

Demo: https://utd8b.csb.app/

Install

$ npm install redux-saga-retry

Usage

Import the module and wrap your saga:

import { retry } from 'redux-saga-retry';

function* getCoffee(action) { ... }

export default function* root() {
  yield all([
    takeLatest('GET_COFFEE', retry(getCoffee)),
  ]);
}

If your saga yields an action with the type ending with _FAILURE the machine will hold it and run the saga again.

function* getCoffee(action) {
  try {
    yield call(...);

    yield put({ type: 'GET_COFFEE_SUCCESS' });
  } catch (error) {
    // This will trigger the retry machine
		yield put({ type: 'GET_COFFEE_FAILURE' });
  }
}

After the request resolves successfully or the retries are exhausted your saga will run through the end.

API

retry(saga, [options])

Wraps a saga function and returns a retryable version of it.

The options argument is an object that you can tune how the retry works.

Options

backoff (attempt: number) => numberexponentialGrowth

A function to sparse the retries, where attempt is the number of the current attempt (starting on 0) and returns how long to wait before trying again, in milliseconds.

We provide some basic functions (linearGrowth and exponentialGrowth) but you are free to implement your own.

condition: RegExp | (v: any) => boolean/_FAILURE$/ The condition to tell if the retry should happen.

As a RegExp, it will listen for redux-saga's action that matches the regex.
As a function, you are responsible to decide if it should retry the execution.

debug: booleanfalse

Dispatch an action when the retry happens.

{
  type: '@@REDUX-SAGA-RETRY',
  payload: {
  	action: 'NOTES_REQUEST',
  	attempt: 1,
  },
}

retries: number3

The maximum number of retries the saga will run. Doesn't include the original run.
You can override this value for each run by setting a meta.retries on the action that you dispatch to the store:

dispatch({
  type: 'GET_COFFEE',
  meta: { retries: 42 }, // that's a lot of retries
});

Examples

Custom options

  • matches any action ending on _FAIL, _FAILED or _FAILURE
  • retry up to 4 times
  • sparse the tries by 400ms, 800ms, 1200ms...
import { retry, linearGrowth } from 'redux-saga-retry';

...

retry(getCoffee, {
  condition: /_FAIL(ED|URE)?$/,
  backoff: linearGrowth,
  retries: 4,
})

Advanced condition and backoff options

  • custom condition: checks for PUT effects in which the action type ends on _FAILURE but not if the payload.status is 401 (maybe you want to handle the authorization before try again).
  • sparse the tries by an initial delay of 1400ms followed by 400ms, 800ms, 1200ms...
import { retry } from 'redux-saga-retry';

function conditionFn(value) {
  if (value?.type !== 'PUT') {
    return false;  // not interested in other effects
  }

  const { type, payload } = value.payload.action;

  return type.endsWith('_FAILURE') && payload.status !== 401;
}

const backoffFn = i => i === 0 ? 1400 : 400 * i;

...

retry(getCoffee, {
  condition: conditionFn,
  backoff: backoffFn,
})

License

MIT