Git Product home page Git Product logo

tarsinzer / redux-pending-effects Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 2.0 5.12 MB

๐Ÿฆ‹ A redux toolkit that tracks your asynchronous redux actions (effects) and informs about the intermediate state using the selector function

JavaScript 7.18% TypeScript 81.72% HTML 3.18% CSS 0.62% SCSS 7.29%
redux pending middleware promise redux-promise redux-pending redux-toolkit redux-saga redux-thunk redux-promise-middleware

redux-pending-effects's Introduction

Redux Pending Effects

๐Ÿฆ‹ A redux toolkit that tracks your asynchronous redux actions (effects) and informs about the pending state using the selector function

List of supported libraries that process redux effects:

It's worth mention that redux-pending-effects allows you to code simultaneously with all libraries above.


Problem it solves

Have you ever been in a situation where you need to add a global loader/spinner to any side effect that your application is processing? Perhaps you are using Redux and some third-party library for asynchronous processing, for example, redux-saga / promise middleware? Great, then it should be interesting to you.

Why not handle the pending state manually for each action?

  • It is very unpleasant to create separately for this state and add start and end actions for these actions to each request.
  • This is an open place to make mistakes because it's very easy to forget to add or remove these actions.
  • It needs to be supported and somehow live with it.

Well, redux-pending-effects does this from scratch:

  • tracks your asynchronous code
  • collects them in a bunch
  • efficiently calculates active pending effects
  • provides a selector for information about the current state of application loading
  • available for debugging in redux-devtools
  • independent of a particular asynchronous processing solution. Can be used simultaneously with redux-saga and redux-toolkit
  • replaces redux-thunk in the matters of side effects (not actions chaining) and redux-promise-middleware (essentially uses it out of the box)

Quickstart

Installation

npm install redux-pending-effects

Extend reducers

redux-pending-effects provides its own state for storing active effects (pending promise phase).

import { combineReducers } from 'redux';
import { includePendingReducer } from 'redux-pending-effects';

import { planetReducer as planet } from './planetReducer';
import { universeReducer as universe } from './universeReducer';

const appReducers = {
  planet,
  universe
};
const reducersWithPending = includePendingReducer(appReducers);
export const rootReducer = combineReducers(reducersWithPending);

Configuration

The package provides a single entry point for set up via configurePendingEffects

configurePendingEffects accepts a single configuration object parameter, with the following options:

  • promise: boolean (default false) - enable/disable tracking of asynchronous effects that you pass a promise to the payload. Yes, if the option enabled, you can pass promise to the payload, that is the way redux-promise-middleware does. For details, you can go to read the documentation of redux-promise-middleware about how this works.
  • toolkit: boolean (default false) - enable/disable tracking of asynchronous effects produced by redux-toolkit
  • saga: boolean (default false) - enable/disable tracking of asynchronous effects produced by redux-saga
  • ignoredActionTypes: string[] (default []) - list of action types to not track (do not react on actions with these types)

configurePendingEffects returns an object with two properties:

  1. middlewares - an array of defined redux middlewares
  2. sagaOptions - options for createSagaMiddleware in case you intend to use redux-saga

Example

Let's show an example with all options enabled.

import { configurePendingEffects } from 'redux-pending-effects';
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit';
import createSagaMiddleware from '@redux-saga/core';

import { rootReducer as reducer } from './root.reducer';
import { rootSaga } from './root.saga';

const { middlewares, sagaOptions } = configurePendingEffects({
  promise: true,
  toolkit: true,
  saga: true,
  ignoredActionTypes: ['IGNORED_ACTION_1', 'IGNORED_ACTION_2']
});
const sagaMiddleware = createSagaMiddleware(sagaOptions);
const toolkitMiddlewares = getDefaultMiddleware();
const middleware = [...middlewares, ...toolkitMiddlewares, sagaMiddleware];

export const store = configureStore({ reducer, middleware });

sagaMiddleware.run(rootSaga);

Selector

Just a regular usage of redux selectors

import React from 'react';
import { useSelector } from 'react-redux';
import { selectIsPending } from 'redux-pending-effects';

import { YourApplication } from './YourApplication';
import { AppLoader } from './App.loader';

export const App = () => {
  const isPending = useSelector(selectIsPending);

  return isPending ? <AppLoader /> : <YourApplication />;
};

Notes

At the moment, this library completely replaces redux-promise-middleware. In the plans, through the collaboration, expand the API of redux-promise-middleware to reuse their internal API.


Contributing

Contributions are welcome. For significant changes, please open an issue first to discuss what you would like to change.

If you made a PR, make sure to update tests as appropriate and keep the examples consistent.


Contact

Please reach me out if you have any questions or comments.


References

I find these packages useful and similar to this one. So, it's important to mention them here.

The main reason why I didn't choose them: they do one thing, and it's impossible to add something second to them.


License

This project is MIT licensed.


redux-pending-effects's People

Contributors

dengoloborodko avatar dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar tarsinzer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

