@valeera/eventdispatcher
Version:
Event dispatcher fit for most framework.
6 lines (5 loc) • 1.59 kB
JavaScript
var g=this;
function h(k){const l=(m=Object,n=[])=>class extends m{static l=l;i=n;filters=[];g=new Map;all(a){return this.filt(()=>!0,a)}clearListenersByKey(a){this.g.delete(a);return this}clearAllListeners(){const a=this.g.keys();for(const b of a)this.g.delete(b);return this}filt(a,b){this.filters.push({listener:b,m:a});return this}fire(a,b){if(!this.h(a))return console.error("EventDispatcher couldn't dispatch the event since EventKeyList doesn't contains key: ",a),this;const c=this.g.get(a)||[];let d=c.length,
e;for(let f=0;f<d;f++)e=c[f],e.listener(b),e.times--,0>=e.times&&(c.splice(f--,1),--d);return this.j(a,b)}off(a,b){a=this.g.get(a);if(!a)return this;const c=a.length;for(let d=0;d<c;d++)if(a[d].listener===b){a.splice(d,1);break}return this}on(a,b){if(a instanceof Array){for(let c=0,d=a.length;c<d;c++)this.times(a[c],Infinity,b);return this}return this.times(a,Infinity,b)}once(a,b){return this.times(a,1,b)}times(a,b,c){if(!this.h(a))return console.error("EventDispatcher couldn't add the listener: ",
c,"since EventKeyList doesn't contains key: ",a),this;const d=this.g.get(a)||[];this.g.has(a)||this.g.set(a,d);d.push({listener:c,times:b});return this}j(a,b){for(const c of this.filters)c.m(a,b)&&c.listener(b,a);return this}h(a){return this.i.length?this.i.includes(a):!0}};var p=l(Object);k["default"]=p;k.l=l;Object.defineProperty(k,"__esModule",{value:!0})}
"object"===typeof exports&&"undefined"!==typeof module?h(exports):"function"===typeof define&&define.o?define(["exports"],h):(g="undefined"!==typeof globalThis?globalThis:g||self,h(g.EventDispatcher={}));