eventemitter-super
Version:
An event emitter/listener tool based on EventEmitter3.
2 lines (1 loc) • 10.2 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,o){return new(n||(n=Promise))(function(r,i){function c(t){try{s(o.next(t))}catch(t){i(t)}}function u(t){try{s(o.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(c,u)}s((o=o.apply(t,e||[])).next())})}function o(t,e){var n,o,r,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(s){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(i=0)),i;)try{if(n=1,o&&(r=2&u[0]?o.return:u[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,u[1])).done)return r;switch(o=0,r&&(u=[2&u[0],r.value]),u[0]){case 0:case 1:r=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,o=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(r=i.trys,(r=r.length>0&&r[r.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!r||u[1]>r[0]&&u[1]<r[3])){i.label=u[1];break}if(6===u[0]&&i.label<r[1]){i.label=r[1],r=u;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(u);break}r[2]&&i.ops.pop(),i.trys.pop();continue}u=e.call(t,i)}catch(t){u=[6,t],o=0}finally{n=r=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}}function r(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 i(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}"function"==typeof SuppressedError&&SuppressedError;var c,u={exports:{}};var s=(c||(c=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 u=new r(o,i||t,c),s=n?n+e:e;return t._events[s]?t._events[s].fn?t._events[s]=[t._events[s],u]:t._events[s].push(u):(t._events[s]=u,t._eventsCount++),t}function c(t,e){0===--t._eventsCount?t._events=new o:delete t._events[e]}function u(){this._events=new o,this._eventsCount=0}Object.create&&(o.prototype=Object.create(null),(new o).__proto__||(n=!1)),u.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},u.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},u.prototype.listenerCount=function(t){var e=n?n+t:t,o=this._events[e];return o?o.fn?1:o.length:0},u.prototype.emit=function(t,e,o,r,i,c){var u=n?n+t:t;if(!this._events[u])return!1;var s,f,a=this._events[u],p=arguments.length;if(a.fn){switch(a.once&&this.removeListener(t,a.fn,void 0,!0),p){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,e),!0;case 3:return a.fn.call(a.context,e,o),!0;case 4:return a.fn.call(a.context,e,o,r),!0;case 5:return a.fn.call(a.context,e,o,r,i),!0;case 6:return a.fn.call(a.context,e,o,r,i,c),!0}for(f=1,s=new Array(p-1);f<p;f++)s[f-1]=arguments[f];a.fn.apply(a.context,s)}else{var l,h=a.length;for(f=0;f<h;f++)switch(a[f].once&&this.removeListener(t,a[f].fn,void 0,!0),p){case 1:a[f].fn.call(a[f].context);break;case 2:a[f].fn.call(a[f].context,e);break;case 3:a[f].fn.call(a[f].context,e,o);break;case 4:a[f].fn.call(a[f].context,e,o,r);break;default:if(!s)for(l=1,s=new Array(p-1);l<p;l++)s[l-1]=arguments[l];a[f].fn.apply(a[f].context,s)}}return!0},u.prototype.on=function(t,e,n){return i(this,t,e,n,!1)},u.prototype.once=function(t,e,n){return i(this,t,e,n,!0)},u.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 u=this._events[i];if(u.fn)u.fn!==e||r&&!u.once||o&&u.context!==o||c(this,i);else{for(var s=0,f=[],a=u.length;s<a;s++)(u[s].fn!==e||r&&!u[s].once||o&&u[s].context!==o)&&f.push(u[s]);f.length?this._events[i]=1===f.length?f[0]:f:c(this,i)}return this},u.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},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,t.exports=u}(u)),u.exports),f=i(s),a=function(t){function i(){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)}(i,t),i.prototype.on=function(e,n,o){var r=this;t.prototype.on.call(this,e,n,o);return function(){r.off(e,n,o)}},i.prototype.addListener=function(e,n,o){var r=this;t.prototype.addListener.call(this,e,n,o);return function(){r.removeListener(e,n,o)}},i.prototype.once=function(e,n,o){var r=this;t.prototype.once.call(this,e,n,o);return function(){r.off(e,n,o,!0)}},i.prototype.emitPromise=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return n(this,void 0,void 0,function(){var n,r,i,c,u,s=this;return o(this,function(o){switch(o.label){case 0:return n=f.prefixed,r=n?n+t:t,(i=null===(u=this._events)||void 0===u?void 0:u[r])?i.fn?(i.once&&this.removeListener(t,i.fn,void 0,!0),[4,i.fn.apply(i.context,e)]):[3,2]:[2];case 1:case 3:return o.sent(),[2];case 2:return c=i.slice(),[4,Promise.all(c.map(function(n){return n.once&&s.removeListener(t,n.fn,void 0,!0),n.fn.apply(n.context,e)}))]}})})},i.prototype.onDebounce=function(t,e,n,o){return e=this._debounce(e,n),this.on(t,e,o)},i.prototype.onThrottle=function(t,e,n,o){return e=this._throttle(e,n),this.on(t,e,o)},i.prototype.onDebounceHtmlEvent=function(t,e,n,o,r){return n=this._debounce(n,o),this.addHtmlListener(t,e,n,r)},i.prototype.onThrottleHtmlEvent=function(t,e,n,o,r){return n=this._throttle(n,o),this.addHtmlListener(t,e,n,r)},i.prototype.onHtmlEvent=function(t,e,n,o){return this.addHtmlListener(t,e,n,o)},i.prototype.addHtmlListener=function(t,e,n,o){return t.addEventListener(e,n,o),function(){t.removeEventListener(e,n,o)}},i.prototype.onceEvents=function(t,e,n){return this.addOnceListeners(t,e,n)},i.prototype.addOnceListeners=function(t,e,n){var o=this,i=r([],t,!0),c=Array.from(new Set(i)),u=[];return c.forEach(function(t){var r=o.on(t,function(){for(var c=[],s=0;s<arguments.length;s++)c[s]=arguments[s];if(i.splice(i.indexOf(t),1),!i.includes(t)){var f=u.indexOf(r);f>-1&&u.splice(f,1),r()}0===i.length&&e.apply(n||o,c)},n);u.push(r)}),function(){u.forEach(function(t){t()})}},i.prototype.bindOnce=function(t,e,n){return this.once(t,e,n)},i.prototype.bind=function(t,e,n){return this.on(t,e,n)},i.prototype.fire=function(e){for(var n=[],o=1;o<arguments.length;o++)n[o-1]=arguments[o];return t.prototype.emit.apply(this,r([e],n,!1))},i.prototype.unbind=function(e,n,o,r){return t.prototype.off.call(this,e,n,o,r)},i.prototype.un=function(e,n,o,r){return t.prototype.off.call(this,e,n,o,r)},i.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},i.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)}},i.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)}},i.prototype.onceEventsWithMaxWaitTime=function(t,e,n,o){return this.addOnceListenersWithMaxWaitTime(t,e,n,o)},i.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)})},i.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)})},i.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)})},i.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)})},i.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)})},i.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)}},i.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)}},i}(f);t.EventEmitterSuper=a,t.default=a,Object.defineProperty(t,"__esModule",{value:!0})});