tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
1 lines • 2.52 kB
JavaScript
(()=>{"use strict";var e={d:(o,t)=>{for(var r in t)e.o(t,r)&&!e.o(o,r)&&Object.defineProperty(o,r,{enumerable:!0,get:t[r]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o)},o={};e.d(o,{TinyTimeout:()=>r});class t{#e=!1;#o;#t;#r=null;#i=new Map;constructor({cooldownWatcherTime:e=5e3,allowAutoConfigChange:o=!1}={}){if(!Number.isFinite(e)||e<=0)throw new TypeError("Expected 'cooldownWatcherTime' to be a positive number.");if("boolean"!=typeof o)throw new TypeError("Expected 'allowAutoConfigChange' to be a boolean.");this.#t=e,this.#o=o,this.setCooldownWatcherTime(e)}isDestroyed(){return this.#e}getAllowAutoConfigChange(){return this.#o}getCooldownWatcherTime(){return this.#t}setAllowAutoConfigChange(e){if("boolean"!=typeof e)throw new TypeError("Expected 'value' to be a boolean.");this.#o=e}setCooldownWatcherTime(e){if(this.#e)throw new Error("TinyTimeout has been destroyed.");if(!Number.isFinite(e)||e<=0)throw new TypeError("Expected 'value' to be a positive number.");this.#t=e,this.#r&&clearInterval(this.#r),this.#r=setInterval(()=>{this.#i.forEach(e=>{e.now>0&&e.now--})},this.#t)}set(e,o,t,r=null){if(this.#e)throw new Error("TinyTimeout has been destroyed.");if("string"!=typeof e||""===e.trim())throw new TypeError("Expected 'id' to be a non-empty string.");if("function"!=typeof o)throw new TypeError("Expected 'callback' to be a function.");if(!Number.isFinite(t)||t<0)throw new TypeError("Expected 'value' to be a non-negative number.");if(null!==r&&(!Number.isFinite(r)||r<0))throw new TypeError("Expected 'limit' to be null or a non-negative number.");let i=this.#i.get(e);(!i||this.#o&&t!==i.value)&&(i={value:t,now:0},this.#i.set(e,i));const n=i.value*i.now;return i.now++,setTimeout(o,"number"==typeof r?Math.min(n,r):n)}static waitForTrue(e,o=100){if("function"!=typeof e)throw new TypeError("Expected 'getValue' to be a function.");if(!Number.isFinite(o)||o<=0)throw new TypeError("Expected 'checkInterval' to be a positive number.");return new Promise(t=>{const r=setInterval(()=>{e()&&(clearInterval(r),t())},o)})}waitForTrue(e,o=100){if(this.#e)throw new Error("TinyTimeout has been destroyed.");if("function"!=typeof e)throw new TypeError("Expected 'getValue' to be a function.");if(null!==o&&(!Number.isFinite(o)||o<=0))throw new TypeError("Expected 'checkInterval' to be null or a positive number.");return t.waitForTrue(e,o??this.#t)}destroy(){this.#e||(this.#e=!0,this.#r&&clearInterval(this.#r),this.#r=null,this.#i.clear())}}const r=t;window.TinyTimeout=o.TinyTimeout})();