Methods
On
Register event handlers for specific events or globally.
Event-specific handler
Parameters:
event
: Event name to listen tohandler
: Function called when event occursoptions.signal
: AbortSignal to automatically remove handler
Returns: Unsubscribe function
Example:
const unsubscribe = emitter.on('userLogin', ({ userId }) => {
console.log(`User ${userId} logged in`)
})
// Later remove the handler
unsubscribe()
Global handler
Parameters:
globalEventHandler
: Function called for all eventsoptions.signal
: AbortSignal to automatically remove handler
Returns: Unsubscribe function
Example:
const unsubscribe = emitter.on(({ kind, payload }) => {
console.log(`Event ${kind}:`, payload)
})
Once
Register a one-time event handler that automatically removes itself after execution.
Event-specific handler
Parameters:
event
: Event name to listen tohandler
: Function called once when event occursoptions.signal
: AbortSignal to automatically remove handler
Returns: Unsubscribe function
Example:
emitter.once('userLogin', ({ userId }) => {
console.log(`First login for user ${userId}`)
})
Global handler
Parameters:
globalEventHandler
: Function called once for the next eventoptions.signal
: AbortSignal to automatically remove handler
Returns: Unsubscribe function
Next
Wait for the next occurrence of an event or any event.
Wait for specific event
Parameters:
event
: Event name to wait foroptions.timeout
: Milliseconds to wait before rejecting
Returns: Promise that resolves with event payload
Example:
try {
const loginData = await emitter.next('userLogin', { timeout: 5000 })
console.log('Login occurred:', loginData)
} catch (error) {
if (error instanceof EmitterTimeoutError) {
console.log('No login within 5 seconds')
}
}
Wait for any event
Parameters:
options.timeout
: Milliseconds to wait before rejecting
Returns: Promise that resolves with event info
Example:
const nextEvent = await emitter.next({ timeout: 10000 })
console.log(`Event ${nextEvent.kind} occurred:`, nextEvent.payload)
Emit
Trigger an event with optional payload.
Parameters:
event
: Event name to emitpayload
: Data to pass to event handlers
Example:
emitter.emit('userLogin', { userId: '123', timestamp: Date.now() })
emitter.emit('userLogout', { userId: '123' })
Off
Remove event handlers.
Remove specific handler
Parameters:
event
: Event namehandler
: Specific handler to remove
Example:
const handler = (data: any) => console.log(data)
emitter.on('userLogin', handler)
emitter.off('userLogin', handler)
Remove all handlers for event
Parameters:
event
: Event name to clear all handlers for
Example:
emitter.off('userLogin') // Removes all userLogin handlers
Remove global handler
Parameters:
globalEventHandler
: Global handler to remove
Example:
const globalHandler = ({ kind, payload }) => console.log(kind, payload)
emitter.on(globalHandler)
emitter.off(globalHandler)
Count
Get the number of registered handlers.
Count handlers for specific event
Parameters:
event
: Event name to count handlers foroptions.global
: Include global handlers in count
Returns: Number of handlers
Example:
const eventCount = emitter.count('userLogin')
const totalCount = emitter.count('userLogin', { global: true })
Count global handlers
Returns: Number of global handlers
Example:
const globalCount = emitter.count()
Clear
Remove all event handlers and global handlers.
Example:
emitter.clear() // Removes all handlers
Set Options
Update emitter configuration after creation.
Parameters:
options.broadcastChannel
: Channel name for cross-tab communication
Example:
emitter.setOptions({ broadcastChannel: 'new-channel' })