Files
Jyotisha/frontend/src/lib/new-chat-timing.ts
T

46 lines
1.4 KiB
TypeScript

/**
* 「新建对话」 timing (BUG-1104). Browser Performance marks only: nothing is
* sent anywhere and nothing personal is recorded. In DevTools → Performance
* (or `performance.getEntriesByType("measure")`) a slow case shows as
* `jyotisha:new-chat (warm|cold)` with its duration from click to a ready Home.
*/
const CLICK = "jyotisha:new-chat:click";
let pending = false;
let path: "warm" | "cold" | null = null;
function perf(): Performance | null {
return typeof performance !== "undefined" && typeof performance.mark === "function" ? performance : null;
}
/** The sidebar's 「新建对话」 was clicked (either the link off `/` or the button on it). */
export function markNewChatClick(): void {
const p = perf();
if (!p) return;
try {
p.clearMarks(CLICK);
p.mark(CLICK);
pending = true;
path = null;
} catch {
// Timing is best-effort.
}
}
/** Which way the Home that mounts next decided to start. */
export function noteHomeStartPath(kind: "warm" | "cold"): void {
if (pending) path ??= kind;
}
/** Home is revealed and usable; closes the measure opened by the click, once. */
export function markHomeReadyAfterNewChat(): void {
const p = perf();
if (!p || !pending) return;
pending = false;
try {
p.measure(`jyotisha:new-chat (${path ?? "unknown"})`, CLICK);
} catch {
// The click mark was cleared by another tool; nothing to measure.
}
}