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