# small-emitter

> A small event emitter library

Latest version **0.2.0** (published 2024-02-12) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2024-02-12 |
| First published | 2021-12-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Lv Heng |
| Maintainers | chennings |
| Keywords | event, emitter, eventbus |

## Links

- npm: https://www.npmjs.com/package/small-emitter
- Repository: https://github.com/uphg/emitter
- Homepage: https://github.com/uphg/emitter#readme
- Issues: https://github.com/uphg/emitter/issues
- npm.io page: https://npm.io/package/small-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

- 0.2.0 (latest) — 2024-02-12
- 0.1.9 — 2024-02-12
- 0.1.8 — 2023-12-01
- 0.1.7 — 2022-11-07
- 0.1.6 — 2022-11-07
- 0.1.5 — 2022-11-07
- 0.1.4 — 2021-12-11
- 0.1.3 — 2021-12-10
- 0.1.2 — 2021-12-10

## README

# Emitter

English | [简体中文](./README.zh.md)

A small event emitter library.

## Install

Install with npm

```sh
npm i small-emitter
# or pnpm add small-emitter
```

Import in the project

```js
// ES6 modules
import { Emitter } from 'small-emitter'

// CommonJS modules
const { Emitter } = require('small-emitter')
```

## Usage

```js
import { Emitter } from 'small-emitter'
const emitter = new Emitter();

// subscribe to events
emitter.on('foo', function (p1, p2, p3) {
 // ...
});

// subscribe the event only once
emitter.once('foo', function (p1, p2, p3) {
 // ...
});

// publish events
emitter.emit('foo', 'p1', 'p2', 'p3');

// clearing all events
emitter.clear()

// use reference functions
function fn() {}
emitter.on('foo', fn)
emitter.off('foo', fn)
```

## API

### `on<T extends Function>(name: string, callback: T)`

**Arguments**

- `name` name of the event
- `callback` event's callback function

**Example**

```js
const emitter = new Emitter()

emitter.on('foo', (data) => {
  console.log(data)
})

emitter.emit('foo', 1) // 1
emitter.emit('foo', 2) // 2
```

### `once<T extends Function>(name: string, callback: T)`

**Arguments**

- `name` name of the event
- `callback` event's callback function

**Example**

```js
const emitter = new Emitter()

emitter.once('foo', (data) => {
  // The function will only be executed once
  console.log(data)
})

emitter.emit('foo', 'a') // 'a'
emitter.emit('foo', 'b') // Not output
```

### `emit<T extends unknown[]>(name: string, ...args: T)`

**Arguments**

- `name` name of the event
- `...args (optional)` parameters passed in by the execution event

**Example**

```js
const emitter = new Emitter()

emitter.once('bar', (message, name) => {
  console.log(`${message}, ${name}`)
})

emitter.emit('bar', 'hi', 'Jack') // hi, Jack
```

### `off<T extends Function>(name: string, callback?: T)`

**Arguments**

- `name` name of the event
- `callback (optional)` functions that need to be cleared or function reference

**Example**

```js
const emitter = new Emitter()

const fn = (message) => {
  console.log(message)
}

emitter.on('bar', fn)
emitter.emit('bar', 'hi') // hi
emitter.off('bar', fn)
emitter.emit('bar', 'Jack') // Not output
```

### `clear()`

Calling this method will clear all events

**Example**

```js
const emitter = new Emitter()

emitter.on('foo', (value) => {
  console.log(value)
})

emitter.on('bar', (value) => {
  console.log(value)
})

emitter.emit('foo', 'hi, foo')
emitter.emit('bar', 'hi, bar') // hi, bar
emitter.clear()
emitter.emit('foo', 'hi, Jack') // Not output
emitter.emit('bar', 'hi, Tom') // Not output
```

## License

[MIT License](https://opensource.org/licenses/MIT)

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