Skip to main content
Go to Latest
class EventEmitter
import { EventEmitter } from "https://deno.land/x/evtemitter@v3.0.0/EventEmitter.ts";

Strictly typed version of an EventEmitter. A TypedEventEmitter takes type parameters for mappings of event names to event data types, and strictly types method calls to the EventEmitter according to these event maps.

Type Parameters

optional
UserEvents extends CustomEventMap = CustomEventMap
optional
ReservedEvents extends CustomEventMap = Record<never, never>

Properties

protected
__listeners__: Map<EvName, CustomEventListenerMap<ReservedEvents, UserEvents>>
dispatch

Alias for emit

listeners
publish

same as emit. Used for pub / sub conventions

Methods

protected
emitReserved<Ev extends EventNames<ReservedEvents>>(type: Ev, ...unnamed 1: CustomEventDetailParameter<ReservedEvents, Ev>): this

Emits a reserved event.

This method is protected, so that only a class extending StrictEventEmitter can emit its own reserved events.

protected
emitUntyped(type: EvName, ...unnamed 1: CustomEventDetailParameter<CustomEventMap, EvName>): this

Emits an event.

This method is protected, so that only a class extending StrictEventEmitter can get around the strict typing. This is useful for calling emit.apply, which can be called as emitUntyped.apply.

Creates a wrapper function around a given callback which passes the value of the detail property of a CustomEvent to the callback

add a callback to an event or multiple events

dispatchEvent<Ev extends Event>(event: Ev): boolean
emit<Ev extends EventNames<UserEvents>>(type: Ev, ...unnamed 1: CustomEventDetailParameter<UserEvents, Ev>): this

Emit an event with given detail Calls all listeners that listen to the emitted event

Get all EventListeners for a specific Event

off(): this

remove all EventListeners

remove all EventListeners for a specific event

off<Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>>(types: Ev[]): this

remove all EventListeners for multiple specific events

remove a specific EventListener for a specific event

remove a specific EventListener for multiple specific events

add a callback to an event

add a callback to multiple events

add a callback to an event only once. After that, the listener is removed.

add a callback to multiple events only once. After that, the listener is removed.

pull<Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>>(type: Ev): Promise<UserEvents[Ev]>

wait for an event to be dispatched

pull<Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>>(type: Ev, timeout: number): Promise<UserEvents[Ev]>

wait for an event to be dispatched and reject after a specific amount of milliseconds

subscribe<Ev extends ReservedOrUserEventNames<ReservedEvents, UserEvents>>(
type: Ev,
options?: Parameters<EventTarget["addEventListener"]>[2],
): Fn<never, void>

Subscribe method. Returns a cleanup function to remove the added EventListener

Static Methods

createEvent<Type extends EvName>(type: EvName): TypedCustomEvent<Type, undefined>

create a typed CustomEvent, which will have a typed type property

createEvent<Type extends EvName, Detail>(
type: EvName,
detail: Detail,
init?: Omit<CustomEventInit, "detail">,
): TypedCustomEvent<Type, Detail>

create a typed CustomEvent, which will have types for the detail and the type property