# global-dispatcher

> Basically a bare bones global event emitter

Latest version **1.0.0** (published 2018-11-24) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-11-24 |
| First published | 2018-05-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=6 |
| Dependencies | 0 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Laxman |
| Maintainers | notlmn |
| Keywords | subscribe, subscriber, dispatch, dispatcher, eventemitter |

## Links

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

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

- 1.0.0 (latest) — 2018-11-24
- 0.0.2 — 2018-09-02
- 0.0.1 — 2018-05-08

## README

# global-dispatcher

> Basically a global bare bones event emitter

- **Functional**: no `this` (for you poeple obsessed with FP)
- **Tiny**: 350 Bytes (min + gzip)
- **No transpilation required**: written in ES3, import directly
- **No constructor**: everything is [global](#why-global-by-default)
- Support [synchronous and asynchronous dispatches](#sync-and-async-dispatches)

## Import

- **CJS**: `const {subscribe, dispath} = require('global-dispatcher');`
- **ESM/Node**: `import {subscribe, dispath} from 'global-dispatcher';`
- **ESM/Unpkg**: `import {subscribe, dispath} from 'https://unpkg.com/global-dispatcher/min.esm.js';`

## Usage

### Basic

``` js
// Register a new subscription
subscribe('event', data => console.log(data));

// ...

// Somewhere else in the code land
dispatch('event', {
	a: 'b'
});
```

### Unsubscribing listeners

``` js
const unsub = subscribe('event', () => {});

// Calling `unsub` removes the specified listener from the list
unsub();
```


## Example

``` js
// list.js
import {subscribe} from 'global-dispatcher';

class List extends HTMLElement {
  connectedCallback() {
    subscribe('new-item', item => this.renderNewItem(item));
  }

  renderNewItem(item) {
    // ...
  }
}
```

``` js
// button.js
import {dispatch} from 'global-dispatcher';

class Button extends HTMLElement {
  connectedCallback() {
    this.addEventListener('click', () => {
      const randomString = (Math.random() * 1e16).toString(36);
      dispatch('new-item', randomString);
    });
  }
}
```

## FAQ

### Motivation

- Some modules require you to transpile for your target, you may end up having to transpile even when your original pipeline doesn't need one.
- They are not global (see below), or require singleton pattern files, even when you just need a single instance.

### Sync and Async dispatches

`dispatch(...)` by default calls the listeners asynchronously, to run listeners synchronously, use `dispathcSync(...)` instead.

### Why global by default?

Because when you create an instance of an emitter thingy in each of your component, there is no way for them to communicate with each other. This module follows the singleton pattern in a global level (expand below to see example).

<details>
<summary>Basically you need to have a common emitter object that all of your modules can import</summary>

Some people end up using hacks like the one below

``` js
// util/emitter.js
import eventEmitterConstructor from 'some-event-emitter-library';

export default eventEmitterConstructor();

// components/component1.js
import emitter from '../util/emitter.js'

// components/component2.js
import emitter from '../util/emitter.js'
```
</details>

### Why the name `global-dispather`

Because its global by default, not bound to any object/constructor. And you do not emit or subscribe to events on an emitter object, so it ended up being a dispatcher.


## License

[MIT](license)

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