UNPKG

nice-timer

Version:

Timer

3 lines (2 loc) 6.16 kB
// nice-timer v1.3.2 by Yann !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).niceTimer={})}(this,(function(e){"use strict";var t=function(){return t=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},t.apply(this,arguments)};function o(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 r(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 n(e){return n="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},n(e)}function i(e){var t=function e(t){var o,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new WeakMap,i=Object.prototype.toString.call(t).match(/\s(\w+)\]/)[1];if("object"!==n(t)||null===t)o=t;else if(["Array","Set","Map","Object","Arguments"].includes(i))if(r.get(t))o=r.get(t);else{var s=t.constructor;o=new s,r.set(t,o),"Array"===i?t.forEach((function(t){o.push(e(t,r))})):"Set"===i?t.forEach((function(t){o.add(e(t,r))})):"Map"===i?t.forEach((function(t,n){o.set(n,e(t,r))})):Object.keys(t).forEach((function(n){o[n]=e(t[n],r)}))}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 s=function(){function e(e){this.closeID=null,this.timerMap=new Map,this.symbolKey=new Map,this.stopState=!1,this.option=i(e)}return e.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},e.prototype.doSomeThing=function(){var e=this;i(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()},e.prototype.closeTimer=function(){cancelAnimationFrame(this.closeID),this.closeID=null},e.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()}},e.prototype.createHandler=function(e,t,o){return{time:t||0,cb:e,loop:o||0}},e.prototype.isAliveSymbolKey=function(e){return this.symbolKey.has(e)},e.prototype.create=function(e,o,r){var n=this,s="function"==typeof e?this.createHandler(e,o,r):e;if(this.judge(s)){var c=Date.now(),l=t(t({},i(s)),{id:Symbol(c),lastTime:c,total:0,times:0,targetCloseHandler:function(){return n.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}},e.prototype.run=function(){this.closeID=requestAnimationFrame(this.doSomeThing.bind(this))},e.prototype.stop=function(){this.closeTimer(),this.stopState=!0},e.prototype.clear=function(){this.stop(),this.timerMap.clear()},e.prototype.wait=function(e){var t=this;return new Promise((function(o){t.create(o,e)}))},e}(),c=new s,l=s;function a(e,t,o){return c.create(e,t,o)}e.Timer=l,e.TimerInstance=c,e.default=a,e.timer=a,e.wait=function(e){return o(this,void 0,void 0,(function(){return r(this,(function(t){return[2,c.wait(e)]}))}))},Object.defineProperty(e,"__esModule",{value:!0})}));