Skip to main content

JavaScript Event Handler

About

EventHandler class for JavaScript.

Constructor

use('EventHandler');
var eh = new EventHandler([string eventType, ...]);

Constructs a new EventHandler instance, and (optionally) starts subscribing to the events provided. Before using the EventHandler you must tell mod_v8 to load the EventHandler class.

Methods

subscribe

bool result = eh.subscribe([string eventType, ...]);

Add eventType(s) to subscribe for.

unSubscribe

bool result = eh.unSubscribe([string eventType, ...]);

Remove eventType(s) from subscribtion.

addFilter

bool result = eh.addFilter(string headerName, string headerValue);

Adds a filter to the subscription, this will allow to get only the events holding the header/value information as provided.

deleteFilter

bool result = eh.deleteFilter(string headerName);

Deletes a previously defined filter.

getEvent

Event evt = eh.getEvent([int timeoutMs]);

Receives an event. If timeout is given it will wait no more that the given time. If no event was received within the given time, null will be returned. If no timeout is given, it will wait here until it receives an event.

sendEvent

bool result = eh.sendEvent(Event event[, Session session | string sessionUUID]);

Sends an event to the FS event system, or if a session/uuid is provided, it will be sent to that specific session's command queue.

executeApi

string result = eh.executeApi(string cmd[, string arg]);

Executes an API command with optional arguments. Returns the result from the API.

executeBgApi

string jubUUID = eh.executeBgApi(string cmd[, string arg]);

Executes a background API command (in a separate thread) with optional arguments. Returns the job UUID. When execution is finished a BACKGROUND_JOB event will be fired, inside that event the job UUID value will be found.

destroy

eh.destroy();

Destroys and unsubscribes for all events.

Properties

ready

Readonly property, returns false if the instance has been destroyed, or true otherwise.

Example of event subscription

   /* This will subscribe for all events in the system, and loop forever */
use('EventHandler');

var eh = new EventHandler('ALL');
var evt;

while ((evt = eh.getEvent(60000))) {
consoleLog('info', 'Got event: ' + evt.serialize() + '\n');
}