# es6-event-emitter

> Custom, simple, extendable event/messaging system written in ES6

Latest version **1.10.2** (published 2017-08-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install es6-event-emitter
pnpm add es6-event-emitter
yarn add es6-event-emitter
bun add es6-event-emitter
```

## 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.10.2 |
| Published | 2017-08-31 |
| First published | 2016-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Joe Fusco |
| Maintainers | jfusco |
| Keywords | event, pub/sub, publish, subscribe, observer, emitter, es2015, es6, es6 events |

## Links

- npm: https://www.npmjs.com/package/es6-event-emitter
- Repository: https://github.com/JFusco/es6-event-emitter
- Homepage: https://github.com/JFusco/es6-event-emitter#readme
- Issues: https://github.com/JFusco/es6-event-emitter/issues
- npm.io page: https://npm.io/package/es6-event-emitter

## 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.10.2 (latest) — 2017-08-31
- 1.10.1 — 2017-08-27
- 1.10.0 — 2017-08-27
- 1.9.0 — 2017-03-29
- 1.8.2 — 2017-03-18
- 1.7.4 — 2017-03-13
- 1.6.3 — 2017-03-11
- 1.5.2 — 2016-09-11
- 1.5.1 — 2016-06-18
- 1.4.0 — 2016-04-10
- 0.0.4 — 2016-04-03
- 0.0.2 — 2016-04-02
- 0.0.1 — 2016-04-02

## README

# es6-event-emitter

[![Build Status][build-image]][build-url]

[![devDependency Status][dev-dep-image]][dev-dep-url]

[![MIT][mit-image]][mit-url]
[![npm][npm-version-image]][npm-url]

> Custom, simple, extendable event/messaging system written in ES6

## Getting Started ##

#### Installation
From the root of your project.
```sh
npm install es6-event-emitter --save
```

#### Usage
Simple implementation of emitter. See [api](#api) below.
```
import Emitter from 'es6-event-emitter';

export default class Component extends Emitter {
	constructor(){
		super();
	}

	//-- Trigger
	someAction(){
		...

		this.trigger('component:action');
	}

	//-- Trigger with data
	someOtherAction(){
		...

        this.trigger('component:otheraction', {
            foo: 'bar',
            baz: 'buzz'
        });
    }
}

//-- Create a new component
const component = new Component();

//-- Set up functions for listeners - best practice in case you want to remove them later.
const action = () => {
	console.log('action triggered');
}

const otheraction = data => {
	console.log(`other action triggered with data ${data}`);
}

//-- Register listeners
component.on('component:action', action);
component.on('component:otheraction', otheraction);

//-- Call methods
component.someAction();
component.someOtherAction();
```

<a name="api"></a>
#### API
* **[`on`](#on)**
* **[`off`](#off)**
* **[`once`](#once)**
* **[`trigger`](#trigger)**
* **[`destroy`](#destroy)**

<a name="on"></a>
##### on - **`on(event:String, callback:Function)`**
Registers a listener
```js
component.on('component:action', action);
```

<a name="off"></a>
##### off - **`off(event:String, callback:Function)`**
Removes a registered listener from the event stack
```js
component.off('component:action', action);
```

<a name="once"></a>
##### once - **`once(event:String, callback:Function)`**
Registers a listener that will only fire once no matter how many times you try to trigger
```js
component.once('component:action', action);
```

<a name="trigger"></a>
##### trigger - **`trigger(event:String, ...args:*)`**
Triggers a registered event with optional data
```js
this.trigger('component:action');

//-- With data
this.trigger('component:action', {
    foo: 'bar',
    baz: 'buzz'
});
```

<a name="destroy"></a>
##### destroy - **`destroy()`**
Destroys the entire event emitter
```js
component.destroy();
```

## Tests ##
View the [test coverage](https://jfusco.github.io/es6-event-emitter/coverage/reports/lcov-report/src/index.html)
```
npm test
```

[build-image]: https://travis-ci.org/JFusco/es6-event-emitter.svg?branch=master
[build-url]: https://travis-ci.org/JFusco/es6-event-emitter
[mit-image]: https://img.shields.io/npm/l/es6-event-emitter.svg?style=flat-square
[mit-url]: https://github.com/JFusco/es6-event-emitter/blob/master/LICENSE
[npm-version-image]: https://img.shields.io/npm/v/npm.svg?maxAge=2592000
[npm-url]: https://www.npmjs.com/package/es6-event-emitter
[dev-dep-image]: https://david-dm.org/JFusco/es6-event-emitter/dev-status.svg
[dev-dep-url]: https://david-dm.org/JFusco/es6-event-emitter?type=dev

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