Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The mitt npm package is a tiny, functional event emitter or pub/sub library that provides a way to emit and listen for events in JavaScript applications. It is designed to be simple, small, and fast, making it suitable for use in both the browser and Node.js environments.
Event Emission
Emitting events with optional data. Listeners can subscribe to these events and react accordingly.
{"const mitt = require('mitt');\nconst emitter = mitt();\nemitter.emit('myEvent', { some: 'data' });"}
Event Listening
Subscribing to events. This allows functions to listen for specific events and execute code when those events are emitted.
{"const mitt = require('mitt');\nconst emitter = mitt();\nemitter.on('myEvent', (data) => {\n console.log('Event received with data:', data);\n});"}
Event Unlistening
Removing event listeners. This provides a way to unsubscribe from events when a listener is no longer needed or the context changes.
{"const mitt = require('mitt');\nconst emitter = mitt();\nconst handler = (data) => {\n console.log('Event received with data:', data);\n};\nemitter.on('myEvent', handler);\nemitter.off('myEvent', handler);"}
All Events Listening
Listening to all events. This special event type allows a listener to react to all events emitted by the emitter.
{"const mitt = require('mitt');\nconst emitter = mitt();\nemitter.on('*', (type, event) => {\n console.log(`Type: ${type}, Event:`, event);\n});"}
EventEmitter3 is a high-performance event emitter. It is similar to mitt but provides more features like context binding and wildcard listeners. It is also slightly larger in size compared to mitt.
TinyEmitter is another small event emitter library that offers basic event emitting and listening functionality. It is comparable to mitt in terms of size and simplicity but does not support wildcard listeners.
The 'events' package is Node.js's EventEmitter class for the browser. It is more feature-rich than mitt, including asynchronous event emitting and more complex event handling capabilities, but it is also larger in size.
mitt
Tiny (~200b) functional event emitter / pubsub.
It's tiny: no dependencies and only 190 bytes when gzipped (250b without).
npm install --save mitt
Mitt: Tiny (~200b) functional event emitter / pubsub.
Returns Mitt
Register an event handler for the given type.
Parameters
type
String Type of event to listen for, or "*"
for all eventshandler
Function Function to call in response to the given eventRemove an event handler for the given type.
Parameters
type
String Type of event to unregister handler
from, or "*"
handler
Function Handler function to removeInvoke all handlers for the given type.
If present, "*"
handlers are invoked prior to type-matched handlers.
Parameters
type
String The event type to invokeevent
[Any] An event object, passed to each handlerFAQs
Tiny 200b functional Event Emitter / pubsub.
We found that mitt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.