@scena/event-emitter
Version:
Implement EventEmitter on object or component
11 lines (10 loc) • 1.95 kB
JavaScript
/*
Copyright (c) 2019 Daybrush
name: @scena/event-emitter
license: MIT
author: Daybrush
repository: git+https://github.com/daybrush/gesture.git
version: 1.0.5
*/
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t="undefined"!=typeof globalThis?globalThis:t||self).EventEmitter=n()}(this,function(){"use strict";var i=function(){return(i=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var i in n=arguments[e])Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i]);return t}).apply(this,arguments)};function o(t){return t&&"object"==typeof t}var t,n=function(){function t(){this._events={}}var n=t.prototype;return n.on=function(t,n){if(o(t))for(var e in t)this.on(e,t[e]);else this._addEvent(t,n,{});return this},n.off=function(t,n){if(t)if(o(t))for(var e in t)this.off(e);else{var r,i;n?!(r=this._events[t])||-1<(i=function(t,n,e){void 0===e&&(e=-1);for(var r=t.length,i=0;i<r;++i)if(n(t[i],i,t))return i;return e}(r,function(t){return t.listener===n}))&&r.splice(i,1):this._events[t]=[]}else this._events={};return this},n.once=function(n,t){var e=this;return t&&this._addEvent(n,t,{once:!0}),new Promise(function(t){e._addEvent(n,t,{once:!0})})},n.emit=function(n,e){var r=this;void 0===e&&(e={});var t=this._events[n];if(!n||!t)return!0;var i=!1;return e.eventType=n,e.stop=function(){i=!0},e.currentTarget=this,function(){for(var t=0,n=0,e=arguments.length;n<e;n++)t+=arguments[n].length;for(var r=Array(t),i=0,n=0;n<e;n++)for(var o=arguments[n],f=0,s=o.length;f<s;f++,i++)r[i]=o[f];return r}(t).forEach(function(t){t.listener(e),t.once&&r.off(n,t.listener)}),!i},n.trigger=function(t,n){return void 0===n&&(n={}),this.emit(t,n)},n._addEvent=function(t,n,e){var r=this._events;r[t]=r[t]||[],r[t].push(i({listener:n},e))},t}(),e={__proto__:null,default:n};for(t in e)n[t]=e[t];return n});
//# sourceMappingURL=event-emitter.min.js.map