Enhanced tiny (~400b) functional event emitter / pubsub with TypeScript support.
- Microscopic: Maintains a small footprint (~400 bytes gzipped)
- Useful: Wildcard "*" event type listens to all events
- Familiar: Same API as Node's EventEmitter and original Mitt
- Functional: Methods don't rely on this
- TypeScript Ready: First-class TypeScript support with generic types
- Enhanced API: Adds useful methods like once(), clear(), hasListeners(), and listenerCount()
- Safer Emission: Protects against modification of handlers during emission
This package includes TypeScript definitions. For best type inference, set "strict": true in your tsconfig.json.
Creates a mitt event emitter instance.
Returns Emitter
A Map of event names to registered handler functions.
emitter.on(type, handler)
Register an event handler for the given type.
- type (String|Symbol): Type of event to listen for, or '*' for all events
- handler (Function): Function to call in response to the event
emitter.off(type, [handler])
Remove an event handler for the given type.
- type (String|Symbol): Type of event to unregister handler from, or '*'
- handler (Function) [optional]: Handler function to remove
If handler is omitted, all handlers of the given type are removed.
emitter.emit(type, event)
Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.
- type (String|Symbol): The event type to invoke
- event (Any): Any value (object is recommended and powerful), passed to each handler
emitter.once(type, handler)
Register an event handler that will be called only once.
- type (String|Symbol): Type of event to listen for, or '*' for all events
- handler (Function): Function to call in response to the event
Remove all event handlers.
emitter.hasListeners(type)
Check if there are any listeners for a given event type.
- type (String|Symbol): Type of event to check
- Returns (Boolean): true if there are listeners for this event type
emitter.listenerCount(type)
Get the number of listeners for a given event type.
- type (String|Symbol): Type of event to check
- Returns (Number): Number of listeners for this event type
This package works in all modern browsers and IE11+.
If you're forking this package and want to publish your own version, follow these steps:
- Create an npm account if you don't already have one
- Update the package name, author, and repository information in package.json
- Login to npm:
- Publish the package:
For more information about publishing to npm, see the npm documentation.
MIT