eventemitter3-timer
Version:
`EventEmitter3-Timer` is a plugin extend [EventEmitter3](https://github.com/primus/eventemitter3) to create time events easily.
1 lines • 6.67 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.EE3Timer=t():e.EE3Timer=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=2)}([function(e,t,r){"use strict";var n=this&&this.__extends||function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(t,"__esModule",{value:!0});var i=r(1),o=r(3),s=function(e){function t(r){void 0===r&&(r=1);var n=e.call(this)||this;return n.time=r,t._timerManager||(t._timerManager=new i.TimerManager),t._timerManager.addTimer(n),n._timerManager=t._timerManager,n.active=!1,n.isEnded=!1,n.isStarted=!1,n.expire=!1,n.delay=0,n.repeat=0,n.loop=!1,n._delayTime=0,n._elapsedTime=0,n._repeat=0,n}return n(t,e),Object.defineProperty(t,"timerManager",{get:function(){return t._timerManager||(t._timerManager=new i.TimerManager),t._timerManager},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"timerManager",{get:function(){return this._timerManager},set:function(e){this._timerManager=e},enumerable:!0,configurable:!0}),t.prototype.remove=function(){return t._timerManager.removeTimer(this),this},t.prototype.start=function(){return this.active=!0,this},t.prototype.stop=function(){return this.active=!1,this.emit("stop",this._elapsedTime),this},t.prototype.reset=function(){return this._elapsedTime=0,this._repeat=0,this._delayTime=0,this.isStarted=!1,this.isEnded=!1,this},t.prototype.update=function(e){if(this.active){if(this.delay>this._delayTime)return void(this._delayTime+=e);if(this.isStarted||(this.isStarted=!0,this.emit("start",this._elapsedTime)),this.time>this._elapsedTime){var t=this._elapsedTime+e,r=t>=this.time;if(this._elapsedTime=r?this.time:t,this.emit("update",this._elapsedTime,e),r){if(this.loop||this.repeat>this._repeat)return this._repeat++,this.emit("repeat",this._elapsedTime,this._repeat),void(this._elapsedTime=0);this.isEnded=!0,this.active=!1,this.emit("end",this._elapsedTime)}}}},t}(o.EventEmitter);t.Timer=s},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(0),i=function(){function e(){this.timers=[],this._timersToDelete=[],this._last=0}return e.prototype.update=function(e){if(e||0===e||(e=this._getDeltaMS()),this._timersToDelete.length){for(var t=0,r=this._timersToDelete;t<r.length;t++){var n=r[t];this._remove(n)}this._timersToDelete.length=0}for(var i=0,o=this.timers;i<o.length;i++){var s=o[i];s.active&&(s.update(e),s.isEnded&&s.expire&&this.removeTimer(s))}},e.prototype.removeTimer=function(e){this._timersToDelete.push(e),e.timerManager=null},e.prototype.addTimer=function(e){this.timers.push(e),e.timerManager&&e.timerManager.removeTimer(e),e.timerManager=this},e.prototype.createTimer=function(e){var t=new n.Timer(e);return this.addTimer(t),t},e.prototype._remove=function(e){var t=this.timers.indexOf(e);t>-1&&this.timers.splice(t,1)},e.prototype._getDeltaMS=function(){0===this._last&&(this._last=Date.now());var e=Date.now(),t=e-this._last;return this._last=e,t},e}();t.TimerManager=i},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(0);t.Timer=n.Timer;var i=r(1);t.TimerManager=i.TimerManager},function(e,t,r){"use strict";function n(){}function i(e,t,r){this.fn=e,this.context=t,this.once=r||!1}function o(e,t,r,n,o){if("function"!=typeof r)throw new TypeError("The listener must be a function");var s=new i(r,n||e,o),a=u?u+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],s]:e._events[a].push(s):(e._events[a]=s,e._eventsCount++),e}function s(e,t){0==--e._eventsCount?e._events=new n:delete e._events[t]}function a(){this._events=new n,this._eventsCount=0}var c=Object.prototype.hasOwnProperty,u="~";Object.create&&(n.prototype=Object.create(null),(new n).__proto__||(u=!1)),a.prototype.eventNames=function(){var e,t,r=[];if(0===this._eventsCount)return r;for(t in e=this._events)c.call(e,t)&&r.push(u?t.slice(1):t);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(e)):r},a.prototype.listeners=function(e){var t=u?u+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var n=0,i=r.length,o=new Array(i);n<i;n++)o[n]=r[n].fn;return o},a.prototype.listenerCount=function(e){var t=u?u+e:e,r=this._events[t];return r?r.fn?1:r.length:0},a.prototype.emit=function(e,t,r,n,i,o){var s=u?u+e:e;if(!this._events[s])return!1;var a,c,p=this._events[s],f=arguments.length;if(p.fn){switch(p.once&&this.removeListener(e,p.fn,void 0,!0),f){case 1:return p.fn.call(p.context),!0;case 2:return p.fn.call(p.context,t),!0;case 3:return p.fn.call(p.context,t,r),!0;case 4:return p.fn.call(p.context,t,r,n),!0;case 5:return p.fn.call(p.context,t,r,n,i),!0;case 6:return p.fn.call(p.context,t,r,n,i,o),!0}for(c=1,a=new Array(f-1);c<f;c++)a[c-1]=arguments[c];p.fn.apply(p.context,a)}else{var h,l=p.length;for(c=0;c<l;c++)switch(p[c].once&&this.removeListener(e,p[c].fn,void 0,!0),f){case 1:p[c].fn.call(p[c].context);break;case 2:p[c].fn.call(p[c].context,t);break;case 3:p[c].fn.call(p[c].context,t,r);break;case 4:p[c].fn.call(p[c].context,t,r,n);break;default:if(!a)for(h=1,a=new Array(f-1);h<f;h++)a[h-1]=arguments[h];p[c].fn.apply(p[c].context,a)}}return!0},a.prototype.on=function(e,t,r){return o(this,e,t,r,!1)},a.prototype.once=function(e,t,r){return o(this,e,t,r,!0)},a.prototype.removeListener=function(e,t,r,n){var i=u?u+e:e;if(!this._events[i])return this;if(!t)return s(this,i),this;var o=this._events[i];if(o.fn)o.fn!==t||n&&!o.once||r&&o.context!==r||s(this,i);else{for(var a=0,c=[],p=o.length;a<p;a++)(o[a].fn!==t||n&&!o[a].once||r&&o[a].context!==r)&&c.push(o[a]);c.length?this._events[i]=1===c.length?c[0]:c:s(this,i)}return this},a.prototype.removeAllListeners=function(e){var t;return e?(t=u?u+e:e,this._events[t]&&s(this,t)):(this._events=new n,this._eventsCount=0),this},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=u,a.EventEmitter=a,e.exports=a}])});