# @irrelon/emitter

> Simple JavaScript event emitter with some powerful features.

Latest version **6.2.0** (published 2025-09-23) · 0 weekly downloads

## Install

```sh
npm install @irrelon/emitter
pnpm add @irrelon/emitter
yarn add @irrelon/emitter
bun add @irrelon/emitter
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.2.0 |
| Published | 2025-09-23 |
| First published | 2018-06-05 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | * |
| Dependencies | 1 |
| Unpacked size | 1.5 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Rob Evans - Irrelon Software Limited |
| Maintainers | coolbloke1324 |
| Keywords | javascript, browser, node, client-side, server-side, emitter, events, on, off, once, listener |

## Links

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

## Dependencies (1)

- [jsdoc](https://npm.io/package/jsdoc.md) ^4.0.4

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 6.2.0 (latest) — 2025-09-23
- 2.0.11 (dev) — 2018-06-05
- 6.1.0 — 2024-06-25
- 6.0.1 — 2024-01-23
- 6.0.0 — 2023-11-28
- 5.0.6 — 2023-11-16
- 5.0.5 — 2023-11-16
- 5.0.4 — 2023-11-16
- 5.0.3 — 2023-04-16
- 5.0.2 — 2023-04-16
- 5.0.0 — 2021-11-26
- 4.0.1 — 2021-03-17
- 3.1.0 — 2021-03-12
- 3.0.1 — 2020-03-21
- 3.0.0 — 2019-08-13

## README

# Irrelon Emitter

### What Is It?
It's an event emitter that supports some very powerful and useful features.

### Why Not Just Use window.dispatchEvent()?
This library is more flexible and operates in both Node.js and the browser with the same interface.
This is useful because it allows you to maintain the same code patterns on both the backend and frontend of your application.

This event emitter also supports many more features than the basic event system built into browsers.

## Usage

### JavaScript
```javascript
import { Emitter } from "@irrelon/emitter";
const emitter = new Emitter();

// When the emit() call is made at the end of this
// example this listener will be called with
// `isEnabled = true` and `id = "1234"`. You can pass
// any number of arguments when calling emit() and
// they will be recieved in the same order by your
// event listeners
emitter.on("someEvent", (isEnabled, id) => {
    return "someReturnValue1";
});

// Lets register another listener on the same event
// that returns a slightly different value
emitter.on("someEvent", (isEnabled, id) => {
	return "someReturnValue2";
});

// The `results` will contain the return values from
// all the event listeners registered for the event
// so in this case ["someReturnValue1", "someReturnValue2"]
const results = emitter.emit("someEvent", true, "1234");
```

### TypeScript Compatibility
> TypeScript based projects can benefit from type safety if you declare the event listener
> function signatures via an interface as shown below

```typescript
import { Emitter } from "@irrelon/emitter";

interface MyEvents {
    event1: (name: string) => number;
}

// Pass your interface to the Emitter instantiation
const emitter = new Emitter<MyEvents>();

// This will show a typescript error because the
// first argument of the event1 listener should be
// a string, and the listener should return a number
// instead of void.
emitter.on("event1", (isEnabled: boolean) => {
    return;
});

// This will not error as it satisfies the
// MyEvents.event1 event listener signature
emitter.on("event1", (name: string) => {
    return 18;
});

// This will error because a string argument is
// expected and none is provided to the call
emitter.emit("event1");

// This will not error as you are passing the
// expected string argument
emitter.emit("event1", "John Smith");

// This will correctly infer the type of `result`
// as a number since the return type was defined
// in the MyEvents.event1 interface
const result = emitter.emit("event1", "John Smith");
```

### Extending The Emitter Class

```js
import { Emitter } from "@irrelon/emitter";

class MyClass extends Emitter {
    async someAsyncFunc () {
        await this.emit('myEvent', myData, myOtherData);
    }

    someFunc () {
        this.emit('myEvent', myData, myOtherData);
    }
};
```

Your class now inherits the emitter methods:

* on
* off
* once
* emit
* emitId
* emitStatic
* emitStaticId
* cancelStatic
* deferEmit
* willEmit
* rpc
* rpcId

### EcmaScript Modules and CommonJS Modules
> The package includes both ESM and CJS modules for ease of use.
> Use `import` to get the ESM version, and `require()` to get
> the CJS version.

### Install via NPM / Yarn:

```bash
npm i @irrelon/emitter
```

```bash
yarn add @irrelon/emitter
```

### Include in Your Application

```js
import {Emitter} from "@irrelon/emitter";
```
or
```js
var Emitter = require("@irrelon/emitter");
```

### Browser

Include the Emitter.js file in your HTML (the path depends on where you've put the file)

```html
<script src="./dist/esm/src/Emitter.js" type="module"></script>
```

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