API
Types
type EventType = string | symbol
type Handler<T> = (event?: T) => void
type WildcardHandler<T> = (type: EventType, event?: T) => voidEmitter
import { Emitter } from 'mittss'
const emitter = new Emitter(all?)Parameters
all
Map<record<EventType, unknown>>?A Map of event names to registered handler functions.
on
Register an event handler for the given type.
const onWildcard = (type, ev) => {}
emitter.on('*', onWildcard)
const onFoo = ev => {}
const remove = emitter.on('foo', onFoo)
// onFoo will be removed after calling remove() functionParameters
typeEventType | '*'Type of event to listen for, or
'*'for all eventshandlerHandler<T> | WildcardHandler<T>Function to call in response to given event
Returns
removeFunctionA function to remove the event handler
off
Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.
const onFoo = ev => {}
emitter.on('foo', onFoo)
emitter.off('foo', onFoo)Parameters
typeEventType | '*'Type of event to unregister
handlerfrom, or'*'handlerHandler<T> | WildcardHandler<T>?Handler function to remove
emit
Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.
Note: Manually firing '*' handlers is not supported.
const onFoo = ev => {} // ev: { foo: 'bar' }
emitter.on('foo', onFoo)
emitter.emit('foo', { foo: 'bar' })Parameters
typeEventTypeThe event type to invoke
eventany?Any value (object is recommended and powerful), passed to each handler
once
Invoke a handler for the given type. If present, '*' handlers areinvoked after type-matched handlers.
The handler will be removed after its first invoked.
const onFoo = ev => {} // ev: { foo: 'bar' }
emitter.once('foo', onFoo)
// emitter.all.get('foo') return [ Function onFoo]
emitter.emit('foo', { foo: 'bar' })
// emitter.all.get('foo') return []Parameters
typeEventType | '*'The event type to invoke
handlerHandler<T> | WildcardHandler<T>Function to call in response to given event