# typesafe-dispatcher

> typesafe-dispatcher

Latest version **0.0.9** (published 2017-01-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install typesafe-dispatcher
pnpm add typesafe-dispatcher
yarn add typesafe-dispatcher
bun add typesafe-dispatcher
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2017-01-12 |
| First published | 2016-12-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | cotto |
| Maintainers | cotto |
| Keywords | EventEmitter, event, flux, typescript |

## Links

- npm: https://www.npmjs.com/package/typesafe-dispatcher
- Repository: https://github.com/cotto89/typesafe-dispatcher
- Homepage: https://github.com/cotto89/typesafe-dispatcher#readme
- Issues: https://github.com/cotto89/typesafe-dispatcher/issues
- npm.io page: https://npm.io/package/typesafe-dispatcher

## Dependencies (1)

- [@types/node](https://npm.io/package/@types/node.md) ^6.0.58

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.0.9 (latest) — 2017-01-12
- 0.0.8 — 2017-01-11
- 0.0.7 — 2017-01-10
- 0.0.6 — 2017-01-10
- 0.0.5 — 2017-01-07
- 0.0.4 — 2016-12-29
- 0.0.3 — 2016-12-29
- 0.0.2 — 2016-12-24
- 0.0.1 — 2016-12-23

## README

# Typesafe dispatcher

This Dispatcher is using `keyof` and `MappedTypes`. So, This package require `"typescript": "^2.1.4"`.

## Install

```
npm i typesafe-dispatcher
```

https://www.npmjs.com/package/typesafe-dispatcher

## Example

```js
import Dispatcher from 'typesafe-dispatcher';
// or import * as Dispatcher from 'typesafe-disaptcher';


// { eventName: payloadTypes }
interface EventTypes {
    action: number;
    action2: { count: number };
    aciton3: string;
}

// create an instance with EventTypes
const dispatcher = new Dispatcher<EventTypes>();


// subscribe
const unsubscribe = dispatcher.subscribe({
	action: (payload) => {
		assert(typeof payload === 'number')
	},

	action2: (payload) => {
		assert(typeof payload.count === 'number')
	}
})


// subscribeAll
dispatcher.subscribeAll((event, payload) => {
    console.log({ event, payload });
})


// reaction
// reaction is event -> event
dispatcher.reaction({
    action: (count) => ({ action3: count + '' }),
    action2: ({count}) => Promise.resolve({ action3: count + '' })
})

/*
same as above

dispatcher.subscribe({
    action: (count) => dispatch('action3', count + '')),
    action2: ({ count }) => Promise.resolve(count).then(() => dispatch('action3', count + ''))
});
*/



// dispatch
dispatcher.dispatch('action', 1)
dispatcher.dispatch('action2', {count: 1})


// unsubscribe
unsubscribe();
```

![](https://raw.githubusercontent.com/cotto89/typesafe-dispatcher/master/img/typesafe-dispatcher2.png)
![](https://raw.githubusercontent.com/cotto89/typesafe-dispatcher/master/img/typesafe-dispatcher1.png)


## HelperTypes

### Subscriber

```js
type Subscriber<T> = {[P in keyof T]?: (payload: T[P]) => any};
```

```js
import { Subscriber } from 'typesafe-dispatcher';

const subscriber: Subscriber<EventTypes> = {
    action: (count) => count + 1,
}

dispatcher.subscribe(subscriber)
```

### Reactions

```js
type Reactions<T> = {[P in keyof T]?: (payload: T[P]) => Partial<T> | Promise<Partial<T>>};
```

```js
import { Reactions } from 'typesafe-dispatcher';

const reactions: Reactions<EventTypes> = {
    action: (count) => Promise.resolve<Partial<EventTypes>>({ action2: { count } }),
    action2: ({ count }) => ({ action3: count + '' })
}

dispatcher.reaction(reactions)
```

### For bind()

```js
import Dispatcher, { Dispatch, Subscribe, SubscribeAll } from 'typesafe-dispatcher';

const dispatcher = new Dispatcher<EventTypes>();
const dispatch: Dispatch<EventTypes> = dispatcher.dispatch.bind(dispatcher);
const subscribeAll: SubscribeAll<EventTypes> = dispatcher.subscribeAll.bind(dispatcher);
const reaction: Reaction<EventTypes> = dispatcher.reaction.bind(dispatcher);
```

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