BB.EventEmitter

A class for providing a basic event-based programming interface. Often used as a base class extended by other classes that require event based programming patterns.

Constructor

BB.EventEmitter ()

Example:


// SHOULD LOG LIKE:
// Fired "now" event 1457470144
// seconds since the unix epoch.
// Fired "often" event 1457470145 seconds since the unix epoch.
// Fired "often" event 1457470146 seconds since the unix epoch.
// Fired "often" event 1457470147 seconds since the unix epoch.
// Fired "often" event 1457470148 seconds since the unix epoch.
// Fired "later" event 1457470149 seconds since the unix epoch.
// Removed "often" event.

 var emitter = new BB.EventEmitter();

 emitter.createEvent('now');
 emitter.createEvent('later');
 emitter.createEvent('often');

 // because this 'now' callback was registered first
 // it will be called first.
 emitter.on('now', function(unixTimestamp) {
     console.log('Fired "now" event ' + unixTimestamp);
 });

 // this one will be called second.
 emitter.on('now', function(unixTimestamp) {
     console.log('seconds since the unix epoch.');
 });

 emitter.on('often', function(unixTimestamp) {
     console.log('Fired "often" event ' + unixTimestamp + ' seconds since the unix epoch.');
 });

 emitter.on('later', function(unixTimestamp) {
     console.log('Fired "later" event ' + unixTimestamp + ' seconds since the unix epoch.');
     emitter.removeEvent('often');
     console.log('Removed "often" event.');
 });

 emitter.notify('now', Math.floor(Date.now() / 1000));

 setTimeout(function(){ emitter.notify('later', Math.floor(Date.now() / 1000)) }, 5000);
 setInterval(function(){ emitter.notify('often', Math.floor(Date.now() / 1000)) }, 1000);

Methods

createEvent
(
  • eventName
)

Create a new event. This event can then have callbacks registered to it with on() and trigger those callbacks with notify().

Note: if eventName has already been created this function does nothing.

Parameters:

  • eventName String

    The name of the event.

notify
(
  • eventName
  • [arguments]
)
Boolean

Notify an event by name, causing all callbacks registered to that event to be called in the order they were registered. Any arguments included in this function call after eventName will be passed directly to each callback that has been registered to this event.

Parameters:

  • eventName String

    The event to notify.

  • [arguments] Arguments optional

    Any arguments included in this function call after eventName will be passed directly to each callback that has been registered to this event.

Returns: Boolean

True if at least one callback function was called.

on
(
  • eventName
  • callback
)
Boolean

Register a callback function to an event.

Note: callback functions registered with on() will be called via notify() with a "this" variable bound to the event emitter itself independent of the function call context.

Parameters:

  • eventName String

    The name of the event to register the callback to.

  • callback Function

    The callback function to call when the event is notified.

Returns: Boolean

True if the callback was successfully registered to the event.

removeEvent
(
  • eventName
)
Boolean

Remove an event by name. This function also removes all registered callbacks to that event.

Parameters:

  • eventName String

    The name of the event to remove.

Returns: Boolean

True if the event existed and was removed.

removeEventCallbackAtIndex
(
  • eventName
  • index
)
type

Remove a callback registered to an event by index.

Parameters:

  • eventName type

    The name of the event to remove a callback from.

  • index type

    The index of that callback starting at 0 (counting up in the order the callbacks were registered). Negative numbers remove from the internal event array starting at the back (e.g. -1 removes the callback most recently registered).

Returns: type

Returns the callback function if one was succesfully removed or false otherwise.

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index