mitt
is a lightweight event handling system designed to manage events and callbacks. It provides a set of concise APIs for registering event listeners, triggering events, removing listeners, and clearing all events.
- Registering Listeners: Register multiple triggers or single triggers for specific events.
- Triggering Events: Trigger registered events and pass parameters to listeners.
- Removing Listeners: Remove specific event listeners as needed.
- Clearing All Events: Remove all registered events and their listeners.
No separate installation is required. Simply import it directly into your project.
import mitt from '@vecal/mitt';
Registers an event listener, specifying the number of times it can trigger.
Parameters
-
eventName
(Key
): The event name. -
callback
(Fn
): The callback function to register. -
time
(number
): Optional parameter, specifies the maximum number of times the callback function can trigger. Default is infinite.
Registers a listener that triggers only once.
Parameters
-
eventName
(Key
): The event name. -
callback
(Fn
): The callback function to register.
Triggers an event and passes parameters to the listeners.
Parameters
-
eventName
(Key
): The event name. -
...args
(any[]
): Additional arguments passed to the callback functions.
Removes all listeners for a specific event.
Parameters
-
eventName
(Key
): The event name.
Removes a specific listener for a specific event.
Parameters
-
eventName
(Key
): The event name. -
callback
(Fn
): The callback function to remove.
Removes all registered events and their listeners.
const mitt = new Mitt();
const callback = (data) => {
console.log('Received update:', data);
};
// Register a listener
mitt.on('update', callback, 2); // Triggers only twice
// Register a one-time listener
mitt.once('finish', () => {
console.log('Task finished');
});
// Trigger the event
mitt.emit('update', { message: 'New data available' });
// Remove the listener
mitt.remove('update', callback);
// This will not remove the one-time listener
mitt.remove('finish', () => {
console.log('Task finished');
});
// Clear all events
mitt.clear();
new Mitt();
Registers an event listener, specifying the number of times it can trigger.
Parameters
-
eventName
(Key
): The event name. -
callback
(Fn
): The callback function to register. -
time
(number
): Optional parameter, specifies the maximum number of times the callback function can trigger. Default is infinite.
Registers a listener that triggers only once.
Parameters
-
eventName
(Key
): The event name. -
callback
(Fn
): The callback function to register.
Triggers an event and passes parameters to the listeners.
Parameters
-
eventName
(Key
): The event name. -
...args
(any[]
): Additional arguments passed to the callback functions.
Removes all listeners for a specific event.
Parameters
-
eventName
(Key
): The event name.
Removes a specific listener for a specific event.
Parameters
-
eventName
(Key
): The event name. -
callback
(Fn
): The callback function to remove.
Removes all registered events and their listeners.
This project is licensed under the MIT License. For details, please refer to the LICENSE file.