@voliware/eventsystem
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -27,2 +27,19 @@ /** | ||
/** | ||
* Generate a random string | ||
* @param {number} length | ||
* @returns {string} | ||
*/ | ||
generateRandomString(length){ | ||
var s = ''; | ||
var randomchar = function() { | ||
var n = Math.floor(Math.random() * 62); | ||
if (n < 10) return n; //1-10 | ||
if (n < 36) return String.fromCharCode(n + 55); //A-Z | ||
return String.fromCharCode(n + 61); //a-z | ||
} | ||
while (s.length < length) s += randomchar(); | ||
return s; | ||
} | ||
/** | ||
* Get the number of handlers for an event. | ||
@@ -95,3 +112,3 @@ * Will look through all namespaced events as well. | ||
let self = this; | ||
let newEventName = event + "." + String.random(8); | ||
let newEventName = event + "." + this.generateRandomString(16); | ||
let newCallback = function(data) { | ||
@@ -175,2 +192,5 @@ callback(data); | ||
} | ||
} | ||
if(typeof module !== "undefined"){ | ||
module.exports = EventSystem; | ||
} |
@@ -1,1 +0,1 @@ | ||
class EventSystem{constructor(){return this.events={},this}getHandlersCount(e){let t=this.events[e];if(!t)return 0;let l=0,n=[];for(n.push(t);n.length>0;){let e=n.pop();for(let t in e)"_handlers"===t?l+=e[t].length:"object"==typeof e[t]&&n.push(e[t])}return l}on(e,t){let l=e.split("."),n=this.events;for(let e=0;e<l.length;e++){let s=l[e];n[s]||(n[s]={}),e===l.length-1?(Array.isArray(n[s]._handlers)||(n[s]._handlers=[]),n[s]._handlers.push(t)):n=n[s]}return this}one(e,t){let l=this,n=e+"."+String.random(8);return this.on(n,function(e){t(e),l.off(n)}),this}off(e,t=!0){let l=e.split("."),n=this.events;for(let e=0;e<l.length;e++){let s=l[e];e===l.length-1?t?delete n[s]:(delete n[s]._handlers,0===Object.keys(n[s]).length&&delete n[s]):n=n[s]}return this}emit(e,t){let l=e.split("."),n=this.events;for(let e=0;e<l.length;e++){if(!(n=n[l[e]]))return this;if(e===l.length-1){let e=[];for(e.push(n);e.length;){let l=e.pop();for(let n in l)if("_handlers"===n)for(let e=0;e<l[n].length;e++)l[n][e](t);else e.push(l[n])}}}return this}} | ||
class EventSystem{constructor(){return this.events={},this}generateRandomString(e){for(var t,n="";n.length<e;)n+=(t=void 0,(t=Math.floor(62*Math.random()))<10?t:t<36?String.fromCharCode(t+55):String.fromCharCode(t+61));return n}getHandlersCount(e){let t=this.events[e];if(!t)return 0;let n=0,r=[];for(r.push(t);r.length>0;){let e=r.pop();for(let t in e)"_handlers"===t?n+=e[t].length:"object"==typeof e[t]&&r.push(e[t])}return n}on(e,t){let n=e.split("."),r=this.events;for(let e=0;e<n.length;e++){let l=n[e];r[l]||(r[l]={}),e===n.length-1?(Array.isArray(r[l]._handlers)||(r[l]._handlers=[]),r[l]._handlers.push(t)):r=r[l]}return this}one(e,t){let n=this,r=e+"."+this.generateRandomString(16);return this.on(r,function(e){t(e),n.off(r)}),this}off(e,t=!0){let n=e.split("."),r=this.events;for(let e=0;e<n.length;e++){let l=n[e];e===n.length-1?t?delete r[l]:(delete r[l]._handlers,0===Object.keys(r[l]).length&&delete r[l]):r=r[l]}return this}emit(e,t){let n=e.split("."),r=this.events;for(let e=0;e<n.length;e++){if(!(r=r[n[e]]))return this;if(e===n.length-1){let e=[];for(e.push(r);e.length;){let n=e.pop();for(let r in n)if("_handlers"===r)for(let e=0;e<n[r].length;e++)n[r][e](t);else e.push(n[r])}}}return this}}"undefined"!=typeof module&&(module.exports=EventSystem); |
{ | ||
"name": "@voliware/eventsystem", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "An event system with on/off/emit functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9559
8
188