Hoopla
An event dispatcher for JavaScript that knows its priorities.
Node's event-emitter package is fine for simple events, but if you get more than a few listeners to a single event, it becomes difficult to manage. Hoopla allows you to set priorities for your event handlers so they get called in a certain order.
Creating a dispatcher
var Hoopla = ;var dispatcher = ;
Basic events
Add listeners with the addListener
method and fire events with dispatch
.
dispatcher; dispatcher;// Prints 'it was initialized!'
Event priority
Hoopla allows you to set an unlimited number of event listeners for any event. By default, they all have a priority of 0, so they get handled in the order they are registered. If you give a handler a negative or positive priority, Hoopla will call the handlers in order from least to greatest.
dispatcher; dispatcher; dispatcher; dispatcher;// Prints 'early init handler', 'default init handler', 'late init handler'
Event objects
Hoopla passes an Event
object to your event listeners. Events are objects that have a name, and attributes.
dispatcher;
If you pass an object as the second argument to dispatch
, Hoopla will set its properties
as attributes on the event object.
dispatcher; dispatcher;
Stopping propagation
Event listeners can stop propagation for an event meaning that event will not trigger any more listeners.
dispatcher; dispatcher; dispatcher;
Creating an event object
If you wish, you can create an event object manually and pass it to the dispatch method.
// Create an event with dispatcher.createEventvar eventA = dispatcher;// or create an event with the Event constructorvar eventB = 'bar' b: 'b'; dispatcher;dispatcher;
You can create custom events objects. An event object must have getName
and isPropagationStopped
methods.
var fooEvent = { return 'foo'; } { return false; };dispatcher;