97 lines
2.2 KiB
JavaScript
97 lines
2.2 KiB
JavaScript
"use strict";
|
|
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
import {Check} from "../Utils/Checks.ecma.js";
|
|
|
|
/**
|
|
* @class Event
|
|
* @constructor
|
|
* @param {boolean} [once = false]
|
|
* @param {boolean} [autoexecute = false]
|
|
* @returns {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
export const Event = (function(){
|
|
|
|
/**
|
|
* @callback event_callback
|
|
* @param {...(any|null)} parameters
|
|
* @return {void}
|
|
*/
|
|
|
|
/**
|
|
* @constructs Event
|
|
* @param {boolean} [once = false]
|
|
* @param {boolean} [autoexecute = false]
|
|
* @returns {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
const Event = function(once = false, autoexecute = false){
|
|
|
|
/** @type {Event} */
|
|
const self = this,
|
|
/** @type {Object.<number, event_callback>} */
|
|
events = {},
|
|
/** @type {number} */
|
|
id_i = 0;
|
|
/** @type {boolean} */
|
|
this.once = once;
|
|
/** @type {boolean} */
|
|
this.autoexecute = autoexecute;
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {};
|
|
|
|
/**
|
|
* @param {...(any|null)} parameters
|
|
* @returns {Array.<any|null>}
|
|
* @access public
|
|
*/
|
|
this.execute = (...parameters) => [...Object.entries(events)].map(([id, callback]) => {
|
|
|
|
/** @type {any|null} */
|
|
const response = Common.execute(callback, ...parameters);
|
|
|
|
if(self.once)
|
|
delete events[id];
|
|
|
|
return response;
|
|
});
|
|
|
|
/**
|
|
* @param {!event_callback} callback
|
|
* @returns {number|null}
|
|
* @access public
|
|
*/
|
|
this.add = callback => {
|
|
if(Check.is_function(callback)){
|
|
|
|
events[++ id_i] = callback;
|
|
|
|
return id_i;
|
|
};
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* @param {!number} id
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.remove = id => {
|
|
if(events[id]){
|
|
delete events[id];
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
};
|
|
|
|
return Event;
|
|
})(); |