Giter Site home page Giter Site logo

redux-saga-retry's Introduction

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) => number โ–ถ exponentialGrowth

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: boolean โ–ถ false

Dispatch an action when the retry happens.

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

retries: number โ–ถ 3

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

redux-saga-retry's People

Contributors

carlosasj avatar gilbarbara avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.