# redux-track-async

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

Latest version **1.1.6** (published 2016-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-track-async
pnpm add redux-track-async
yarn add redux-track-async
bun add redux-track-async
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2016-06-22 |
| First published | 2016-04-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Lars Rønhøj |
| Maintainers | lkr_ |
| Keywords | redux, async |

## Links

- npm: https://www.npmjs.com/package/redux-track-async
- Repository: https://github.com/lronhoj/redux-track-async
- Homepage: https://github.com/lronhoj/redux-track-async#readme
- Issues: https://github.com/lronhoj/redux-track-async/issues
- npm.io page: https://npm.io/package/redux-track-async

## Dependencies (1)

- [uuid](https://npm.io/package/uuid.md) ^2.0.2

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.1.6 (latest) — 2016-06-22
- 1.1.5 — 2016-06-22
- 1.1.4 — 2016-06-22
- 1.1.3 — 2016-06-21
- 1.1.2 — 2016-04-18
- 1.1.1 — 2016-04-18
- 1.1.0 — 2016-04-18
- 1.0.0 — 2016-04-18
- 0.0.3 — 2016-04-18
- 0.0.2 — 2016-04-17
- 0.0.1 — 2016-04-15

## README

Install
=======
`npm install --save redux-track-async`

Usage
=====
Creating actions
```javascript
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.
```javascript
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
```javascript
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
```javascript
{
  "status": "request",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  { other attributes }
}
```

#### Success action
```javascript
{
  "status": "success",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  "payload": result,
  { other attributes }
}
```

#### Failure action
```javascript
{
  "status": "failure",
  [ASYNC]: {
    "id": "<generated uuid>"
  },
  "error": error,
  { other attributes }
}
```

#### Completed action
```javascript
{
  "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](http://www.arosii.com/)

---
_Source: https://npm.io/package/redux-track-async · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
