# extended-emitter

> An emitter with expanded utility for node and the browser

Latest version **1.6.0** (published 2023-09-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install extended-emitter
pnpm add extended-emitter
yarn add extended-emitter
bun add extended-emitter
```

## 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.6.0 |
| Published | 2023-09-03 |
| First published | 2014-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | * |
| Dependencies | 1 |
| Unpacked size | 39.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Abbey Hawk Sparrow |
| Maintainers | khrome |
| Keywords | emitter, query, monitor |

## Links

- npm: https://www.npmjs.com/package/extended-emitter
- Repository: https://github.com/khrome/extended-emitter
- Issues: https://github.com/khrome/extended-emitter/issues
- npm.io page: https://npm.io/package/extended-emitter

## Dependencies (1)

- [sift](https://npm.io/package/sift.md) *

## 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.6.0 (latest) — 2023-09-03
- 1.5.1 — 2023-08-28
- 1.5.0 — 2023-08-28
- 1.4.0 — 2023-08-28
- 1.3.2 — 2023-06-19
- 1.3.1 — 2023-06-19
- 1.3.0 — 2023-06-19
- 1.2.0 — 2023-06-09
- 1.1.2 — 2023-04-15
- 1.1.1 — 2023-04-06
- 1.1.0 — 2021-09-23
- 1.0.4 — 2020-07-25
- 1.0.3 — 2019-05-02
- 1.0.2 — 2016-12-20
- 1.0.1 — 2016-11-22
- … 5 more at https://npm.io/package/extended-emitter/versions

## README

extended-emitter.js
===================

[![NPM version](https://img.shields.io/npm/v/extended-emitter.svg)](https://www.npmjs.com/package/extended-emitter)
[![npm](https://img.shields.io/npm/dt/extended-emitter.svg)](https://www.npmjs.com/package/extended-emitter)

[//]: # (Old travis link went here, replace with actions based build)

Everything you expect from `require('events').EventEmitter` in both the browser and client, plus:
- `criteria` : use a sift expression to work on a subset of the events
- `.allOff()` : removes all events from this emitter
- `.emitter` : the internal emitter used, in case you need direct access.

import
------
**CommonJS**
```javascript
const { Emitter } = require('extended-emitter');
const emitter = new Emitter();
```
**ES6 imports**
```javascript
import { Emitter } from 'extended-emitter';
const emitter = new Emitter();
```

optional criteria
-----------------
you can now using [mongo-style](https://docs.mongodb.com/manual/reference/operator/query/) queries (supported by [sift](https://www.npmjs.com/package/sift)) to subscribe to specific events (in this context `.once()` means meeting the criteria, not just firing an event of that type).

```javascript
emitter.on('my_object_event', {
    myObjectId : object.id
}, function(){
    //do stuff here
});

// or

emitter.once('my_object_event', {
    myObjectId : object.id,
    myObjectValue : {
    	$gt : 20,
    	$lt : 40
    }
}, function(){
    //do stuff here
});
```

.when()
-------

and there's also the addition of a `when` function which can take ready-style functions, real promises or events, making it easy to delay or wait for a state, without resorting to chaining.

### async
```javascript
await emitter.when([$(document).ready, 'my-init-event', 'my-load-event']);
```

### callbacks
```javascript
emitter.when([$(document).ready, 'my-init-event', 'my-load-event'], function(){
	//do stuff
});
```

.onto()
-------
Often you want an object to implement emitters, and while it's easy enough to wrap them, why not just have that done for you and avoid the boilerplate?

```javascript
emitter.onto(MyClass.prototype);
emitter.onto(MyInstance);
emitter.onto(MyObject);
```

or in the constructor:

```javascript
(new Emitter()).onto(this);
```

Testing
-------

Run the tests at the project root with:

```bash
npm run import-test
npm run require-test
```

Enjoy,

-Abbey Hawk Sparrow

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