@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 4.22 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as SDK from"../../core/sdk/sdk.js";import*as SourceMapScopes from"../../models/source_map_scopes/source_map_scopes.js";import*as TimelineModel from"../../models/timeline_model/timeline_model.js";import{TimelineUIUtils}from"./TimelineUIUtils.js";const resolveNamesTimeout=500;export class PerformanceModel extends Common.ObjectWrapper.ObjectWrapper{mainTargetInternal;tracingModelInternal;filtersInternal;timelineModelInternal;frameModelInternal;windowInternal;willResolveNames=!1;recordStartTimeInternal;constructor(){super(),this.mainTargetInternal=null,this.tracingModelInternal=null,this.filtersInternal=[],this.timelineModelInternal=new TimelineModel.TimelineModel.TimelineModelImpl,this.frameModelInternal=new TimelineModel.TimelineFrameModel.TimelineFrameModel((e=>TimelineUIUtils.eventStyle(e).category.name)),this.windowInternal={left:0,right:1/0},this.recordStartTimeInternal=void 0}setMainTarget(e){this.mainTargetInternal=e}mainTarget(){return this.mainTargetInternal}setRecordStartTime(e){this.recordStartTimeInternal=e}recordStartTime(){return this.recordStartTimeInternal}setFilters(e){this.filtersInternal=e}filters(){return this.filtersInternal}isVisible(e){return this.filtersInternal.every((t=>t.accept(e)))}async setTracingModel(e,t=!1){this.tracingModelInternal=e,this.timelineModelInternal.setEvents(e,t),await this.addSourceMapListeners();const n=this.timelineModelInternal.tracks().filter((e=>e.type===TimelineModel.TimelineModel.TrackType.MainThread&&e.forMainFrame&&e.events.length)).map((e=>{const t=e.events[0];return{thread:t.thread,time:t.startTime}}));this.frameModelInternal.addTraceEvents(this.mainTargetInternal,this.timelineModelInternal.inspectedTargetEvents(),n),this.autoWindowTimes()}async addSourceMapListeners(){const e=new Set;for(const t of this.timelineModel().cpuProfiles())for(const n of t.cpuProfileData.nodes()||[]){if(!n)continue;const i=this.#e(n,t.target);i&&e.add(i)}for(const t of e)t.sourceMapManager().addEventListener(SDK.SourceMapManager.Events.SourceMapAttached,this.#t,this);await this.#n()}#e(e,t){const n=t?.model(SDK.DebuggerModel.DebuggerModel);if(!n)return null;const i=n.scriptForId(String(e.callFrame.scriptId));return!i||i.sourceMapURL?n:null}async#n(){for(const e of this.timelineModel().cpuProfiles()){const t=e.target;for(const n of e.cpuProfileData.nodes()||[]){const e=await SourceMapScopes.NamesResolver.resolveProfileFrameFunctionName(n.callFrame,t);n.setFunctionName(e)}}}async#t(){this.willResolveNames||(this.willResolveNames=!0,setTimeout(this.resolveNamesAndUpdate.bind(this),500))}async resolveNamesAndUpdate(){this.willResolveNames=!1,await this.#n(),this.dispatchEventToListeners(Events.NamesResolved)}tracingModel(){if(!this.tracingModelInternal)throw"call setTracingModel before accessing PerformanceModel";return this.tracingModelInternal}timelineModel(){return this.timelineModelInternal}frames(){return this.frameModelInternal.getFrames()}frameModel(){return this.frameModelInternal}setWindow(e,t){this.windowInternal=e,this.dispatchEventToListeners(Events.WindowChanged,{window:e,animate:t})}window(){return this.windowInternal}minimumRecordTime(){return this.timelineModelInternal.minimumRecordTime()}maximumRecordTime(){return this.timelineModelInternal.maximumRecordTime()}autoWindowTimes(){const e=this.timelineModelInternal;let t=[];for(const n of e.tracks())n.type===TimelineModel.TimelineModel.TrackType.MainThread&&n.forMainFrame&&(t=n.tasks);if(!t.length)return void this.setWindow({left:e.minimumRecordTime(),right:e.maximumRecordTime()});function n(e,n){let i=e,r=(t[i].startTime+t[i].endTime)/2,o=0;const a=Math.sign(n-e);for(let s=e;s!==n;s+=a){const e=t[s],n=(e.startTime+e.endTime)/2;o<.1*Math.abs(r-n)&&(i=s,r=n,o=0),o+=e.duration}return i}const i=n(t.length-1,0),r=n(0,i);let o=t[r].startTime,a=t[i].endTime;const s=a-o;s<.1*(e.maximumRecordTime()-e.minimumRecordTime())?(o=e.minimumRecordTime(),a=e.maximumRecordTime()):(o=Math.max(o-.05*s,e.minimumRecordTime()),a=Math.min(a+.05*s,e.maximumRecordTime())),this.setWindow({left:o,right:a})}}export var Events;!function(e){e.WindowChanged="WindowChanged",e.NamesResolved="NamesResolved"}(Events||(Events={}));