R
C

PubSub

v0.0.2tested

Simple PubSub implementation

Signature

ts
class PubSub<Events extends Record<string, (...args: any[]) => any>>

Type Parameters

Eventsextends Record<string, (...args: any[]) => any>

Methods

on

Subscribe to an event

ts
public on<K extends keyof Events>(event: K, listener: Events[K])
ParameterTypeDescription
eventKName of the event
listenerEvents[K]Listener function
Returnsthis
off

Unsubscribe from an event

ts
public off<K extends keyof Events>(event: K, listener: Events[K])
ParameterTypeDescription
eventKName of the event
listenerEvents[K]Listener function
Returnsthis
once

Subscribe to an event only once

ts
public once<K extends keyof Events>(event: K, listener: Events[K])
ParameterTypeDescription
eventKName of the event
listenerEvents[K]Listener function
Returnsthis
emit

Emit an event

ts
public emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>)
ParameterTypeDescription
eventKName of the event
args?Parameters<Events[K]>Arguments for the listener
Returnsboolean
clear

Clear all listeners for an event

ts
public clear<K extends keyof Events>(event: K)
ParameterTypeDescription
eventKName of the event
Returnsthis