eventemitter-super
Version:
An event emitter/listener tool based on EventEmitter3.
2 lines (1 loc) • 8.24 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).EventTool={})}(this,(function(t){"use strict";var e=function(t,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},e(t,n)};function n(t,e,n){if(n||2===arguments.length)for(var o,r=0,i=e.length;r<i;r++)!o&&r in e||(o||(o=Array.prototype.slice.call(e,0,r)),o[r]=e[r]);return t.concat(o||Array.prototype.slice.call(e))}function o(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}"function"==typeof SuppressedError&&SuppressedError;var r,i={exports:{}};var c=(r||(r=1,function(t){var e=Object.prototype.hasOwnProperty,n="~";function o(){}function r(t,e,n){this.fn=t,this.context=e,this.once=n||!1}function i(t,e,o,i,c){if("function"!=typeof o)throw new TypeError("The listener must be a function");var s=new r(o,i||t,c),u=n?n+e:e;return t._events[u]?t._events[u].fn?t._events[u]=[t._events[u],s]:t._events[u].push(s):(t._events[u]=s,t._eventsCount++),t}function c(t,e){0==--t._eventsCount?t._events=new o:delete t._events[e]}function s(){this._events=new o,this._eventsCount=0}Object.create&&(o.prototype=Object.create(null),(new o).__proto__||(n=!1)),s.prototype.eventNames=function(){var t,o,r=[];if(0===this._eventsCount)return r;for(o in t=this._events)e.call(t,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(t)):r},s.prototype.listeners=function(t){var e=n?n+t:t,o=this._events[e];if(!o)return[];if(o.fn)return[o.fn];for(var r=0,i=o.length,c=new Array(i);r<i;r++)c[r]=o[r].fn;return c},s.prototype.listenerCount=function(t){var e=n?n+t:t,o=this._events[e];return o?o.fn?1:o.length:0},s.prototype.emit=function(t,e,o,r,i,c){var s=n?n+t:t;if(!this._events[s])return!1;var u,f,p=this._events[s],a=arguments.length;if(p.fn){switch(p.once&&this.removeListener(t,p.fn,void 0,!0),a){case 1:return p.fn.call(p.context),!0;case 2:return p.fn.call(p.context,e),!0;case 3:return p.fn.call(p.context,e,o),!0;case 4:return p.fn.call(p.context,e,o,r),!0;case 5:return p.fn.call(p.context,e,o,r,i),!0;case 6:return p.fn.call(p.context,e,o,r,i,c),!0}for(f=1,u=new Array(a-1);f<a;f++)u[f-1]=arguments[f];p.fn.apply(p.context,u)}else{var l,h=p.length;for(f=0;f<h;f++)switch(p[f].once&&this.removeListener(t,p[f].fn,void 0,!0),a){case 1:p[f].fn.call(p[f].context);break;case 2:p[f].fn.call(p[f].context,e);break;case 3:p[f].fn.call(p[f].context,e,o);break;case 4:p[f].fn.call(p[f].context,e,o,r);break;default:if(!u)for(l=1,u=new Array(a-1);l<a;l++)u[l-1]=arguments[l];p[f].fn.apply(p[f].context,u)}}return!0},s.prototype.on=function(t,e,n){return i(this,t,e,n,!1)},s.prototype.once=function(t,e,n){return i(this,t,e,n,!0)},s.prototype.removeListener=function(t,e,o,r){var i=n?n+t:t;if(!this._events[i])return this;if(!e)return c(this,i),this;var s=this._events[i];if(s.fn)s.fn!==e||r&&!s.once||o&&s.context!==o||c(this,i);else{for(var u=0,f=[],p=s.length;u<p;u++)(s[u].fn!==e||r&&!s[u].once||o&&s[u].context!==o)&&f.push(s[u]);f.length?this._events[i]=1===f.length?f[0]:f:c(this,i)}return this},s.prototype.removeAllListeners=function(t){var e;return t?(e=n?n+t:t,this._events[e]&&c(this,e)):(this._events=new o,this._eventsCount=0),this},s.prototype.off=s.prototype.removeListener,s.prototype.addListener=s.prototype.on,s.prefixed=n,s.EventEmitter=s,t.exports=s}(i)),i.exports),s=function(t){function o(){return null!==t&&t.apply(this,arguments)||this}return function(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function o(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(o.prototype=n.prototype,new o)}(o,t),o.prototype.on=function(e,n,o){var r=this;return t.prototype.on.call(this,e,n,o),function(){r.off(e,n,o)}},o.prototype.addListener=function(e,n,o){var r=this;return t.prototype.addListener.call(this,e,n,o),function(){r.removeListener(e,n,o)}},o.prototype.once=function(e,n,o){var r=this;return t.prototype.once.call(this,e,n,o),function(){r.off(e,n,o,!0)}},o.prototype.onDebounce=function(t,e,n,o){return e=this._debounce(e,n),this.on(t,e,o)},o.prototype.onThrottle=function(t,e,n,o){return e=this._throttle(e,n),this.on(t,e,o)},o.prototype.onDebounceHtmlEvent=function(t,e,n,o,r){return n=this._debounce(n,o),this.addHtmlListener(t,e,n,r)},o.prototype.onThrottleHtmlEvent=function(t,e,n,o,r){return n=this._throttle(n,o),this.addHtmlListener(t,e,n,r)},o.prototype.onHtmlEvent=function(t,e,n,o){return this.addHtmlListener(t,e,n,o)},o.prototype.addHtmlListener=function(t,e,n,o){return t.addEventListener(e,n,o),function(){t.removeEventListener(e,n,o)}},o.prototype.onceEvents=function(t,e,n){return this.addOnceListeners(t,e,n)},o.prototype.addOnceListeners=function(t,e,o){var r=this,i=n([],t,!0),c=Array.from(new Set(i)),s=[];return c.forEach((function(t){var n=r.on(t,(function(){for(var c=[],u=0;u<arguments.length;u++)c[u]=arguments[u];if(i.splice(i.indexOf(t),1),!i.includes(t)){var f=s.indexOf(n);f>-1&&s.splice(f,1),n()}0===i.length&&e.apply(o||r,c)}),o);s.push(n)})),function(){s.forEach((function(t){t()}))}},o.prototype.bindOnce=function(t,e,n){return this.once(t,e,n)},o.prototype.bind=function(t,e,n){return this.on(t,e,n)},o.prototype.fire=function(e){for(var o=[],r=1;r<arguments.length;r++)o[r-1]=arguments[r];return t.prototype.emit.apply(this,n([e],o,!1))},o.prototype.unbind=function(e,n,o,r){return t.prototype.off.call(this,e,n,o,r)},o.prototype.un=function(e,n,o,r){return t.prototype.off.call(this,e,n,o,r)},o.prototype.onceByExecCount=function(t,e,n,o){var r=this;void 0===n&&(n=1);var i=this.on(t,(function(){for(var t=[],c=0;c<arguments.length;c++)t[c]=arguments[c];0===--n&&(e.apply(o||r,t),i())}),o);return i},o.prototype.onceWithMaxWaitTime=function(t,e,n,o){var r=this,i=void 0,c=this.once(t,(function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];e.apply(o||r,t),i&&(clearTimeout(i),i=void 0)}),o);return i=setTimeout((function(){c&&(c(),c=void 0),e.apply(o||r)}),n),function(){i&&(clearTimeout(i),i=void 0),c&&(c(),c=void 0)}},o.prototype.addOnceListenersWithMaxWaitTime=function(t,e,n,o){var r=this,i=void 0,c=this.addOnceListeners(t,(function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];e.apply(o||r,t),i&&(clearTimeout(i),i=void 0)}),o);return i=setTimeout((function(){c&&(c(),c=void 0),e.apply(o||r)}),n),function(){i&&(clearTimeout(i),i=void 0),c&&(c(),c=void 0)}},o.prototype.onceEventsWithMaxWaitTime=function(t,e,n,o){return this.addOnceListenersWithMaxWaitTime(t,e,n,o)},o.prototype.oncePromise=function(t,e){var n=this;return new Promise((function(o){n.once(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];o(t)}),e)}))},o.prototype.onceEventsPromise=function(t,e){var n=this;return new Promise((function(o){n.onceEvents(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];o(t)}),e)}))},o.prototype.onceByExecCountPromise=function(t,e,n){var o=this;return void 0===e&&(e=1),new Promise((function(r){o.onceByExecCount(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];r(t)}),e,n)}))},o.prototype.onceWithMaxWaitTimePromise=function(t,e,n){var o=this;return new Promise((function(r){o.onceWithMaxWaitTime(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];r(t)}),e,n)}))},o.prototype.onceEventsWithMaxWaitTimePromise=function(t,e,n){var o=this;return new Promise((function(r){o.onceEventsWithMaxWaitTime(t,(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];r(t)}),e,n)}))},o.prototype._debounce=function(t,e){var n=this,o=void 0;return function(){for(var r=[],i=0;i<arguments.length;i++)r[i]=arguments[i];o&&(clearTimeout(o),o=void 0),o=setTimeout((function(){t.apply(n,r)}),e)}},o.prototype._throttle=function(t,e){var n=this,o=0;return function(){for(var r=[],i=0;i<arguments.length;i++)r[i]=arguments[i];var c=Date.now();c-o>=e&&(t.apply(n,r),o=c)}},o}(o(c));t.EventEmitterSuper=s,t.default=s,Object.defineProperty(t,"__esModule",{value:!0})}));