Class: CustomEvents

CustomEvents(debugopt)

new CustomEvents(debugopt)

JavaScript regular and custom events abstraction

The CustomEvents class provides an abstraction of JavaScript event listener, to allow easy binding and removing those events. It also provides an interface to register custom events. This class is meant to be used on all scopes you need ; module or global. Refer to each public method for detailed features. For source code, please go to https://github.com/ArthurBeaulieu/CustomEvents.js
Parameters:
Name Type Attributes Default Description
debug boolean <optional>
false

Debug flag ; when true, logs will be output in JavaScript console at each event

Since:
  • June 2020
Author:
  • Arthur Beaulieu
Source:

Members

(private) _customEvents :object

We store custom events by name as key, each key stores an Array of subscribed events

Type:
  • object
Source:

(private) _debug :boolean

Internal logging flag from constructor options, allow to output each event action

Type:
  • boolean
Source:

(private) _idIncrementor :number

Start the ID incrementer at pseudo random value, used for both regular and custom events

Type:
  • number
Source:

(private) _regularEvents :Array.<any>

We store classical event listeners in array of objects containing all their information

Type:
  • Array.<any>
Source:

version :string

Component version

Type:
  • string
Source:

Methods

(private, static) _clearRegularEvent(index) → {boolean}

_clearRegularEvent method remove the saved event listener for a given index in regularEvents array range.
Parameters:
Name Type Description
index number

The regular event index to remove from class attributes

Source:
Returns:
  • The method status ; true for success, false for not cleared any event
Type
boolean

(private, static) _raise(level, errorValue)

Internal method to abstract console wrapped in debug flag./blockquote>
Parameters:
Name Type Description
level string

The console method to call

errorValue string

The error value to display in console method

Source:

(static) addEvent(eventName, element, callback, scopeopt, optionsopt) → {number|boolean}

addEvent method abstracts the addEventListener method to easily remove it when needed, also to set a custom scope on callback.
Parameters:
Name Type Attributes Default Description
eventName string

The event name to fire (mousemove, click, context etc.)

element object

The DOM element to attach the listener to

callback function

The callback function to execute when event is realised

scope object <optional>
element

The event scope to apply to the callback (optional, default to DOM element)

options object | boolean <optional>
false

The event options (useCapture and else)

Source:
Returns:
  • The event ID to use to manually remove an event, false if arguments are invalid
Type
number | boolean

(static) destroy()

CustomEvents destructor. Will remove all event listeners and keys in instance.
Source:

(static) publish(eventName, dataopt) → {boolean}

Publish method allow you to fire an event by name and trigger all its subscription by callbacks./blockquote>
Parameters:
Name Type Attributes Description
eventName string

Event name (the one to use to publish)

data object <optional>

The data object to sent through the custom event

Source:
Returns:
  • The method status ; true for success, false for non-existing event
Type
boolean

(static) removeAllEvents() → {boolean}

Clear all event listener registered through this class object.
Source:
Returns:
  • The method status ; true for success, false for not removed any event
Type
boolean

(static) removeEvent(eventId) → {boolean}

removeEvent method abstracts the removeEventListener method to really remove event listeners.
Parameters:
Name Type Description
eventId number

The event ID to remove listener from. Returned when addEvent is called

Source:
Returns:
  • The method status ; true for success, false for non-existing event
Type
boolean

(static) subscribe(eventName, callback, oneShotopt) → {number|boolean}

Subscribe method allow you to listen to an event and react when it occurs.
Parameters:
Name Type Attributes Default Description
eventName string

Event name (the one to use to publish)

callback function

The callback to execute when event is published

oneShot boolean <optional>
false

One shot : to remove subscription the first time callback is fired

Source:
Returns:
  • The event id, to be used when manually unsubscribing
Type
number | boolean

(static) unsubscribe(eventId) → {boolean}

Unsubscribe method allow you to revoke an event subscription from its string name.
Parameters:
Name Type Description
eventId number

The subscription id returned when subscribing to an event name

Source:
Returns:
  • The method status ; true for success, false for non-existing subscription
Type
boolean

(static) unsubscribeAllFor(eventName) → {boolean}

unsubscribeAllFor method clear all subscriptions registered for given event name.
Parameters:
Name Type Description
eventName string

The event to clear subscription from

Source:
Returns:
  • The method status ; true for success, false for non-existing event
Type
boolean