UNPKG

ticker-service

Version:

A service used to create animation loops in a simple way and to keep everything in sync within an app.

3 lines (2 loc) 3.77 kB
const e=self.setTimeout,t=self.clearTimeout,i=self.setInterval,a=self.clearInterval,r=self.requestAnimationFrame,s=self.cancelAnimationFrame,n=new class{constructor(){this.currentIndex=1e4,this.currentTime=0,this.delta=0,this.animationFrameRequest=null,this.useScopeFunctionsFlag=!0,this.running=!1,this.frameRateHistory=[],this.tickerCallbacks=new Map,this.getTime=()=>self.performance?performance.now():Date.now(),this.clearTickerCallback=e=>{this.tickerCallbacks.delete(e)},this.createTickerCallback=(e,t,i,a)=>{if("number"!=typeof a||a<=0)throw new Error("Delay must be a positive number.");if("number"!=typeof i||i<=0)throw new Error("A callback must be executed at least once.");const r=this.currentIndex++;return this.tickerCallbacks.set(r,{callback:e,params:t,repeats:i,delay:a,count:0,executionTime:0,totalTime:this.currentTime-this.getTime()}),r},this.tick=e=>{this.tickerCallbacks.forEach((t,i)=>{t.totalTime+=e;const a=t.totalTime-t.executionTime;a>=t.delay&&"function"==typeof t.callback&&(t.callback(...t.params,a,t.count),t.count++,t.executionTime=t.totalTime,t.count>=t.repeats&&this.clearTickerCallback(i))})},this.playAnimationFrame=()=>{this.animationFrameRequest=r(()=>{if(this.running){const e=this.getTime();this.delta=this.currentTime?e-this.currentTime:0,this.delta&&this.tick(this.delta),this.currentTime=e,this.storeFrameRateHistory(),this.playAnimationFrame()}})},this.storeFrameRateHistory=()=>{if(this.delta>0){const e=Math.min(1e3/this.delta,this.maxFrameRate);this.frameRateHistory.unshift(e),this.frameRateHistory.length>120&&(this.frameRateHistory.length=120)}},self.setAnimationLoop=(e,t)=>this.setAnimationLoop(e,t),self.clearAnimationLoop=e=>this.clearAnimationLoop(e),self.setCounter=(e,t,i,...a)=>this.setCounter(e,t,i,...a),self.clearCounter=e=>this.clearCounter(e),self.sleep=e=>this.sleep(e),self.frame=()=>this.frame(),this.start()}get frameRate(){return this.delta?Math.min(1e3/this.delta,this.maxFrameRate):this.maxFrameRate}get maxFrameRate(){return this.delta?30*Math.round(1e3/this.delta/30):60}get averageFrameRate(){return this.frameRateHistory.length?this.frameRateHistory.reduce((e,t)=>e+t)/this.frameRateHistory.length:this.frameRate}get score(){return Math.round(this.averageFrameRate/this.maxFrameRate*100)}get isRunning(){return this.running}get useScopeFunctions(){return this.useScopeFunctionsFlag}set useScopeFunctions(n){(n=Boolean(n))!==this.useScopeFunctionsFlag&&(self.setTimeout=n?e:(e,t,...i)=>this.setTimeout(e,t,...i),self.clearTimeout=n?t:e=>this.clearTimeout(e),self.setInterval=n?i:(e,t,...i)=>this.setInterval(e,t,...i),self.clearInterval=n?a:e=>this.clearInterval(e),self.requestAnimationFrame=n?r:e=>this.requestAnimationFrame(e),self.cancelAnimationFrame=n?s:e=>this.cancelAnimationFrame(e),this.useScopeFunctionsFlag=n)}setTimeout(e,t,...i){return this.createTickerCallback(e,i,1,t=t||1)}clearTimeout(e){this.clearTickerCallback(e)}setInterval(e,t,...i){return this.createTickerCallback(e,i,Infinity,t)}clearInterval(e){this.clearTickerCallback(e)}setCounter(e,t,i,...a){return this.createTickerCallback(e,a,i,t)}clearCounter(e){this.clearTickerCallback(e)}requestAnimationFrame(e){return this.createTickerCallback(e,[],1,1)}cancelAnimationFrame(e){this.clearTickerCallback(e)}setAnimationLoop(e,t){return this.createTickerCallback(e,[],Infinity,t?1e3/t:1)}clearAnimationLoop(e){this.clearTickerCallback(e)}sleep(e){return new Promise(t=>setTimeout(t,e))}frame(){return new Promise(e=>requestAnimationFrame(e))}start(){return this.running||(this.useScopeFunctions=!1,this.running=!0,this.currentTime=this.getTime(),this.playAnimationFrame()),this}stop(){return this.running=!1,s(this.animationFrameRequest),this.useScopeFunctions=!0,this}};export default n; //# sourceMappingURL=index.modern.js.map