# @jswork/event-mitt

> A mini and light event emitter.

Latest version **2.0.17** (published 2025-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jswork/event-mitt
pnpm add @jswork/event-mitt
yarn add @jswork/event-mitt
bun add @jswork/event-mitt
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.17 |
| Published | 2025-07-03 |
| First published | 2020-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 59.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | afeiship, yujiao.luo |
| Keywords | event, emitter, mini, on, off, emit, tiny, event-bus, one, once, immediate |

## Links

- npm: https://www.npmjs.com/package/@jswork/event-mitt
- npm.io page: https://npm.io/package/@jswork/event-mitt

## Dependencies (1)

- [@jswork/wildcard-match](https://npm.io/package/@jswork/wildcard-match.md) ^1.0.1

## 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

- 2.0.17 (latest) — 2025-07-03
- 2.0.16 — 2024-09-08
- 2.0.15 — 2024-09-08
- 2.0.14 — 2024-09-08
- 2.0.13 — 2024-09-08
- 2.0.12 — 2024-09-08
- 2.0.11 — 2024-09-08
- 2.0.10 — 2024-09-08
- 2.0.9 — 2024-09-08
- 2.0.8 — 2024-09-08
- 2.0.7 — 2024-09-07
- 2.0.6 — 2024-09-07
- 2.0.5 — 2024-09-07
- 2.0.4 — 2024-09-07
- 2.0.3 — 2024-09-07
- … 17 more at https://npm.io/package/@jswork/event-mitt/versions

## README

# event-mitt
> A mini and light event emitter

[![version][version-image]][version-url]
[![license][license-image]][license-url]
[![size][size-image]][size-url]
[![download][download-image]][download-url]

## apis
| name         | params         | description                                           |
|--------------|----------------|-------------------------------------------------------|
| on           | name/*,handler | register an event                                     |
| off          | name,handler   | unregister an event                                   |
| emit         | name,obj       | fire an event                                         |
| one          | name,handler   | fire an event,only can register once, same function   |
| once         | name,handler   | fire an event,only can execute once                   |
| upon         | name,handler   | fire an event, only register once, different function |
| on2immediate | name,handler   | Fire an event, immdiate execute function              |

## installation
```shell
npm install -S @jswork/event-mitt
```

## usage
```js
import EventMitt from '@jswork/event-mitt';
const Person = class { };
Object.assign(Person.prototype, EventMitt);
const p1 = new Person();
const sum = 0;
const total = 0;

// attach events:
var res = p1.on('ev1', () => {
  console.log('ev1', sum);
  sum = sum + 1;
});

p1.on('ev2', () => {
  console.log('ev2', sum);
  sum = sum + 3;
});

p1.on('ev3', () => {
  console.log('ev3', sum);
  sum = sum + 5;
});

p1.on('*', (name) => {
  console.log('just a log', name);
  total++;
});

// emit events:
p1.emit('ev1');
p1.emit('ev2');
p1.emit('ev3');

// destory:
res.destroy();

// ---- mitt fn ----
import { mitt } from "@jswork/event-mitt";
const emitter = mitt();

let sum = 0;

emitter.on("ev1", () => {
  console.log("ev1", sum);
  sum = sum + 1;
});

emitter.on("ev2", () => {
  console.log("ev2", sum);
  sum = sum + 3;
});

emitter.on("ev3", () => {
  console.log("ev3", sum);
  sum = sum + 5;
});

emitter.on("*", (name) => {
  console.log("just a log", name);
});

emitter.emit("ev1");
emitter.emit("ev2");
emitter.emit("ev3");
```

## license
Code released under [the MIT license](https://github.com/afeiship/event-mitt/blob/master/LICENSE.txt).

[version-image]: https://img.shields.io/npm/v/@jswork/event-mitt
[version-url]: https://npmjs.org/package/@jswork/event-mitt

[license-image]: https://img.shields.io/npm/l/@jswork/event-mitt
[license-url]: https://github.com/afeiship/event-mitt/blob/master/LICENSE.txt

[size-image]: https://img.shields.io/bundlephobia/minzip/@jswork/event-mitt
[size-url]: https://github.com/afeiship/event-mitt/blob/master/dist/event-mitt.min.js

[download-image]: https://img.shields.io/npm/dm/@jswork/event-mitt
[download-url]: https://www.npmjs.com/package/@jswork/event-mitt

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