Skip to main content

Overview

The Emitter class provides a simple and efficient event emitter implementation for the publish-subscribe pattern. It allows you to register event listeners, emit events, and manage subscriptions.

Class: Emitter

Constructor

const emitter = new Emitter();
Creates a new instance of the Emitter class.

Methods

on()

Registers an event listener for the specified event.
on<Args extends unknown[]>(event: string, callback: Callback<Args>): () => void
event
string
required
The name of the event to listen for
callback
Callback<Args>
required
The callback function to execute when the event is emitted. The callback receives the arguments passed to emit().
return
() => void
Returns an unsubscribe function that removes the listener when called
Example:
const unsubscribe = emitter.on('user:created', (user) => {
  console.log('User created:', user);
});

// Later, to remove the listener
unsubscribe();

off()

Removes a specific event listener.
off<Args extends unknown[]>(event: string, callback: Callback<Args>): void
event
string
required
The name of the event
callback
Callback<Args>
required
The callback function to remove
Example:
const handler = (data) => console.log(data);
emitter.on('data:update', handler);

// Remove the specific listener
emitter.off('data:update', handler);

offAll()

Removes all listeners for a specific event, or all listeners for all events.
offAll(event?: string): void
event
string
The name of the event to clear. If omitted, clears all listeners for all events.
Example:
// Remove all listeners for a specific event
emitter.offAll('user:created');

// Remove all listeners for all events
emitter.offAll();

once()

Registers a one-time event listener that automatically unsubscribes after being called once.
once<Args extends unknown[]>(event: string, callback: Callback<Args>): void
event
string
required
The name of the event to listen for
callback
Callback<Args>
required
The callback function to execute once when the event is emitted
Example:
emitter.once('app:ready', () => {
  console.log('Application is ready!');
});

emitter.emit('app:ready'); // Logs: "Application is ready!"
emitter.emit('app:ready'); // Does nothing - listener was removed

emit()

Emits an event synchronously, calling all registered listeners immediately.
emit<Args extends unknown[]>(event: string, ...args: Args): void
event
string
required
The name of the event to emit
args
Args
required
Arguments to pass to the event listeners
Example:
emitter.emit('user:created', { id: 1, name: 'John' });
emitter.emit('data:processed', 'result', 42, true);

emitAsync()

Emits an event asynchronously, calling all registered listeners in the next microtask.
emitAsync<Args extends unknown[]>(event: string, ...args: Args): void
event
string
required
The name of the event to emit
args
Args
required
Arguments to pass to the event listeners
Example:
emitter.emitAsync('background:task', taskData);
console.log('This logs first');

// The event listeners will be called in the next microtask

Type Definitions

Callback

type Callback<Args extends unknown[] = unknown[]> = (...args: Args) => void;
A callback function type that accepts a variable number of arguments.

Usage Example

import { Emitter } from '@resolid/event';

const emitter = new Emitter();

// Register event listeners
const unsubscribe = emitter.on('user:login', (user) => {
  console.log(`User ${user.name} logged in`);
});

emitter.on('user:login', (user) => {
  console.log(`Welcome back, ${user.name}!`);
});

// Emit an event
emitter.emit('user:login', { name: 'Alice', id: 1 });
// Logs:
// "User Alice logged in"
// "Welcome back, Alice!"

// One-time listener
emitter.once('app:initialized', () => {
  console.log('App initialized!');
});

// Unsubscribe
unsubscribe();

// Clear all listeners for an event
emitter.offAll('user:login');

Source Code

Location: packages/event/src/index.ts

Build docs developers (and LLMs) love