-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13146 from sebavan/master
Update Pressure Observer to latest version of the WICG spec Former-commit-id: 11fda9004a0aca651f6a82f50beae41a36257cde
- Loading branch information
Showing
7 changed files
with
175 additions
and
123 deletions.
There are no files selected for viewing
This file contains 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 file contains 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 file was deleted.
Oops, something went wrong.
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { Nullable } from "../types"; | ||
import { Observable } from "./observable"; | ||
|
||
/** | ||
* A wrapper for the experimental pressure api which allows a callback to be called whenever certain thresholds are met. | ||
*/ | ||
export class PressureObserverWrapper { | ||
private _observer: Nullable<PressureObserver> = null; | ||
private _currentState: PressureRecord[] = []; | ||
|
||
/** | ||
* An event triggered when the cpu usage/speed meets certain thresholds. | ||
* Note: pressure is an experimental API. | ||
*/ | ||
public onPressureChanged = new Observable<PressureRecord[]>(); | ||
|
||
/** | ||
* A pressure observer will call this callback, whenever these thresholds are met. | ||
* @param options An object containing the thresholds used to decide what value to to return for each update property (average of start and end of a threshold boundary). | ||
*/ | ||
constructor(options?: PressureObserverOptions) { | ||
if (PressureObserverWrapper.IsAvailable) { | ||
this._observer = new PressureObserver((update) => { | ||
this._currentState = update; | ||
this.onPressureChanged.notifyObservers(update); | ||
}, options); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if PressureObserver is available for use, false otherwise. | ||
*/ | ||
public static get IsAvailable() { | ||
return typeof PressureObserver !== "undefined" && PressureObserver.supportedSources.includes("cpu"); | ||
} | ||
|
||
/** | ||
* Method that must be called to begin observing changes, and triggering callbacks. | ||
* @param source defines the source to observe | ||
*/ | ||
observe(source: PressureSource): void { | ||
try { | ||
this._observer?.observe(source); | ||
this.onPressureChanged.notifyObservers(this._currentState); | ||
} catch { | ||
// Ignore error | ||
} | ||
} | ||
|
||
/** | ||
* Method that must be called to stop observing changes and triggering callbacks (cleanup function). | ||
* @param source defines the source to unobserve | ||
*/ | ||
unobserve(source: PressureSource): void { | ||
try { | ||
this._observer?.unobserve(source); | ||
} catch { | ||
// Ignore error | ||
} | ||
} | ||
|
||
/** | ||
* Release the associated resources. | ||
*/ | ||
dispose() { | ||
this._observer?.disconnect(); | ||
this._observer = null; | ||
this.onPressureChanged.clear(); | ||
} | ||
} |
This file contains 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 file contains 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