UNPKG

tempus

Version:
2 lines 1.91 kB
var p=0;function a(){return p++}var r=typeof window<"u",m=r&&window.requestAnimationFrame,o=r&&window.cancelAnimationFrame,s=class{callbacks;fps;time;lastTickDate;constructor(e=Number.POSITIVE_INFINITY){this.callbacks=[],this.fps=e,this.time=0,this.lastTickDate=performance.now()}get executionTime(){return 1e3/this.fps}dispatch(e,i){for(let t=0;t<this.callbacks.length;t++)this.callbacks[t]?.callback(e,i)}raf(e,i){if(this.time+=i,this.fps===Number.POSITIVE_INFINITY)this.dispatch(e,i);else if(this.time>=this.executionTime){this.time=this.time%this.executionTime;let t=e-this.lastTickDate;this.lastTickDate=e,this.dispatch(e,t)}}add({callback:e,priority:i}){typeof e!="function"&&console.error("Tempus.add: callback is not a function");let t=a();return this.callbacks.push({callback:e,priority:i,uid:t}),this.callbacks.sort((f,h)=>f.priority-h.priority),()=>this.remove(t)}remove(e){this.callbacks=this.callbacks.filter(({uid:i})=>e!==i)}},n=class{framerates;time;constructor(){this.framerates={},this.time=r?performance.now():0,r&&requestAnimationFrame(this.raf)}add(e,{priority:i=0,fps:t=Number.POSITIVE_INFINITY}={}){if(r&&typeof t=="number")return this.framerates[t]||(this.framerates[t]=new s(t)),this.framerates[t].add({callback:e,priority:i})}raf=e=>{if(!r)return;requestAnimationFrame(this.raf,!0);let i=e-this.time;this.time=e;for(let t of Object.values(this.framerates))t.raf(e,i)};patch(){r&&(window.requestAnimationFrame=(e,{priority:i=0,fps:t=Number.POSITIVE_INFINITY}={})=>e===this.raf||!e.toString().includes("requestAnimationFrame(")?m(e):(e.__tempusPatched||(e.__tempusPatched=!0,e.__tempusUnsubscribe=this.add(e,{priority:i,fps:t})),e.__tempusUnsubscribe),window.cancelAnimationFrame=e=>{if(typeof e=="function"){e?.();return}return o(e)})}unpatch(){r&&(window.requestAnimationFrame=m,window.cancelAnimationFrame=o)}},u=new n;globalThis.Tempus=u; //# sourceMappingURL=tempus.min.js.map