nice-timer
Version:
Timer
3 lines (2 loc) • 5.88 kB
JavaScript
// nice-timer v1.3.2 by Yann
var e=function(){return e=Object.assign||function(e){for(var t,o=1,r=arguments.length;o<r;o++)for(var n in t=arguments[o])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},e.apply(this,arguments)};function t(e,t,o,r){return new(o||(o=Promise))((function(n,i){function s(e){try{l(r.next(e))}catch(e){i(e)}}function c(e){try{l(r.throw(e))}catch(e){i(e)}}function l(e){var t;e.done?n(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(s,c)}l((r=r.apply(e,t||[])).next())}))}function o(e,t){var o,r,n,i,s={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(l){return function(c){if(o)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(o=1,r&&(n=2&c[0]?r.return:c[0]?r.throw||((n=r.return)&&n.call(r),0):r.next)&&!(n=n.call(r,c[1])).done)return n;switch(r=0,n&&(c=[2&c[0],n.value]),c[0]){case 0:case 1:n=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!(n=s.trys,(n=n.length>0&&n[n.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!n||c[1]>n[0]&&c[1]<n[3])){s.label=c[1];break}if(6===c[0]&&s.label<n[1]){s.label=n[1],n=c;break}if(n&&s.label<n[2]){s.label=n[2],s.ops.push(c);break}n[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{o=n=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,l])}}}function r(e){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},r(e)}function n(e){var t=function e(t){var o,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new WeakMap,i=Object.prototype.toString.call(t).match(/\s(\w+)\]/)[1];if("object"!==r(t)||null===t)o=t;else if(["Array","Set","Map","Object","Arguments"].includes(i))if(n.get(t))o=n.get(t);else{var s=t.constructor;o=new s,n.set(t,o),"Array"===i?t.forEach((function(t){o.push(e(t,n))})):"Set"===i?t.forEach((function(t){o.add(e(t,n))})):"Map"===i?t.forEach((function(t,r){o.set(r,e(t,n))})):Object.keys(t).forEach((function(r){o[r]=e(t[r],n)}))}else{var c=t.constructor;if("RegExp"===i)(o=new c(t.source,/\w*$/.exec(t))).lastIndex=t.lastIndex;else if(i.includes("Function"))o=t;else if(["Date"].includes(i))o=new c(t);else if("Error"===i)(o=new c(t.message)).stack=t.stack;else if("URL"===i)o=new c(t.href);else if(i.includes("Array"))o=t.slice();else if("DataView"===i)o=new c(t.buffer.slice(0),t.byteOffset,t.byteLength);else try{var l=c.prototype.valueOf.call(t);o=Object(l)}catch(e){console.warn("Uncatched type:".concat(i)),console.warn(e)}}return o}(e);return t}"function"==typeof SuppressedError&&SuppressedError;var i=function(){function t(e){this.closeID=null,this.timerMap=new Map,this.symbolKey=new Map,this.stopState=!1,this.option=n(e)}return t.prototype.judge=function(e){if("number"!=typeof e.time||e.time<0)throw new Error("There is a problem with the time field. Please check whether there is time, or time is not of type number, or time is less than 0");if(!e.cb||"function"!=typeof e.cb)throw new Error("There is a problem with cb field. Please check whether cb exists or cb does not belong to function type");if("check"in e&&"function"!=typeof e.check)throw new Error("There is a problem with check field. Check must be a function type");if("loop"in e&&!["number","boolean"].includes(typeof e.loop))throw new Error("There is a problem with loop field. Loop must be of numeric or boolean type");if("timerKey"in e&&!["string","symbol"].includes(typeof e.timerKey))throw new Error("There is a problem with timerKey field. Timerkey must be of type symbol or string");return!0},t.prototype.doSomeThing=function(){var e=this;n(this.timerMap).forEach((function(t,o){var r=e.timerMap.get(o),n=Date.now();if(n-r.lastTime>=r.time){if(r.total++,r.lastTime=n,"check"in r){if(!r.check(r))return;r.times++}else r.times++;(!r.loop||"number"==typeof r.loop&&r.loop<=r.times)&&e.close(o);try{Promise.resolve(r).then((function(e){return e.cb(e)}))}catch(e){console.error(e)}}})),this.run()},t.prototype.closeTimer=function(){cancelAnimationFrame(this.closeID),this.closeID=null},t.prototype.close=function(e){if(this.timerMap.has(e)){var t=this.timerMap.get(e);this.isAliveSymbolKey(t.timerKey)&&this.symbolKey.delete(t.timerKey),this.timerMap.delete(e),0===this.timerMap.size&&this.closeTimer()}},t.prototype.createHandler=function(e,t,o){return{time:t||0,cb:e,loop:o||0}},t.prototype.isAliveSymbolKey=function(e){return this.symbolKey.has(e)},t.prototype.create=function(t,o,r){var i=this,s="function"==typeof t?this.createHandler(t,o,r):t;if(this.judge(s)){var c=Date.now(),l=e(e({},n(s)),{id:Symbol(c),lastTime:c,total:0,times:0,targetCloseHandler:function(){return i.close(l.id)}});if("timerKey"in s)if(this.isAliveSymbolKey(s.timerKey)){l.id=this.symbolKey.get(s.timerKey);var a=this.timerMap.get(l.id);l.total=a.total,l.times=a.times}else this.symbolKey.set(s.timerKey,l.id);return this.timerMap.set(l.id,l),null!==this.closeID||this.stopState||this.run(),l.targetCloseHandler}},t.prototype.run=function(){this.closeID=requestAnimationFrame(this.doSomeThing.bind(this))},t.prototype.stop=function(){this.closeTimer(),this.stopState=!0},t.prototype.clear=function(){this.stop(),this.timerMap.clear()},t.prototype.wait=function(e){var t=this;return new Promise((function(o){t.create(o,e)}))},t}(),s=new i,c=i;function l(e,t,o){return s.create(e,t,o)}function a(e){return t(this,void 0,void 0,(function(){return o(this,(function(t){return[2,s.wait(e)]}))}))}export{c as Timer,s as TimerInstance,l as default,l as timer,a as wait};