UNPKG

tempus

Version:
2 lines 5.75 kB
var o=class{_elapsed=0;_currentTime=0;_startTime=void 0;_lastTime=void 0;_isPlaying=!1;_deltaTime=0;play(){this._isPlaying||(this._currentTime=0,this._startTime=void 0,this._isPlaying=!0)}pause(){this._isPlaying&&(this._deltaTime=0,this._isPlaying=!1)}reset(){this._elapsed=0,this._deltaTime=0,this._currentTime=0,this._lastTime=void 0,this._isPlaying=!1}update(e){this._isPlaying&&(this._startTime||(this._startTime=e),this._lastTime===void 0?(this._lastTime=this._startTime,this._currentTime=0,this._deltaTime=0):(this._lastTime=this._currentTime,this._currentTime=e-this._startTime,this._deltaTime=this._currentTime-this._lastTime,this._elapsed+=this._deltaTime))}get time(){return this._elapsed}get isPlaying(){return this._isPlaying}get deltaTime(){return this._deltaTime}};var v=0;function d(){return v++}var f="1.0.0-dev.18";var r=typeof window<"u",l=r&&window.requestAnimationFrame.bind(window),p=r&&window.cancelAnimationFrame.bind(window);r&&(window.tempusVersion=f);function u(n){let e=performance.now();return n(),performance.now()-e}var y=60;function b(n,e){n.push(e),n.length>y&&n.shift()}var k=120,c=class{callbacks=[];fps;time=0;lastTickDate=0;framesCount=0;constructor(e=Number.POSITIVE_INFINITY){this.fps=e}get isRelativeFps(){return typeof this.fps=="string"&&this.fps.endsWith("%")}get maxFramesCount(){return this.isRelativeFps?Math.max(1,Math.round(100/Number(this.fps.replace("%","")))):1}get executionTime(){return this.isRelativeFps?0:1e3/this.fps}dispatch(e){for(let t=0;t<this.callbacks.length;t++){let i=u(()=>{this.callbacks[t]?.callback(e)});b(this.callbacks[t].samples,i)}}raf(e){if(this.time+=e.deltaTime,this.isRelativeFps){if(this.framesCount===0){let t=e.deltaTime;e.deltaTime=e.time-this.lastTickDate,this.lastTickDate=e.time,this.dispatch(e),e.deltaTime=t}this.framesCount++,this.framesCount%=this.maxFramesCount}else if(this.fps===Number.POSITIVE_INFINITY)this.dispatch(e);else if(this.time>=this.executionTime){this.time=this.time%this.executionTime;let t=e.deltaTime;e.deltaTime=e.time-this.lastTickDate,this.lastTickDate=e.time,this.dispatch(e),e.deltaTime=t}}add({callback:e,order:t,label:i}){if(typeof e!="function"){console.warn("Tempus.add: callback is not a function");return}let s=d();return this.callbacks.push({callback:e,order:t,uid:s,label:i,samples:[]}),this.callbacks.sort((a,m)=>a.order-m.order),()=>this.remove(s)}remove(e){this.callbacks=this.callbacks.filter(({uid:t})=>e!==t)}},h=class{framerates={};clock=new o;fps;usage=0;rafId;frameCount=0;targetFps=60;frameStartTime=performance.now();get frameBudget(){return 1e3/this.targetFps}state;patched=!1;rafQueue=new Map;rafHandleId=0;patchedRAF;patchedCancelRAF;flushUnsubscribe;patchMeta=new WeakMap;patchEntries=[];patchAnonCount=0;constructor(){let e=this;this.state={time:0,deltaTime:0,frame:0,budget(){return e.frameBudget-(performance.now()-e.frameStartTime)}},r&&this.play()}restart(){this.rafId&&p(this.rafId),this.frameCount=0;for(let e of Object.values(this.framerates))e.framesCount=0,e.time=0,e.lastTickDate=performance.now();this.clock.reset(),this.play()}play(){!r||this.clock.isPlaying||(this.clock.play(),this.rafId=l(this.raf))}pause(){!r||!this.rafId||!this.clock.isPlaying||(p(this.rafId),this.rafId=void 0,this.clock.pause())}get isPlaying(){return this.clock.isPlaying}add(e,{order:t,priority:i,fps:s=Number.POSITIVE_INFINITY,label:a=""}={}){if(!r)return;let m=t??i??0;if(typeof s=="number"||typeof s=="string"&&s.endsWith("%"))return this.framerates[s]||(this.framerates[s]=new c(s)),this.framerates[s].add({callback:e,order:m,label:a});console.warn('Tempus.add: fps is not a number or a string ending with "%"')}raf=e=>{if(!r)return;this.clock.update(e);let t=this.clock.time,i=this.clock.deltaTime;this.fps=1e3/i,this.frameStartTime=performance.now(),this.state.time=t,this.state.deltaTime=i,this.state.frame=this.frameCount;let s=u(()=>{for(let a of Object.values(this.framerates))a.raf(this.state)});i&&(this.usage=s/i),this.frameCount++,this.rafId=l(this.raf)};patch(){!r||this.patched||(this.patched=!0,this.flushUnsubscribe=this.add(()=>{if(this.rafQueue.size===0)return;let e=Array.from(this.rafQueue.values());this.rafQueue.clear();let t=performance.now(),i=this.frameCount;for(let s of e){let a=this.patchMeta.get(s);a||(a={callback:s,label:s.name||`anonymous#${++this.patchAnonCount}`,samples:[],lastFrame:i},this.patchMeta.set(s,a),this.patchEntries.push(a));let m=u(()=>{try{s(t)}catch(T){console.error("Tempus.patch: rAF callback threw",T)}});b(a.samples,m),a.lastFrame=i}this.prunePatchEntries(i)},{label:"tempus"}),this.patchedRAF=e=>{let t=++this.rafHandleId;return this.rafQueue.set(t,e),t},window.requestAnimationFrame=this.patchedRAF,this.patchedCancelRAF=e=>{this.rafQueue.delete(e)},window.cancelAnimationFrame=this.patchedCancelRAF)}unpatch(){!r||!this.patched||(window.requestAnimationFrame===this.patchedRAF&&(window.requestAnimationFrame=l),window.cancelAnimationFrame===this.patchedCancelRAF&&(window.cancelAnimationFrame=p),this.flushUnsubscribe?.(),this.flushUnsubscribe=void 0,this.rafQueue.clear(),this.patchedRAF=void 0,this.patchedCancelRAF=void 0,this.patchMeta=new WeakMap,this.patchEntries=[],this.patchAnonCount=0,this.patched=!1)}prunePatchEntries(e){for(let t=this.patchEntries.length-1;t>=0;t--){let i=this.patchEntries[t];e-i.lastFrame>k&&(this.patchMeta.delete(i.callback),this.patchEntries.splice(t,1))}}inspect(){let e=Object.values(this.framerates).flatMap(i=>i.callbacks.map(s=>({label:s.label,samples:s.samples.slice(),order:s.order,fps:i.fps,source:"add"}))),t=this.patchEntries.map(i=>({label:i.label,samples:i.samples.slice(),order:0,fps:Number.POSITIVE_INFINITY,source:"patch"}));return[...e,...t]}},g=new h;globalThis.Tempus=g; //# sourceMappingURL=tempus.min.js.map