r3-v2/dist/r3-node/r3-event.js

469 lines
12 KiB
JavaScript

//GENERATED_IMPORTS_START
//GENERATED_IMPORTS_END
//CUSTOM_IMPORTS_START
const Utils = require('./r3-utils.js');
//CUSTOM_IMPORTS_END
/**
GENERATED_INHERITED_START
GENERATED_INHERITED_END
TEMPLATE_OPTIONS_START
TEMPLATE_OPTIONS_END
CUSTOM_OPTIONS_START
CUSTOM_OPTIONS_END
TEMPLATE_STATIC_OPTIONS_START
TEMPLATE_STATIC_OPTIONS_END
CUSTOM_STATIC_OPTIONS_START
CUSTOM_STATIC_OPTIONS_END
TEMPLATE_METHODS_START
TEMPLATE_METHODS_END
CUSTOM_METHODS_START
async(eventId, data, clientCallback, clientErrorCallback) - Simply calls 'Async()' passing it the arguments
emit(eventId, data, clientCallback, clientErrorCallback) - Simply calls 'Emit()' passing it the arguments
subscribe(eventId, callback) - Simply calls 'Subscribe()' passing it the arguments
CUSTOM_METHODS_END
TEMPLATE_STATIC_METHODS_START
TEMPLATE_STATIC_METHODS_END
CUSTOM_STATIC_METHODS_START
Async(eventId, data, clientCallback, clientErrorCallback) - Calls all subscription functions registered to eventId with data, clientCallback and clientErrorCallback as arguments. If an error occurs during clientCallback it additionally will execute clientErrorCallback with the error as argument.
Emit(eventId, data, clientCallback, clientErrorCallback) - Calls all subscription functions registered to eventId with data as arg. Calls clientCallback directly after the event result is obtained, passing it the result. If an exception occurs during execution, the clientErrorCallback is called with the error as argument.
Subscribe(eventId, callback) - Subscribes to 'eventName', ex. Event.BEFORE_RENDER and executes 'callback()' when eventName is raised @return {Object} - A handle to the subscription which can be removed by calling handle.remove()
CUSTOM_STATIC_METHODS_END
**/
class Event {
//GENERATED_CONSTRUCTOR_START
constructor(options) {
if (typeof options === 'undefined') {
options = {};
}
//GENERATED_TEMPLATE_OPTIONS_INIT_START
//GENERATED_TEMPLATE_OPTIONS_INIT_END
//GENERATED_OPTIONS_INIT_START
//GENERATED_OPTIONS_INIT_END
//CUSTOM_OPTIONS_INIT_START
//CUSTOM_OPTIONS_INIT_END
Object.assign(this, options);
//CUSTOM_BEFORE_INIT_START
//CUSTOM_BEFORE_INIT_END
//CUSTOM_AFTER_INIT_START
//CUSTOM_AFTER_INIT_END
}
//GENERATED_CONSTRUCTOR_END
//GENERATED_TEMPLATE_METHODS_START
//GENERATED_TEMPLATE_METHODS_END
//GENERATED_METHODS_START
/**
* async()
* - Simply calls 'Async()' passing it the arguments
* @param eventId
* @param data
* @param clientCallback
* @param clientErrorCallback
*/
async(
eventId,
data,
clientCallback,
clientErrorCallback
) {
//GENERATED_ASYNC_METHOD_START
//GENERATED_ASYNC_METHOD_END
//CUSTOM_ASYNC_METHOD_START
return Event.Async(
eventId,
data,
clientCallback,
clientErrorCallback
);
//CUSTOM_ASYNC_METHOD_END
//GENERATED_ASYNC_METHOD_AFTER_START
//GENERATED_ASYNC_METHOD_AFTER_END
}
/**
* emit()
* - Simply calls 'Emit()' passing it the arguments
* @param eventId
* @param data
* @param clientCallback
* @param clientErrorCallback
*/
emit(
eventId,
data,
clientCallback,
clientErrorCallback
) {
//GENERATED_EMIT_METHOD_START
//GENERATED_EMIT_METHOD_END
//CUSTOM_EMIT_METHOD_START
return Event.Emit(
eventId,
data,
clientCallback,
clientErrorCallback
);
//CUSTOM_EMIT_METHOD_END
//GENERATED_EMIT_METHOD_AFTER_START
//GENERATED_EMIT_METHOD_AFTER_END
}
/**
* subscribe()
* - Simply calls 'Subscribe()' passing it the arguments
* @param eventId
* @param callback
*/
subscribe(
eventId,
callback
) {
//GENERATED_SUBSCRIBE_METHOD_START
//GENERATED_SUBSCRIBE_METHOD_END
//CUSTOM_SUBSCRIBE_METHOD_START
return Event.Subscribe(eventId, callback.bind(this));
//CUSTOM_SUBSCRIBE_METHOD_END
//GENERATED_SUBSCRIBE_METHOD_AFTER_START
//GENERATED_SUBSCRIBE_METHOD_AFTER_END
}
//GENERATED_METHODS_END
//GENERATED_TEMPLATE_STATIC_METHODS_START
//GENERATED_TEMPLATE_STATIC_METHODS_END
//GENERATED_STATIC_METHODS_START
/**
* Async()
* - Calls all subscription functions registered to eventId with data, clientCallback and clientErrorCallback as
* arguments. If an error occurs during clientCallback it additionally will execute clientErrorCallback with the
* error as argument.
* @param eventId
* @param data
* @param clientCallback
* @param clientErrorCallback
*/
static Async(
eventId,
data,
clientCallback,
clientErrorCallback
) {
//GENERATED_STATIC_ASYNC_METHOD_START
//GENERATED_STATIC_ASYNC_METHOD_END
//CUSTOM_STATIC_ASYNC_METHOD_START
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;
}
}
}
)
}
//CUSTOM_STATIC_ASYNC_METHOD_END
}
/**
* Emit()
* - Calls all subscription functions registered to eventId with data as arg. Calls clientCallback directly after
* the event result is obtained, passing it the result. If an exception occurs during execution, the
* clientErrorCallback is called with the error as argument.
* @param eventId
* @param data
* @param clientCallback
* @param clientErrorCallback
*/
static Emit(
eventId,
data,
clientCallback,
clientErrorCallback
) {
//GENERATED_STATIC_EMIT_METHOD_START
//GENERATED_STATIC_EMIT_METHOD_END
//CUSTOM_STATIC_EMIT_METHOD_START
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;
}
}
}
)
} else {
if (clientCallback) {
clientCallback();
}
}
//CUSTOM_STATIC_EMIT_METHOD_END
}
/**
* Subscribe()
* - Subscribes to 'eventName', ex. Event.BEFORE_RENDER and executes 'callback()' when eventName is raised
* @param eventId
* @param callback
* @returns {Object} - A handle to the subscription which can be removed by calling handle.remove()
*/
static Subscribe(
eventId,
callback
) {
//GENERATED_STATIC_SUBSCRIBE_METHOD_START
//GENERATED_STATIC_SUBSCRIBE_METHOD_END
//CUSTOM_STATIC_SUBSCRIBE_METHOD_START
let subscriptionId = Utils.RandomId(10);
if (Event.Subscriptions.hasOwnProperty(eventId)) {
if (Event.Subscriptions[eventId][subscriptionId]) {
throw new Error('A component can only subscribe to a particular event ID once');
}
Event.Subscriptions[eventId][subscriptionId] = callback;
} else {
Event.Subscriptions[eventId] = {};
Event.Subscriptions[eventId][subscriptionId] = callback;
}
/**
* Return a handle to the caller to allow us to unsubscribe to this event
*/
return {
fn: callback,
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];
}
return true;
}
}(eventId, subscriptionId),
subscriptionId : subscriptionId
};
//CUSTOM_STATIC_SUBSCRIBE_METHOD_END
}
//GENERATED_STATIC_METHODS_END
//CUSTOM_IMPLEMENTATION_START
/**
* Some nice Events handling
* @type {{}}
*/
static Subscriptions = {};
//CUSTOM_IMPLEMENTATION_END
}
//GENERATED_TEMPLATE_STATIC_OPTIONS_INIT_START
//GENERATED_TEMPLATE_STATIC_OPTIONS_INIT_END
//GENERATED_STATIC_OPTIONS_INIT_START
//GENERATED_STATIC_OPTIONS_INIT_END
//GENERATED_OUT_OF_CLASS_IMPLEMENTATION_START
//GENERATED_OUT_OF_CLASS_IMPLEMENTATION_END
//CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_START
//GENERATED_EVENTS_START
Event.BEFORE_RENDER = 0x1;
Event.BLACKLISTED_COMPONENT_INSTANCE_REQUEST = 0x2;
Event.COMPONENT_CREATED = 0x3;
Event.COMPONENT_INITIALIZED = 0x4;
Event.CREATE_INSTANCE_BEFORE = 0x5;
Event.DISPOSE_INSTANCE = 0x6;
Event.DISPOSE_OBJECT = 0x7;
Event.DOM_COMPONENT_INITIALIZED = 0x8;
Event.ENTITY_CREATED = 0x9;
Event.ENTITY_INITIALIZED = 0xa;
Event.ENTITY_STARTED = 0xb;
Event.FINAL_INITIALIZE = 0xc;
Event.GET_API_URL = 0xd;
Event.GET_RUNTIME = 0xe;
Event.GET_WINDOW_SIZE = 0xf;
Event.GRAPHICS_COMPONENT_INITIALIZED = 0x10;
Event.IMAGE_INSTANCE_CREATED = 0x11;
Event.INPUT_COMPONENT_INITIALIZED = 0x12;
Event.INSTANCE_CREATED = 0x13;
Event.INSTANCE_DISPOSED = 0x14;
Event.KEYBOARD_DOWN = 0x15;
Event.KEYBOARD_UP = 0x16;
Event.MOUSE_DOWN = 0x17;
Event.MOUSE_MOVE = 0x18;
Event.MOUSE_UP = 0x19;
Event.MOUSE_WHEEL = 0x1a;
Event.OBJECT_CREATED = 0x1b;
Event.OBJECT_PROPERTY_UPDATE = 0x1c;
Event.OBJECT_PROPERTY_UPDATED = 0x1d;
Event.ON_READY_STATE_CHANGE = 0x1e;
Event.PAUSE = 0x1f;
Event.PROJECT_INITIALIZED = 0x20;
Event.RESTART = 0x21;
Event.RUNTIME_CREATED = 0x22;
Event.SLIDER_ENTITY_INITIALIZED = 0x23;
Event.START = 0x24;
Event.TOUCH_CANCEL = 0x25;
Event.TOUCH_END = 0x26;
Event.TOUCH_MOVE = 0x27;
Event.TOUCH_START = 0x28;
Event.UPDATE_FROM_INSTANCE_AFTER = 0x29;
Event.UPDATE_FROM_INSTANCE_BEFORE = 0x2a;
Event.UPDATE_INSTANCE_AFTER = 0x2b;
Event.UPDATE_INSTANCE_BEFORE = 0x2c;
Event.UPDATE_INSTANCE_PROPERTY = 0x2d;
Event.UPDATE_PROPERTY_FROM_INSTANCE = 0x2e;
Event.MAX_EVENTS = 0x2f;
Event.GetEventName = function(eventId) {
switch(eventId) {
case 0x1 : return 'before_render';
case 0x2 : return 'blacklisted_component_instance_request';
case 0x3 : return 'component_created';
case 0x4 : return 'component_initialized';
case 0x5 : return 'create_instance_before';
case 0x6 : return 'dispose_instance';
case 0x7 : return 'dispose_object';
case 0x8 : return 'dom_component_initialized';
case 0x9 : return 'entity_created';
case 0xa : return 'entity_initialized';
case 0xb : return 'entity_started';
case 0xc : return 'final_initialize';
case 0xd : return 'get_api_url';
case 0xe : return 'get_runtime';
case 0xf : return 'get_window_size';
case 0x10 : return 'graphics_component_initialized';
case 0x11 : return 'image_instance_created';
case 0x12 : return 'input_component_initialized';
case 0x13 : return 'instance_created';
case 0x14 : return 'instance_disposed';
case 0x15 : return 'keyboard_down';
case 0x16 : return 'keyboard_up';
case 0x17 : return 'mouse_down';
case 0x18 : return 'mouse_move';
case 0x19 : return 'mouse_up';
case 0x1a : return 'mouse_wheel';
case 0x1b : return 'object_created';
case 0x1c : return 'object_property_update';
case 0x1d : return 'object_property_updated';
case 0x1e : return 'on_ready_state_change';
case 0x1f : return 'pause';
case 0x20 : return 'project_initialized';
case 0x21 : return 'restart';
case 0x22 : return 'runtime_created';
case 0x23 : return 'slider_entity_initialized';
case 0x24 : return 'start';
case 0x25 : return 'touch_cancel';
case 0x26 : return 'touch_end';
case 0x27 : return 'touch_move';
case 0x28 : return 'touch_start';
case 0x29 : return 'update_from_instance_after';
case 0x2a : return 'update_from_instance_before';
case 0x2b : return 'update_instance_after';
case 0x2c : return 'update_instance_before';
case 0x2d : return 'update_instance_property';
case 0x2e : return 'update_property_from_instance';
default :
throw new Error('Event type not defined : ' + eventId);
}
};
//GENERATED_EVENTS_END
//CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_END
module.exports = Event;