redux-pending-effects's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency redux-devtools-extension with @redux-devtools/extension 3.0.0
  • fix(deps): update dependency redux-thunk to v2.4.2
  • fix(deps): update dependency styled-components to v5.3.11
  • chore(deps): update dependency eslint to v8.57.0
  • chore(deps): update dependency eslint-config-prettier to v8.10.0
  • chore(deps): update dependency eslint-plugin-compat to v4.2.0
  • chore(deps): update dependency eslint-plugin-import to v2.29.1
  • chore(deps): update dependency eslint-plugin-jsx-a11y to v6.8.0
  • chore(deps): update dependency eslint-plugin-prettier to v4.2.1
  • chore(deps): update dependency eslint-plugin-react to v7.34.1
  • chore(deps): update dependency eslint-plugin-sonarjs to v0.25.1
  • chore(deps): update dependency monoreact to v0.35.0
  • chore(deps): update dependency prettier to v2.8.8
  • chore(deps): update dependency redux-saga to v1.3.0
  • chore(deps): update dependency tslib to v2.6.2
  • chore(deps): update dependency typescript to v4.9.5
  • chore(deps): update dependency webpack to v5.91.0
  • chore(deps): update typescript-eslint monorepo to v5.62.0 (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • fix(deps): update dependency @reduxjs/toolkit to v1.9.7
  • fix(deps): update dependency grommet to v2.37.0
  • fix(deps): update dependency grommet-icons to v4.12.1
  • fix(deps): update dependency react-router-dom to v6.23.0
  • fix(deps): update dependency redux-promise-middleware to v6.2.0
  • fix(deps): update dependency redux-saga to v1.3.0
  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update dependency @types/jest to v29
  • chore(deps): update dependency @types/node to v20
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-config-prettier to v9
  • chore(deps): update dependency eslint-plugin-prettier to v5
  • chore(deps): update dependency eslint-plugin-sonarjs to v1
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency lint-staged to v15
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency redux to v5
  • chore(deps): update dependency typescript to v5
  • chore(deps): update typescript-eslint monorepo to v7 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • fix(deps): update dependency @reduxjs/toolkit to v2
  • fix(deps): update dependency node-sass to v9
  • fix(deps): update dependency react-redux to v9
  • fix(deps): update dependency redux-thunk to v3
  • fix(deps): update dependency styled-components to v6
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/default.yaml
  • actions/checkout v3
  • actions/setup-node v3
npm
example-app/package.json
  • @reduxjs/toolkit 1.8.6
  • grommet 2.23.0
  • grommet-icons 4.7.0
  • history 5.3.0
  • jest-fetch-mock 3.0.3
  • node-sass 7.0.3
  • react 17.0.2
  • react-dom 17.0.2
  • react-html-parser 2.0.2
  • react-redux 8.0.2
  • react-router-dom 6.3.0
  • react-scripts 5.0.1
  • redux 4.2.0
  • redux-promise-middleware 6.1.2
  • redux-saga 1.1.3
  • redux-saga-tester 1.0.874
  • redux-thunk 2.4.1
  • styled-components 5.3.5
  • @types/jest 27.5.2
  • @types/node 17.0.35
  • @types/react 17.0.45
  • @types/react-dom 17.0.17
  • @types/react-html-parser 2.0.2
  • @types/react-redux 7.1.24
  • @types/redux-mock-store 1.0.3
  • @typescript-eslint/parser 5.41.0
  • eslint-config-prettier 8.5.0
  • eslint-config-react-app 6.0.0
  • eslint-plugin-compat 4.0.2
  • eslint-plugin-prettier 4.0.0
  • eslint-plugin-sonarjs 0.13.0
  • majestic 1.8.1
  • prettier 2.6.2
  • redux-devtools-extension 2.13.9
  • redux-mock-store 1.5.4
  • tslib 2.4.0
  • typescript 4.6.4
  • yarn 1.22.19
lib/package.json
  • redux-saga ^1.1.3
  • redux ^4.1.0
package.json
  • @typescript-eslint/eslint-plugin 5.41.0
  • @typescript-eslint/parser 5.41.0
  • eslint 8.16.0
  • eslint-config-prettier 8.5.0
  • eslint-config-react-app 6.0.0
  • eslint-plugin-compat 4.0.2
  • eslint-plugin-flowtype 8.0.3
  • eslint-plugin-import 2.26.0
  • eslint-plugin-jsx-a11y 6.5.1
  • eslint-plugin-prettier 4.0.0
  • eslint-plugin-react 7.33.2
  • eslint-plugin-react-hooks 4.5.0
  • eslint-plugin-sonarjs 0.13.0
  • husky 4.3.8
  • lint-staged 12.5.0
  • monoreact 0.34.0
  • prettier 2.6.2
  • webpack 5.76.0
  • yarn 1.22.19

  • Check this box to trigger a request for Renovate to run again on this repository

Ignore API

We should provide the possibility to ignore specific action types to be tracked for the pending state.

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.