mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2026-04-20 05:19:28 +01:00
Fix portal background opening health metric (#2425)
* Fix portal background opening * fix rAF
This commit is contained in:
@@ -1,25 +1,41 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import MetricScenario from "./MetricEvents";
|
import MetricScenario from "./MetricEvents";
|
||||||
import { scenarioMonitor } from "./ScenarioMonitor";
|
|
||||||
import { ApplicationMetricPhase, CommonMetricPhase } from "./ScenarioConfig";
|
import { ApplicationMetricPhase, CommonMetricPhase } from "./ScenarioConfig";
|
||||||
|
import { scenarioMonitor } from "./ScenarioMonitor";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to automatically complete the Interactive phase when the component becomes interactive.
|
* Completes the Interactive phase once the browser is ready to paint.
|
||||||
* Uses requestAnimationFrame to complete after the browser has painted.
|
|
||||||
*
|
*
|
||||||
* Calls scenarioMonitor directly (not via React context) so that the effect dependencies
|
* Uses requestAnimationFrame with a setTimeout fallback. In foreground tabs rAF fires
|
||||||
* are only [scenario, enabled] — both stable primitives. This prevents re-renders from
|
* first (~16 ms) giving an accurate "browser painted" signal. In background tabs browsers
|
||||||
* cancelling the pending rAF due to an unstable context function reference.
|
* suspend rAF indefinitely, so the setTimeout fallback (1 s) completes the phase instead —
|
||||||
|
* well within the 10 s scenario timeout — preventing false-negative unhealthy reports.
|
||||||
*/
|
*/
|
||||||
export function useInteractive(scenario: MetricScenario, enabled = true) {
|
export function useInteractive(scenario: MetricScenario, enabled = true) {
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const id = requestAnimationFrame(() => {
|
|
||||||
|
let completed = false;
|
||||||
|
const complete = () => {
|
||||||
|
if (completed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
completed = true;
|
||||||
|
cancelAnimationFrame(rafId);
|
||||||
|
clearTimeout(timeoutId);
|
||||||
scenarioMonitor.completePhase(scenario, CommonMetricPhase.Interactive);
|
scenarioMonitor.completePhase(scenario, CommonMetricPhase.Interactive);
|
||||||
});
|
};
|
||||||
return () => cancelAnimationFrame(id);
|
|
||||||
|
const rafId = requestAnimationFrame(complete);
|
||||||
|
// Fallback for background tabs where rAF is suspended.
|
||||||
|
const timeoutId = setTimeout(complete, 1000);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelAnimationFrame(rafId);
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
};
|
||||||
}, [scenario, enabled]);
|
}, [scenario, enabled]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user