# evt-listener

> EventListener, as an object.

Latest version **1.0.3** (published 2015-04-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install evt-listener
pnpm add evt-listener
yarn add evt-listener
bun add evt-listener
```

## 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.0.3 |
| Published | 2015-04-14 |
| First published | 2014-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Brian Mullan |
| Maintainers | bmullan91 |
| Keywords | evt-listener, events, listener, EventListener |

## Links

- npm: https://www.npmjs.com/package/evt-listener
- Repository: https://github.com/bmullan91/evt-listener
- Issues: https://github.com/bmullan91/evt-listener/issues
- npm.io page: https://npm.io/package/evt-listener

## 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.3 (latest) — 2015-04-14
- 1.0.2 — 2015-01-31
- 1.0.1 — 2014-10-19
- 1.0.0 — 2014-10-17
- 0.0.0 — 2014-10-08

## README

[![Build Status](https://travis-ci.org/bmullan91/evt-listener.svg?branch=master)](https://travis-ci.org/bmullan91/evt-listener) [![Coverage Status](https://img.shields.io/coveralls/bmullan91/evt-listener.svg)](https://coveralls.io/r/bmullan91/evt-listener?branch=master)

# evt-listener

### What is it?

A class encapsulating an event and it's listener together.

### Why would you use it?

Whenever you create an event listener and need to eventually turn it off, you would pass the same function passed to emitter.**on** to emitter.**removeListener** to do so.

Creating an EventListener (the class behind evt-listener) allows you to just call listener.**off** (where *listener* would be an instance of EventListener) without having to worry about saving and passing the event name and function. See below for a comparison.

## Prerequisites

Install it using npm, saving it as a dependency.

    npm i evt-listener --save

Both examples share the same event emitter boilerplate code.

```js
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
```

# Comparison

#### Without EventListener

```js
//we have to save the event name and the listener
var eventName = 'eventName';
var listener = function(payload) {
  console.log('Event ' + eventName + ' triggered with: ' + playload);
};

emitter.on(eventName, listener);

//sometime later...
emitter.removeListener(eventName, listener);
``` 

#### With EventListener

```js
var EventListener = require('evt-listener');

//create a listener
var eventNameListener = new EventListener(emitter, 'eventName');

eventNameListener.on(function(payload) {
  console.log('Event ' + eventNameListener.event + ' triggered with: ' + playload);
});

//sometime later...
eventNameListener.off();
``` 

## Tips

__1.__ Name your listeners appropriately!

__2.__ You can chain your listener function after you create an EventListener instance.

```js
//create a listener, giving it a handler
var eventNameListener = new EventListener(emitter, 'eventName').on(function(payload) {
  console.log('Event ' + listener.event + ' triggered with: ' + playload);
});

//same goes for once
var eventNameListener = new EventListener(emitter, 'eventName').once(function(payload) {
  console.log('Event ' + listener.event + ' triggered with: ' + playload);
});
```

__3.__ Don't like using the *new* keyword in javascript? No worries.

```js
var EventListener = require('evt-listener');
var listener = EventListener(emitter, 'eventName');
```

__4.__ Prefer the revealing module pattern?

```js
var EventListener = require('evt-listener').EventListener;
```

## PS

Check out [evt-emitter](https://www.npmjs.org/package/evt-emitter) which adds a __createListener__ factory method on the EventEmitter prototype. This allows you to create instances of EventListener without having to pass an emitter.

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