@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 2.16 kB
JavaScript
import*as Common from"../../core/common/common.js";import{EmulationModel}from"./EmulationModel.js";import{TargetManager}from"./TargetManager.js";let throttlingManagerInstance;export class CPUThrottlingManager extends Common.ObjectWrapper.ObjectWrapper{#t;#e;#n;constructor(){super(),this.#t=CPUThrottlingRates.NoThrottling,TargetManager.instance().observeModels(EmulationModel,this)}static instance(t={forceNew:null}){const{forceNew:e}=t;return throttlingManagerInstance&&!e||(throttlingManagerInstance=new CPUThrottlingManager),throttlingManagerInstance}cpuThrottlingRate(){return this.#t}setCPUThrottlingRate(t){this.#t=t;for(const t of TargetManager.instance().models(EmulationModel))t.setCPUThrottlingRate(this.#t);this.dispatchEventToListeners(Events.RateChanged,this.#t)}setHardwareConcurrency(t){this.#e=t;for(const e of TargetManager.instance().models(EmulationModel))e.setHardwareConcurrency(t);this.dispatchEventToListeners(Events.HardwareConcurrencyChanged,this.#e)}hasPrimaryPageTargetSet(){try{return null!==TargetManager.instance().primaryPageTarget()}catch{return!1}}async getHardwareConcurrency(){const t=TargetManager.instance().primaryPageTarget(),e=this.#n;if(!t)return new Promise(e?t=>{this.#n=n=>{t(n),e(n)}}:t=>{this.#n=t});const n=await t.runtimeAgent().invoke_evaluate({expression:"navigator.hardwareConcurrency",returnByValue:!0,silent:!0,throwOnSideEffect:!0}),r=n.getError();if(r)throw new Error(r);const{result:a,exceptionDetails:o}=n;if(o)throw new Error(o.text);return a.value}modelAdded(t){if(this.#t!==CPUThrottlingRates.NoThrottling&&t.setCPUThrottlingRate(this.#t),void 0!==this.#e&&t.setHardwareConcurrency(this.#e),this.#n){const t=this.#n;this.#n=void 0,this.getHardwareConcurrency().then(t)}}modelRemoved(t){}}export var Events;!function(t){t.RateChanged="RateChanged",t.HardwareConcurrencyChanged="HardwareConcurrencyChanged"}(Events||(Events={}));export function throttlingManager(){return CPUThrottlingManager.instance()}export var CPUThrottlingRates;!function(t){t[t.NoThrottling=1]="NoThrottling",t[t.MidTierMobile=4]="MidTierMobile",t[t.LowEndMobile=6]="LowEndMobile"}(CPUThrottlingRates||(CPUThrottlingRates={}));