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-track-async

v1.1.6

Published

A lib for tracking pending async requests. Exposes middleware & a reducer

Downloads

5

Readme

Install

npm install --save redux-track-async

Usage

Creating actions

import {ASYNC} from 'redux-track-async';

export const SOME_ENTITIES = 'SOME_ENTITIES';
export function fetchSomeEntities() {
    return {
        type   : SOME_ENTITIES,
        // simple mode - just a promise needed
        [ASYNC]: fetch('/api/some/entities')
    };
}

export const SOME_ENTITY = 'SOME_ENTITY';
export function fetchSomeEntity(id) {
    return {
        type   : SOME_ENTITY,
        // specify options
        [ASYNC]: {
            promise: fetch(`/api/some/entities/${id}`),
            parse  : response => response.json()
        }
    };
}

Create a store with a reducer to track async actions. Wrap your own root reducer in order to allow the asyncReducer to block orphaned responses. Attach middleware.

import {middleware as asyncMiddleware, reducer as asyncReducer} from 'redux-track-async';
import {createStore, applyMiddleware} from 'redux';

const initialState = {};
const store = createStore(
    asyncReducer(rootReducer),
    initialState,
    applyMiddleware(asyncMiddleware)
);

API

middleware

ASYNC

Type: Symbol

A constant which is used as an attribute of dispatched actions to signal middleware.

middleware consumed actions

action[ASYNC]: options || promise

promise (Required)

Type: Promise

The promise to track.

parse

Type: function
Default: returns response.json() if the result is a Response

middleware produced actions

The middleware produces a series of actions for each async actions it consumes. The status attribute changes througout the series. request => success|failure => completed. Request and completed actions are always produced. Success or failure actions are produced depending on wether the promise is fulfilled or rejected.

Request action

{
  "status": "request",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  { other attributes }
}

Success action

{
  "status": "success",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  "payload": result,
  { other attributes }
}

Failure action

{
  "status": "failure",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  "error": error,
  { other attributes }
}

Completed action

{
  "status": "completed",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  { other attributes }
}

reducer(rootReducer)

rootReducer (Required)

Type: function

The reducer wraps the application root reducer. This allows it to block orphaned actions caused by async actions being fired after page shifts or other context shifts where the async state has been cleared.

actions

clearPendingRequests()

Takes no arguments and causes reducer to clear it's state of pending requests. All pending requests will be orphans and success|failure actions will be blocked from the rootReducer. Only the completed action will be fired. The completed action is fired to allow other reducers to remove loading states for pending requests.

clearPendingRequest(id)

Takes a uuid of an async request and removes it from the async state. Like clearPendingRequests() it causes the success|failure actions to be blocked from the rootReducer. Only the completed action will be fired. The completed action is fired to allow other reducers to remove loading state for the pending request.

LICENSE

MIT © Arosii A/S