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-awaiter

v0.3.0

Published

A Redux middleware for giving opportunities to await and receive actions in anywhere

Downloads

21

Readme

Redux Awaiter

Redux Awaiter is a Redux middleware for giving opportunities to await and receive actions in anywhere.

It's tiny (gzip < 1kB, no dependencies).

build status Coverage Status npm version npm downloads

Motivation

Local state is fine.

Redux Awaiter is designed to help us manage local state conveniently.

It's inspired by Redux Saga, but the problems they solve are totally different.

Example

We can use Redux Awaiter and async/await to pause execution until an expected action has been dispatched.

class UserListView extends React.PureComponent {
    state = { loading: false };

    async componentDidMount() {
        const { actions: { fetchUserList } } = this.props;
        fetchUserList();
        this.setState(state => ({ ...state, loading: true }));
        // start loading, until RECEIVE_USER_LIST has been dispatched
        await take('RECEIVE_USER_LIST');
        // reducers may update `users` in redux store, stop loading
        this.setState(state => ({ ...state, loading: false }));
    }

    render() {
        const { users } = this.props; // `users` is mapped from redux store
        const { loading } = this.state;
        return (
            <Spin loading={loading}>
                <ul>
                    {users.map(({ id, name }) => <li key={id}>{name}</li>)}
                </ul>
            </Spin>
        );
    }
}

Installation

npm i redux-awaiter

Documentation

Type Definition

Redux Awaiter is written in TypeScript.

The internal type definition is based on flux standard action.

type ActionType = string;

interface BaseAction {
    type: ActionType;
}

interface Action<P, M = {}> extends BaseAction {
    payload: P;
    meta?: M;
    error?: true;
}

A pattern determines whether an action is matching.

type Pattern<P = {}, M = {}> = string | RegExp | ((action: Action<P, M>) => boolean);

object pattern is not supported, use a function instead.

API

createAwaiterMiddleware

import { createStore, applyMiddleware } from 'redux';
import { createAwaiterMiddleware } from 'redux-awaiter';

const awaiterMiddleware = createAwaiterMiddleware();

const store = createStore(rootReducer, applyMiddleware(
    // other middlewares (e.g. routerMiddleware)
    awaiterMiddleware,
));

take

const take: <P = {}, M = {}>(pattern: Pattern<P, M>) => Promise<Action<P, M>>;

take receives a pattern as its single argument, and returns a Promise which contains the first matching action.

const action = await take('RECEIVE_DATA'); // action.type should be RECEIVE_DATA

takeAllOf

const takeAllOf: <P = {}, M = {}>(patterns: Pattern<P, M>[]) => Promise<Action<P, M>[]>;

takeAllOf receives an array of patterns as its single argument, and returns a Promise which contains an array of actions corresponding to patterns.

Internally, takeAllOf(patterns) is the same with Promise.all(patterns.map(take)).

If you need to await and receive multiple actions in specific order, use multiple await take() instead.

const [{ payload: articles }, { payload: users }] = await takeAllOf(['RECEIVE_ARTICLES', 'RECEIVE_USERS']);

takeOneOf

const takeOneOf: <P = {}, M = {}>(patterns: Pattern<P, M>[]) => Promise<Action<P, M>>;

takeOneOf receives an array of patterns as its single argument, and returns a Promise which contains the first action matching with one of patterns.

Internally, takeOneOf(patterns) is the same with Promise.race(patterns.map(take)).

const { type } = await takeOneOf(['FETCH_USER_SUCCESS', 'FETCH_USER_FAILURE']);
if (type === 'FETCH_USER_SUCCESS') {
    // success
} else {
    // failure
}

You might not need takeOneOf.

const { type } = await take(/^FETCH_USER/);

DO NOT OVERUSE

Avoid relying on props MUTATION!

This may cause unexpected behaviors, or make your components difficult to maintain.

async componentDidMount() {
    const { data } = this.props // this.props.data is mapped from redux store.
    // dispatch an action and do some async call (e.g. xhr, fetch)
    await take('RECEIVE_DATA'); // receive new data and update redux store by reducer
    // DANGER: this.props.data is MUTATED!
    console.assert(this.props.data === data); // Assertion failed!
}