# redux-command-handler

> [![Build Status](https://travis-ci.org/fabioelizandro/redux-command-handler.svg?branch=master)](https://travis-ci.org/fabioelizandro/redux-command-handler) [![Maintainability](https://api.codeclimate.com/v1/badges/d24f5963a7dfa50e8b71/maintainability)](ht

Latest version **2.0.0** (published 2018-01-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-command-handler
pnpm add redux-command-handler
yarn add redux-command-handler
bun add redux-command-handler
```

## 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 | 2.0.0 |
| Published | 2018-01-08 |
| First published | 2018-01-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Author | Fabio Elizandro |
| Maintainers | fabioelizandro |

## Links

- npm: https://www.npmjs.com/package/redux-command-handler
- npm.io page: https://npm.io/package/redux-command-handler

## Recent versions

- 2.0.0 (latest) — 2018-01-08
- 1.0.0 — 2018-01-06

## README

# Redux command handler

[![Build Status](https://travis-ci.org/fabioelizandro/redux-command-handler.svg?branch=master)](https://travis-ci.org/fabioelizandro/redux-command-handler)
[![Maintainability](https://api.codeclimate.com/v1/badges/d24f5963a7dfa50e8b71/maintainability)](https://codeclimate.com/github/fabioelizandro/redux-command-handler/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/d24f5963a7dfa50e8b71/test_coverage)](https://codeclimate.com/github/fabioelizandro/redux-command-handler/test_coverage)


`redux-command-handler` is a library that aims to separate commands from events instead of have just actions. Using this libraries all redux actions becames events and events are generate just by commands. 

Commands with side effects are not a problem at all.

## How to use

`get-user-command.js`
```js
export default async (commandPayload, eventDispatcher) => {
  eventDispatcher('USER_REQUESTED'); //dispatch event { type: 'USER_REQUESTED' } into redux store
  const userId = commandPayload.id;
  const user = await fetch(`/user/${userId}`).then(response => response.json());
  eventDispatcher('USER_LOADED', user); // event { type: 'USER_LOADED', payload: {...}}
};
```

`command-handler.js`
```js
import getUserCommand as GET_USER from './get-user-command';
import { createCommandHandler } from 'redux-command-handler';

export default createCommandHandler({ GET_USER });
```

`show-user-button.js`
```js
import React from 'react';
import commandHandler from './command-handler';

const ShowComponent = props => {
 return (<button onClick={props.dispatch('GET_USER', { id: props.userId })}>Show User</button>);
};

const mapStateToProps = state => state;
export default connect(mapStateToProps, commandHandler)(ShowComponent);
```

## Combine commands

`command-handler.js`
```js
import payBillCommand from './pay-bill-command';
import showReceiptCommand from './show-receipt-command';
import { createCommandHandler, combineCommandHandlers } from 'redux-command-handler';

const PAY_BILL = combineCommandHandlers(payBillCommand, showReceiptCommand);

export default createCommandHandler({ PAY_BILL });
```

## Parallelize commands

`command-handler.js`
```js
import getUserCommand from './get-user-command';
import trackGetUserCommand from './track-get-user-command';
import { createCommandHandler, parallelizeCommandHandler } from 'redux-command-handler';

const GET_USER = parallelizeCommandHandler(getUserCommand, trackGetUserCommand);

export default createCommandHandler({ GET_USER });
```

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