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 • 1.64 kB
JavaScript
(()=>{"use strict";var e={d:(r,i)=>{for(var a in i)e.o(i,a)&&!e.o(r,a)&&Object.defineProperty(r,a,{enumerable:!0,get:i[a]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r)},r={};e.d(r,{TinyDomReadyManager:()=>i});const i=class{#e=[];#r=!1;#i=!1;#a=[];#n(){this.#r&&Promise.all(this.#a).then(()=>{this.#i=!0,this.#o(!1)}).catch(e=>{console.error("[TinyDomReadyManager] Promise rejected:",e)})}#o(e){this.#e.filter(r=>r.domOnly===e).sort((e,r)=>r.priority-e.priority).forEach(e=>this.#t(e)),this.#e=this.#e.filter(r=>!(r.once&&(!e||r.domOnly)))}#t(e){if("function"==typeof e.filter)try{if(!e.filter())return}catch(e){return void console.warn("[TinyDomReadyManager] Filter error:",e)}try{e.fn()}catch(e){console.error("[TinyDomReadyManager] Handler error:",e)}}_markDomReady(){this.#r=!0,this.#o(!0),this.#n()}init(){if(this.#r)throw new Error("[TinyDomReadyManager] init() has already been called.");"interactive"===document.readyState||"complete"===document.readyState?this._markDomReady():document.addEventListener("DOMContentLoaded",()=>this._markDomReady())}addPromise(e){if(!(e instanceof Promise))throw new TypeError("[TinyDomReadyManager] promise must be a valid Promise.");this.#i||(this.#a.push(e),this.#r&&this.#n())}onReady(e,{once:r=!0,priority:i=0,filter:a=null,domOnly:n=!1}={}){if("function"!=typeof e)throw new TypeError("[TinyDomReadyManager] fn must be a function.");const o={fn:e,once:r,priority:i,filter:a,domOnly:n};if(n&&this.#r)return this.#t(o),void(r||this.#e.push(o));!n&&this.#i?this.#t(o):this.#e.push(o)}isReady(){return this.#i}isDomReady(){return this.#r}};window.TinyDomReadyManager=r.TinyDomReadyManager})();