EventsHub

shared/eventsHub. EventsHub

A minimal implementation of an events handler service.

Constructor

new EventsHub()

Source:
Tutorials:

Methods

emit(event, …args)

Source:

Emits an event and call all its listeners.

Parameters:
Name Type Attributes Description
event string | Array.<string>

An event name or a list of them.

args * <repeatable>

A list of parameters to send to the listeners.

off(event, fn) → {boolean|Array.<boolean>}

Source:

Removes an event listener.

Parameters:
Name Type Description
event string | Array.<string>

An event name or a list of them.

fn function

The listener function.

Returns:

If event was a string, it will return whether or not the listener was found and removed; but if event was an Array, it will return a list of boolean values.

Type
boolean | Array.<boolean>

on(event, fn) → {function}

Source:

Adds a new event listener.

Parameters:
Name Type Description
event string | Array.<string>

An event name or a list of them.

fn function

The listener function.

Returns:

An unsubscribe function to remove the listener or listeners.

Type
function

once(event, fn) → {function}

Source:

Adds an event listener that will only be executed once.

Parameters:
Name Type Description
event string | Array.<string>

An event name or a list of them.

fn function

The listener function.

Returns:

An unsubscribe function to remove the listener.

Type
function

reduce(event, target, …args) → {T}

Source:

Reduces a target using an event. It's like emit, but the events listener return a modified (or not) version of the target.

Parameters:
Name Type Attributes Description
event string | Array.<string>

An event name or a list of them.

target T

The variable to reduce with the listeners.

args * <repeatable>

A list of parameters to send to the listeners.

Returns:

A version of the target processed by the listeners.

Type
T

reduceAsync(event, target, …args) → {Promise.<T>}

Source:

Reduces a target using an event. It's like emit, but the events listener return a modified (or not) version of the target. This is the version async of reduce.

Parameters:
Name Type Attributes Description
event string | Array.<string>

An event name or a list of them.

target T

The variable to reduce with the listeners.

args * <repeatable>

A list of parameters to send to the listeners.

Returns:

A version of the target processed by the listeners.

Type
Promise.<T>

subscribers(event) → {Array.<function()>}

Source:

Gets all the listeners for an event.

Parameters:
Name Type Description
event string

The name of the event.

Returns:
Type
Array.<function()>