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