A simple class-based JavaScript event dispatcher.
function onChange(name) {
console.log(name);
}
const events = new EventDispatcher();
events.on('change', onChange);
events.emit('change', 'Amanda');
Attaches an event handler function to the provided event. This method returns the instance of EventDispatcher
to allow for method chaining.
- event
String
- The type of event. - callback
Function
- The function to execute when the event is triggered.
Attaches an event handler function to the provided event that is executed, at most, once per event type. Once the event has been triggered the event handler automatically removes itself. This method returns the instance of EventDispatcher
to allow for method chaining.
- event
String
- The type of event. - callback
Function
- The function to execute when the event is triggered.
Removes event handlers. This method returns the instance of EventDispatcher
to allow for method chaining.
- event
String
- The type of event. - [callback]
Function
- The function to execute when the event is triggered. Ifcallback
is not passed, this method will remove all handlers for the providedevent
.
If neither event
or callback
are passed, this method will remove all event handlers.
Dispatches an event to the event handlers. This method returns the instance of EventDispatcher
to allow for method chaining.
- type
String
- The type of event. - [...args]
Mixed
- Optional arguments to be passed to the callback function.
Removes any events to allow for prompt garbage collection.
EventDispatcher.js
can be used freely for any open source or commercial works and is released under a MIT license.