2021-06-18 02:10:32 +02:00
|
|
|
const Utils = require('./r3-utils');
|
2020-04-23 15:26:54 +02:00
|
|
|
|
|
|
|
class Event {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
console.log('Event created');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some nice Events handling
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
static Subscriptions = {};
|
|
|
|
|
|
|
|
static Subscribe(
|
|
|
|
eventName,
|
|
|
|
fn
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* Todo - maybe eventually store a boolean which indicates if the function has been executed
|
|
|
|
*/
|
|
|
|
let subscriptionId = Utils.RandomId(10);
|
|
|
|
|
|
|
|
if (Event.Subscriptions.hasOwnProperty(eventName)) {
|
|
|
|
|
|
|
|
if (Event.Subscriptions[eventName][subscriptionId]) {
|
|
|
|
throw new Error('A component can only subscribe to a particular event ID once');
|
|
|
|
}
|
|
|
|
|
|
|
|
Event.Subscriptions[eventName][subscriptionId] = fn;
|
|
|
|
} else {
|
|
|
|
Event.Subscriptions[eventName] = {};
|
|
|
|
Event.Subscriptions[eventName][subscriptionId] = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a handle to the caller to allow us to unsubscribe to this event
|
|
|
|
*/
|
|
|
|
return {
|
|
|
|
fn: fn,
|
|
|
|
remove: function (eventId, subscriptionId) {
|
|
|
|
|
|
|
|
return function () {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop listening for this event from this component
|
|
|
|
*/
|
|
|
|
delete Event.Subscriptions[eventId][subscriptionId];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the length of listeners is 0, stop referencing this event
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
let listeners = Object.keys(Event.Subscriptions[eventId]);
|
|
|
|
if (listeners.length === 0) {
|
|
|
|
delete Event.Subscriptions[eventId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}(eventName, subscriptionId),
|
|
|
|
subscriptionId : subscriptionId
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to some events
|
|
|
|
* @param eventName
|
|
|
|
* @param callback
|
|
|
|
*/
|
|
|
|
subscribe(
|
|
|
|
eventName,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
return Event.Subscribe(eventName, callback.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static Synchronous Event - Calls clientCallback directly after the event result is obtained
|
|
|
|
* @param eventId
|
|
|
|
* @param data
|
|
|
|
* @param clientCallback is executed ideally when the event completed
|
|
|
|
* @param clientErrorCallback
|
|
|
|
* @returns {number} of callbacks executed
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
static Emit(
|
|
|
|
eventId,
|
|
|
|
data,
|
|
|
|
clientCallback,
|
|
|
|
clientErrorCallback
|
|
|
|
) {
|
|
|
|
if (Event.Subscriptions.hasOwnProperty(eventId)) {
|
|
|
|
|
|
|
|
let subscriptionIds = Object.keys(Event.Subscriptions[eventId]);
|
|
|
|
|
|
|
|
subscriptionIds.map(
|
|
|
|
function(subscriptionId) {
|
|
|
|
try {
|
|
|
|
let result = Event.Subscriptions[eventId][subscriptionId](data);
|
|
|
|
|
|
|
|
if (clientCallback) {
|
|
|
|
clientCallback(result);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (clientErrorCallback) {
|
|
|
|
clientErrorCallback(error);
|
|
|
|
} else {
|
|
|
|
console.error(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(
|
|
|
|
eventName,
|
|
|
|
data,
|
|
|
|
clientCallback,
|
|
|
|
clientErrorCallback
|
|
|
|
) {
|
|
|
|
return Event.Emit(
|
|
|
|
eventName,
|
|
|
|
data,
|
|
|
|
clientCallback,
|
|
|
|
clientErrorCallback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the functions which subscribe to this event, but don't process the client callback - the subscription function
|
|
|
|
* should execute the client callback
|
|
|
|
* @param eventId
|
|
|
|
* @param data
|
|
|
|
* @param clientCallback
|
|
|
|
* @param clientErrorCallback
|
|
|
|
* @returns {number}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
static Async(
|
|
|
|
eventId,
|
|
|
|
data,
|
|
|
|
clientCallback,
|
|
|
|
clientErrorCallback
|
|
|
|
) {
|
|
|
|
if (Event.Subscriptions.hasOwnProperty(eventId)) {
|
|
|
|
|
|
|
|
let subscriptionIds = Object.keys(Event.Subscriptions[eventId]);
|
|
|
|
|
|
|
|
subscriptionIds.map(
|
|
|
|
function(subscriptionId) {
|
|
|
|
try {
|
|
|
|
Event.Subscriptions[eventId][subscriptionId](data, clientCallback, clientErrorCallback);
|
|
|
|
} catch (error) {
|
|
|
|
if (clientErrorCallback) {
|
|
|
|
clientErrorCallback(error);
|
|
|
|
} else {
|
|
|
|
console.error(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-18 02:10:32 +02:00
|
|
|
Event.OBJECT_CREATED = 0x1;
|
|
|
|
|
2020-04-23 15:26:54 +02:00
|
|
|
module.exports = Event;
|