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

@weavedev/redux-async

v2.0.0

Published

Async function wrapper for redux powered by @reduxjs/toolkit

Downloads

122

Readme

redux-async

Build Status - Travis CI Test Coverage - Code Climate MIT NPM

Default reducer with reset functionality for Redux Toolkit's createAsyncThunk

Install

npm i @weavedev/redux-async

API documentation

We generate API documentation with TypeDoc.

API Documentation

Creating

In this example we create a reducer from a thunk. The async function inside the thunk will trigger its callback after 3 seconds.

import { createAsyncThunk } from '@reduxjs/toolkit';
import { createAsyncReducer } from '@weavedev/redux-async';

// First, create the thunk with @reduxjs/toolkit's createAsyncThunk
//     Docs, see: https://redux-toolkit.js.org/api/createAsyncThunk
export const runSayHello = createAsyncThunk(
    'say/hello',
    async (name: string): Promise<string> => {
        // This promise waits 3 seconds and then returns
        return new Promise((resolve: ((value: string) => void)): void => {
            setTimeout(() => {
                resolve(`Hey, ${name}!`);
            }, 3000);
        });
    },
);

// Then create the reducer from the thunk
export const sayHello = createAsyncReducer(runSayHello);

Triggering

You can run your async function by dispatching the thunk. If your Promise expects a parameter, you can pass it to the thunk.

import { runSayHello } from './runSayHello';
import { store } from './store';

// Dispatch the thunk on your store
store.dispatch(runSayHello('Dave'));

For more information, see Redux Toolkit: createAsyncThunk - Overview.

Resetting

If you pass an action as the second argument to createAsyncReducer the generated reducer will reset itself to its initial state when this action is dispatched.

If this is done while the thunk is 'pending', the corresponding 'rejected' or 'fulfilled' will be ignored (the thunk will still fire the action, but the state will not update).

import { createAction, createAsyncThunk } from '@reduxjs/toolkit';
import { createAsyncReducer } from '@weavedev/redux-async';

// First, create the thunk with @reduxjs/toolkit's createAsyncThunk
//     Docs, see: https://redux-toolkit.js.org/api/createAsyncThunk
export const runSayHello = createAsyncThunk(
    'say/hello',
    async (name: string): Promise<string> => {
        // This promise waits 3 seconds and then returns
        return new Promise((resolve: ((value: string) => void)): void => {
            setTimeout(() => {
                resolve(`Hey, ${name}!`);
            }, 3000);
        });
    },
);

// Create a reset action
//     We suggest adding `/reset` or `/initial` after your thunk action string
export const resetSayHello = createAction('say/hello/reset');

// Then create the reducer from the thunk and the reset action
export const sayHello = createAsyncReducer(runSayHello, resetSayHello);

To reset the state on your reducer, simply dispatch the reset action

store.dispatch(resetSayHello());

State

The state output contains the following fields

error?: any

The reducer will contain the rejected or thrown value in the error field if the Promise has rejected / thrown. This value will be cleared if you dispatch the thunk again (or on reset).

result?: ThunkResult

The reducer will contain the returned value in the result field if the Promise has resolved. This value will be cleared if you dispatch the thunk again (or on reset).

meta

The meta field contains a copy of the data in the meta field in Thunk's Promise Lifecycle Actions.

For example; when pending the meta field contains the following values:

requestId: string;
arg: ThunkArg;

The meta field also contains the requestStatus with the current state of the thunk.

requestStatus: 'initial' | 'pending' | 'fulfilled' | 'rejected';

meta.requestStatus: 'initial'

This is the requestStatus in the reducer, or the state after the reset action has been dispatched.

meta.requestStatus: 'pending'

This is the requestStatus in reducer while we are waiting for the Promise to resolve (or throw).

You can implement a busy indicator by checking meta.requestStatus === 'pending'.

meta.requestStatus: 'fulfilled'

This is the requestStatus in the reducer after the Promise has resolved. The reducer will also contain the returned value in the result field.

meta.requestStatus: 'rejected'

This is the requestStatus in the reducer after the Promise has rejected / thrown. The reducer will also contain the rejected or thrown value in the error field.

License

MIT

Made by Paul Gerarts and Weave