UNPKG

stopwatch-emitter

Version:
1 lines 3.33 kB
(function(t){"use strict";function EventEmitter(){}function e(t,e){if(i)return e.indexOf(t);for(var n=e.length;n--;)if(e[n]===t)return n;return-1}var n=EventEmitter.prototype,i=Array.prototype.indexOf?!0:!1;n._getEvents=function(){return this._events||(this._events={})},n.getListeners=function(t){var e=this._getEvents();return e[t]||(e[t]=[])},n.addListener=function(t,n){var i=this.getListeners(t);return-1===e(n,i)&&i.push(n),this},n.on=n.addListener,n.removeListener=function(t,n){var i=this.getListeners(t),r=e(n,i);return-1!==r&&(i.splice(r,1),0===i.length&&this.removeEvent(t)),this},n.off=n.removeListener,n.addListeners=function(t,e){return this.manipulateListeners(!1,t,e)},n.removeListeners=function(t,e){return this.manipulateListeners(!0,t,e)},n.manipulateListeners=function(t,e,n){var i,r,s=t?this.removeListener:this.addListener,u=t?this.removeListeners:this.addListeners;if("object"==typeof e)for(i in e)e.hasOwnProperty(i)&&(r=e[i])&&("function"==typeof r?s.call(this,i,r):u.call(this,i,r));else for(i=n.length;i--;)s.call(this,e,n[i]);return this},n.removeEvent=function(t){return t?delete this._getEvents()[t]:delete this._events,this},n.emitEvent=function(t,e){for(var n,i=this.getListeners(t),r=i.length;r--;)n=e?i[r].apply(null,e):i[r](),n===!0&&this.removeListener(t,i[r]);return this},n.trigger=n.emitEvent,n.emit=function(t){var e=Array.prototype.slice.call(arguments,1);return this.emitEvent(t,e)},"function"==typeof define&&define.amd?define(function(){return EventEmitter}):t.EventEmitter=EventEmitter})(this),function(t){var e={}.hasOwnProperty,n=function(t,n){function i(){this.constructor=t}for(var r in n)e.call(n,r)&&(t[r]=n[r]);return i.prototype=n.prototype,t.prototype=new i,t.__super__=n.prototype,t},Stopwatch=function(t){function Stopwatch(t){null==t&&(t="5m"),this.currentTime=0,this._stopEmitted=!1,this._running=!1,this.toQ=[],this.parseTime(t),this._setupEvents()}n(Stopwatch,t);var e=Stopwatch.prototype;return e.parseTime=function(t){if(!isNaN(t))return this.maxTime=t;t=(""+t).match(/([\d\.]+)(\w{1})/);var e=parseFloat(t[1],10),n=t[2];switch(n){case"s":e*=1;break;case"m":e*=60;break;case"h":e*=3600}this.maxTime=Math.round(e)},e._setupEvents=function(){var t=this;t.on("stop",function(){t._stopEmitted=!0,t.clear()});var e=function(){t._stopEmitted=!1,t._running&&t.clear()};t.on("start",e),t.on("pause",e),t.on("restart",e)},e.tick=function(){var t=this;if(t.currentTime>=t.maxTime)return t._stopEmitted||t.emit("stop"),t._running=!1,void 0;if(t._running){t.emit("tick");var e=setTimeout(function(){t.currentTime++,t.tick()},1e3);t.toQ.push(e)}},e.clear=function(){for(var t=this.toQ;t.length;)clearTimeout(t[t.length-1]),t.pop()},e.pause=function(){this._running&&(this._running=!1,this.emit("pause"))},e.stop=function(){this._running&&(this._running=!1,this.currentTime=0,this.emit("stop"))},e.restart=function(){this.currentTime=0,this.emit("restart"),this.start(!1)},e.start=function(t){null==t&&(t=!0),this._running&&t||this.currentTime>=this.maxTime||(t&&this.emit("start"),this._running=!0,this.tick())},e.getCurrentTime=function(){return this.currentTime},e.getRemainingTime=function(){return this.maxTime-this.currentTime},e.getMaxTime=function(){return this.maxTime},e.isRunning=function(){return!!this._running},Stopwatch}(EventEmitter);t.Stopwatch=Stopwatch}(this);