Home Reference Source Repository
import UniversalEvents from 'universalevents'
public class | source

UniversalEvents

UniversalEvents is a class for managing events

Each event has a name, which is a string, which allows one UniversalEvents object to receive and coordinate multiple types of event.

There are 2 methods for listening for an event. The first is to attach a handler function with UniversalEvents#on. The second is to await an event with UniversalEvents#await which returns a Promise.

Events can then be raised with emit, optionally with arbitrary data which is passed to those listening for that event.

Constructor Summary

Public Constructor
public

constructor(validEvents: Set<string> | Array<string>)

Create a UniversalEvents object

Method Summary

Public Methods
public

addEventListener(eventName: string, handler: function(data: Object)): UniversalEvents

Listen for an event.
public

await(successEventName: string, failureEventName: string): Promise

Await an event for success and another event for failure.
public

emit(eventName: string, data: Object): boolean

Raise an event.
public

on(eventName: string, handler: function(data: Object)): UniversalEvents

Listen for an event.
public

raiseEvent(eventName: string, data: Object): boolean

Raise an event.
public

removeEventListener(eventName: string, handler: function(data: Object)): UniversalEvents

Remove a listener for an event.

Public Constructors

public constructor(validEvents: Set<string> | Array<string>) source

Create a UniversalEvents object

Params:

NameTypeAttributeDescription
validEvents Set<string> | Array<string>
  • optional
  • nullable: true

The set of events which this object should handle. If undefined, null, or empty, this object will handle all events.

Public Methods

public addEventListener(eventName: string, handler: function(data: Object)): UniversalEvents source

Listen for an event. Alias for UniversalEvents#on

Params:

NameTypeAttributeDescription
eventName string

The name of the event to listen for

handler function(data: Object)

The function which is called when the event is raised

Return:

UniversalEvents

Returns the UniversalEvents object, allowing calls to be chained

public await(successEventName: string, failureEventName: string): Promise source

Await an event for success and another event for failure.

Params:

NameTypeAttributeDescription
successEventName string

The name of the event which will resolve the promise when raised

failureEventName string

The name of the event which will reject the promise when raised

Return:

Promise

A promise which will be resolved on success and rejected on failure

public emit(eventName: string, data: Object): boolean source

Raise an event. Causes all listeners of this event to run. Alias of UniversalEvents#raiseEvent

Params:

NameTypeAttributeDescription
eventName string

The name of the event to raise

data Object
  • optional
  • nullable: true

An object passed to the handlers

Return:

boolean

A boolean representing whether the event had any handlers

public on(eventName: string, handler: function(data: Object)): UniversalEvents source

Listen for an event. Alias for UniversalEvents#addEventListener

Params:

NameTypeAttributeDescription
eventName string

The name of the event to listen for

handler function(data: Object)

The function which is called when the event is raised

Return:

UniversalEvents

Returns the UniversalEvents object, allowing calls to be chained

public raiseEvent(eventName: string, data: Object): boolean source

Raise an event. Causes all listeners of this event to run. Alias of UniversalEvents#emit

Params:

NameTypeAttributeDescription
eventName string

The name of the event to raise

data Object
  • optional
  • nullable: true

An object passed to the handlers

Return:

boolean

A boolean representing whether the event had any handlers

public removeEventListener(eventName: string, handler: function(data: Object)): UniversalEvents source

Remove a listener for an event. Be careful when doing this, you should only really remove listeners that were added by you.

Params:

NameTypeAttributeDescription
eventName string

The name of the event the handler was attached to

handler function(data: Object)

The function to remove

Return:

UniversalEvents

Returns the UniversalEvents object, allowing calls to be chained