Created
December 30, 2021 12:30
-
-
Save drslump/b652acaebd6a874e66e4de51549c5713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This module uses an experimental API that is only available on Chromium using | |
// a trial or by enabling a browser flag. It's unlikely that the API will get | |
// standardized and if it is it'll probably end up having a quite different form, | |
// that's to say that we should remove this once the trial is over :) | |
// | |
// - Spec: https://oyiptong.github.io/compute-pressure/ | |
// - Status: https://chromestatus.com/feature/5597608644968448 | |
// - Trial: https://developer.chrome.com/origintrials/#/view_trial/1838594547874004993 | |
// - Chrome flag: Experimental Web Platform features | |
// | |
// NOTE: as of Aug'21 (Chrome 95) the only OS supported is Linux! | |
// | |
export type ComputePressureObserverOptions = { | |
cpuUtilizationThresholds: number[] | |
cpuSpeedThresholds: number[] | |
} | |
export type ComputePressureObserverUpdate = { | |
cpuSpeed: number | |
cpuUtilization: number | |
options: ComputePressureObserverOptions | |
} | |
export type ComputePressureUpdateCallback = ( | |
update: ComputePressureObserverUpdate, | |
observer: any // ComputePressureObserver (how to do a class forward decl?) | |
) => void | |
declare class ComputePressureObserver { | |
constructor(callback: ComputePressureUpdateCallback, options?: ComputePressureObserverOptions) | |
observe(): void | |
unobserve(): void // this one is from the spec | |
stop(): void // this one is from latest Chrome | |
} | |
export const COMPUTE_PRESSURE_SUPPORTED = typeof ComputePressureObserver === 'function' | |
// just a place to hold onto references to fight the GC | |
const hodor = new Set() | |
/** | |
* Starts a compute pressure observer if supported by the browser. | |
* The return is a canceller function to stop observing. | |
*/ | |
export default (cb: ComputePressureUpdateCallback) => { | |
if (!COMPUTE_PRESSURE_SUPPORTED) { | |
console.warn('ComputePressure interface is not support in this browser') | |
return () => {} | |
} | |
// chrome's prototype seems to be limited to 3 threshold only | |
const observer = new ComputePressureObserver(cb, { | |
cpuSpeedThresholds: [0.5, 0.75], | |
cpuUtilizationThresholds: [0.35, 0.65], | |
}) | |
observer.observe() | |
// need to hold onto it otherwise it'll be automatically stopped by GC | |
hodor.add(observer) | |
return () => { | |
hodor.delete(observer) | |
if (typeof observer.unobserve === 'function') { | |
observer.unobserve() | |
} else if (typeof observer.stop === 'function') { | |
observer.stop() | |
} else { | |
console.warn('neither unobserve nor close are defined :shrug:') | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment