GithubHelp home page GithubHelp logo

redux-saga-restart's Introduction

react-redux-restart

CircleCI Maintainability Test Coverage

Don't let your sagas die just like that. Catch the errors, allow them to try again.

Example

import keepAlive from 'redux-saga-restart';
import myUndyingSaga from './myUndyingSaga';
import myMortalSaga from './myMortalSaga';

function* onEachError(next, error, saga) {
  yield put({ type: 'SAGA_ERROR' });
}

function* onFail(error, saga) {
  console.warn(`Your saga ${sagaName} will not be restarted anymore`);
  yield put({ type: 'FATAL_ERROR' });
}

const sagas = [
  myMortalSaga,
  keepAlive(myUndyingSaga, {
    onEachError,
    onFail,
  })
]

export default function* rootSaga() {
  yield all(sagas.map(saga => fork(saga)));
}

API

keepAlive(saga, options)

Pass any saga and options. When an error kills it, the keepAlive wrapper will restart it again until it runs out of attempts. If no error handler is passed, sagas will trigger warnings to the console.

{
  defaultBehavior = RESTART,
  disableWarnings = false,
  maxAttempts = 3,
  onEachError,
  onFail,
}

onFail(error, sagaName, attempts)

Execute any action when saga fails. It can be either a function or another saga.

import { put } from 'redux-saga/effects';
import keepAlive from 'redux-saga-restart';

function* onFail(error) {
  yield put({ type: 'FATAL_ERROR', error });
}

keepAlive(saga, {
  onFail,
});

onFail(error, sagaName, attempts)

Execute any action when saga is killed by error and decide if it is restarted or not. It can be either a function or another saga.

import { put } from 'redux-saga/effects';
import keepAlive, { FAIL, RESTART } from 'redux-saga-restart';

function* logEachError(next, error, sagaName) {
  logError(error);
}

function* killOnEachError(next, error) {
  yield put({ type: 'FATAL_ERROR', error });
  next(FAIL);
}

keepAlive(saga, {
  onEachError: killOnEachError,
  /* Including defaultBehavior despite this
     is its default value just to be obvious */
  defaultBehavior: RESTART,
});

maxAttempts

Configure maximum number of restart attempts.

import keepAlive from 'redux-saga-restart';
keepAlive(saga, {
  maxAttempts: 100,
});

Whitelist instead of blacklist

Library provides constants that shape the default behavior of the keepAlive function.

  • RESTART (the default) - Saga will be restarted unless told othwerwise
  • FAIL - Saga will not be restarted unless told otherwise.
import keepAlive from 'redux-saga-restart';
keepAlive(saga, {
  defaultBehavior: FAIL,
});

redux-saga-restart's People

Contributors

just-paja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nachii oojikoo

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.