https://github.com/beefchimi/emitten
A barebones event emitter written in TypeScript
https://github.com/beefchimi/emitten
emitter event events typescript
Last synced: 5 months ago
JSON representation
A barebones event emitter written in TypeScript
- Host: GitHub
- URL: https://github.com/beefchimi/emitten
- Owner: beefchimi
- License: isc
- Created: 2023-01-23T21:32:42.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-18T21:53:28.000Z (almost 2 years ago)
- Last Synced: 2025-10-10T19:41:36.172Z (9 months ago)
- Topics: emitter, event, events, typescript
- Language: TypeScript
- Homepage: https://dulmage.me
- Size: 621 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Emitten
[](https://opensource.org/licenses/ISC)
> `Emitten` is a very basic event emitter for TypeScript.
This simple `class` allows you to subscribe/unsubscribe to a defined library of events.
## Get started
Add `Emitten` to your project.
```sh
# Using NPM
npm install emitten
# Or some other package manager, such as Yarn
yarn add emitten
```
Import and start emit’in!
```ts
// Import the fully public Emitten variant.
// Alternatively, use the fully protected variant: EmittenProtected
// Or, the partially protected variant: EmittenCommon
import {Emitten} from 'emitten';
// Define your “event map” of `eventName: listener(args)` pairs.
// Both the `eventName` and the `args` from the `listener` are
// captured by TypeScript to assert type-safety!
type EventMap = {
change: (value: string) => void;
count: (value?: number) => void;
collect: (...values: boolean[]) => void;
other: (required: string, ...optional: string[]) => void;
};
// Instantiate, passing `EventMap` as a generic.
const myEmitter = new Emitten();
// By referencing the `EventMap` you created,
// you can assign the correct type to your listeners.
const handleChange: EventMap['change'] = (value) => {};
// Register a subscription!
myEmitter.on('change', handleChange);
// Emit an event! The arguments accepted
// by `.emit()` will be correctly typed.
myEmitter.emit('change', 'Hello world');
// Remove a subscription!
myEmitter.off('change', handleChange);
// Register a subscription only once! The listener
// will be automatically removed after a single `emit`.
myEmitter.once('count', (value = 0) => {});
myEmitter.emit('count', 123);
// Capture and manually call the removal function!
// The arguments accepted by any listener will correctly
// correspond with the registered `eventName`.
const dispose = myEmitter.on('collect', (...values) => {});
myEmitter.emit('collect', true, false, !true, !false);
dispose();
// Get a list of all the events that currently
// have subscriptions.
const currentEvents = myEmitter.activeEvents;
// Remove all registered listeners!
myEmitter.empty();
```
For more guidance, please take a look at the [`Examples document`](./docs/examples.md).