UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.23 kB
import*as SDK from"../../../../core/sdk/sdk.js";import*as Bindings from"../../../../models/bindings/bindings.js";import*as Workspace from"../../../../models/workspace/workspace.js";import*as SourceFrame from"../source_frame/source_frame.js";let performanceInstance,memoryInstance;export class Performance{helper;constructor(){this.helper=new Helper(SourceFrame.SourceFrame.DecoratorType.PERFORMANCE)}static instance(e={forceNew:null}){const{forceNew:t}=e;return performanceInstance&&!t||(performanceInstance=new Performance),performanceInstance}reset(){this.helper.reset()}appendLegacyCPUProfile(e,t){const o=[e.profileHead],r=(e.profileEndTime-e.profileStartTime)/e.totalHitCount;for(;o.length;){const e=o.pop().children;for(let s=0;s<e.length;++s){const n=e[s];if(o.push(n),n.url&&n.positionTicks)for(let e=0;e<n.positionTicks.length;++e){const o=n.positionTicks[e],s=o.line,i=o.ticks*r;this.helper.addLineData(t,n.url,s,i)}}}}appendCPUProfile(e,t){if(!e.lines)return this.appendLegacyCPUProfile(e,t),void this.helper.scheduleUpdate();if(e.samples){for(let o=1;o<e.samples.length;++o){const r=e.lines[o];if(!r)continue;const s=e.nodeByIndex(o);if(!s)continue;const n=Number(s.scriptId)||s.url;if(!n)continue;const i=e.timestamps[o]-e.timestamps[o-1];this.helper.addLineData(t,n,r,i)}this.helper.scheduleUpdate()}}}export class Memory{helper;constructor(){this.helper=new Helper(SourceFrame.SourceFrame.DecoratorType.MEMORY)}static instance(e={forceNew:null}){const{forceNew:t}=e;return memoryInstance&&!t||(memoryInstance=new Memory),memoryInstance}reset(){this.helper.reset()}appendHeapProfile(e,t){const o=this.helper;!function e(r){if(r.children.forEach(e),!r.selfSize)return;const s=Number(r.callFrame.scriptId)||r.callFrame.url;if(!s)return;const n=r.callFrame.lineNumber+1;o.addLineData(t,s,n,r.selfSize)}(e.head),o.scheduleUpdate()}}export class Helper{type;locationPool;updateTimer;lineData;constructor(e){this.type=e,this.locationPool=new Bindings.LiveLocation.LiveLocationPool,this.updateTimer=null,this.reset()}reset(){this.lineData=new Map,this.scheduleUpdate()}addLineData(e,t,o,r){let s=this.lineData.get(e);s||(s=new Map,this.lineData.set(e,s));let n=s.get(t);n||(n=new Map,s.set(t,n)),n.set(o,(n.get(o)||0)+r)}scheduleUpdate(){this.updateTimer||(this.updateTimer=window.setTimeout((()=>{this.updateTimer=null,this.doUpdate()}),0))}async doUpdate(){this.locationPool.disposeAll();const e=new Map,t=[];for(const[o,r]of this.lineData){const s=o?o.model(SDK.DebuggerModel.DebuggerModel):null;for(const[o,n]of r){const r=Workspace.Workspace.WorkspaceImpl.instance();if(s){const r=Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance();for(const i of n){const n=i[0]-1,a=i[1],c="string"==typeof o?s.createRawLocationByURL(o,n,0):s.createRawLocationByScriptId(String(o),n,0);c&&t.push(r.rawLocationToUILocation(c).then((t=>{if(t){let o=e.get(t.uiSourceCode);o||(o=new Map,e.set(t.uiSourceCode,o)),o.set(t.lineNumber+1,a)}})))}}else if("string"==typeof o){const t=r.uiSourceCodeForURL(o);t&&e.set(t,n)}}await Promise.all(t);for(const[t,o]of e)t.setDecorationData(this.type,o)}for(const t of Workspace.Workspace.WorkspaceImpl.instance().uiSourceCodes())e.has(t)||t.setDecorationData(this.type,void 0)}}