Component

Typed Emitter

See on GitHub

A thin typed wrapper around tiny-emitter. Subclasses pin an event-map type so on/once/emit are fully type-safe.

Installation

pnpm dlx shadcn@latest add @joyco/typed-emitter

Usage

Extend TypedEmitter with an event map ({ eventName: payloadType }). Listeners and emits are checked against the map.

import { TypedEmitter } from '@/registry/lib/typed-emitter'
 
type PlayerEvents = {
  play: { time: number }
  pause: void
  seek: { from: number; to: number }
}
 
class Player extends TypedEmitter<PlayerEvents> {
  start() {
    this.emit('play', { time: 0 })
  }
 
  stop() {
    this.emit('pause', undefined)
  }
}
 
const player = new Player()
 
const off = player.on('play', ({ time }) => {
  console.log('playing from', time)
})
 
player.start()
off() // unsubscribe

API

MethodSignatureDescription
on(event, listener) => () => voidSubscribe to an event. Returns an unsubscribe function.
once(event, listener) => voidSubscribe to a single firing of an event.
off(event, listener?) => voidUnsubscribe a specific listener, or all listeners for an event when listener is omitted.
emit(event, payload) => voidprotected — emit an event from the subclass.

Notes

  • emit is protected on purpose — events are an implementation detail of the subclass, not part of its public surface. Consumers subscribe with on/once/off and let the class decide when to fire.
  • The unsubscribe function returned by on is the recommended way to clean up, particularly inside useEffect.
  • For events with no payload, type the value as void and pass undefined when emitting.

Related Components

Maintainers
Downloads
23Total
1 downloads today