scheduling.js 2.4 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{assumeNonNull as e}from"./maybe.js";import{nextTick as t}from"./nextTick.js";import r from"./PerformanceSampler.js";import s from"./PooledArray.js";import{createResolver as o,isAborted as n,createAbortError as a}from"./promiseUtils.js";import{Milliseconds as i}from"./time.js";class c{constructor(e){this.phases=e,this.paused=!1,this.ticks=-1,this.removed=!1}}class m{constructor(e){this.callback=e,this.isActive=!0}remove(){this.isActive=!1}}let l=0,p=0;const u={time:i(0),deltaTime:i(0),elapsedFrameTime:i(0),frameDuration:i(0)},f=["prepare","preRender","render","postRender","update","finish"],h=[],d=new s;class w{constructor(e){this._task=e}remove(){this._task.removed=!0}pause(){this._task.paused=!0}resume(){this._task.paused=!1}}const k={frameTasks:d,willDispatch:!1,clearFrameTasks:j,dispatch:b,executeFrameTasks:D};function v(e){const r=new m(e);return h.push(r),k.willDispatch||(k.willDispatch=!0,t(b)),r}function A(e){const t=new c(e);return d.push(t),null==T&&(l=performance.now(),T=requestAnimationFrame(x)),new w(t)}let T=null;function j(e=!1){d.forAll((e=>{e.removed=!0})),e&&_()}function F(e){p=Math.max(0,e)}function x(){const e=performance.now();T=null,T=d.length>0?requestAnimationFrame(x):null,k.executeFrameTasks(e)}function D(e){const t=i(e-l);l=e;const r=p>0?p:1e3/60,s=Math.max(0,t-r);for(let o=0;o<f.length;o++){const n=performance.now(),a=f[o];d.forAll((n=>{if(n.paused||n.removed)return;0===o&&n.ticks++;n.phases[a]&&(u.time=e,u.deltaTime=0===n.ticks?i(0):t,u.elapsedFrameTime=i(performance.now()-e),u.frameDuration=i(r-s),n.phases[a]?.call(n,u))})),R[o].record(performance.now()-n)}_(),q.record(performance.now()-e)}const g=new s;function _(){d.forAll((e=>{e.removed&&g.push(e)})),d.removeUnorderedMany(g.data,g.length),g.clear()}function b(){for(;h.length;){const t=e(h.shift());t.isActive&&t.callback()}k.willDispatch=!1}function y(e=1,r){const s=o(),i=()=>{n(r)?s.reject(a()):0===e?s():(--e,t((()=>i())))};return i(),s.promise}function M(){const e=o(),t=A({postRender:()=>{t.remove(),v(e)}});return e.promise}const R=f.map((e=>new r(e))),q=new r("total");export{w as FrameTaskHandle,A as addFrameTask,k as debug,R as performanceInfo,q as performanceTotal,v as schedule,F as setFrameDuration,M as waitAnimationFrame,y as waitTicks};