import { ref } from "vue"; import { createNativeNavigationProfiler, type NativeNavigationProfiler, type NativeProfilerReport, type NativeRouterRuntime, } from "@native-vue-router/core"; import { pwaBuildId } from "./pwa"; let profiler: NativeNavigationProfiler | undefined; export const profilerRecording = ref(false); export const profilerHasCapture = ref(false); function instance(runtime: NativeRouterRuntime) { profiler ??= createNativeNavigationProfiler(runtime, { metadata: { app: "nvr-messenger-demo", build: pwaBuildId }, }); return profiler; } export function startDemoProfile(runtime: NativeRouterRuntime) { const active = instance(runtime); active.start(); profilerRecording.value = true; profilerHasCapture.value = true; } export function stopDemoProfile(runtime: NativeRouterRuntime) { const report = instance(runtime).stop(); profilerRecording.value = false; return report; } export function snapshotDemoProfile(runtime: NativeRouterRuntime) { return instance(runtime).snapshot(); } export async function shareDemoProfile( runtime: NativeRouterRuntime, report?: NativeProfilerReport, ) { const active = instance(runtime); const current = report ?? (profilerRecording.value ? stopDemoProfile(runtime) : active.snapshot()); const json = active.toJSON(current); const stamp = new Date() .toISOString() .replaceAll(":", "-") .replaceAll(".", "-"); const filename = `native-vue-router-profile-${stamp}.json`; const file = new File([json], filename, { type: "application/json" }); const shareNavigator = navigator as Navigator & { canShare?: (data: ShareData) => boolean; share?: (data: ShareData) => Promise; }; if (shareNavigator.share && shareNavigator.canShare?.({ files: [file] })) { await shareNavigator.share({ title: "Native Vue Router performance profile", text: "Frame pacing and navigation diagnostics. Route params and query values are omitted.", files: [file], }); return { report: current, method: "shared" as const }; } const url = URL.createObjectURL(file); const link = document.createElement("a"); link.href = url; link.download = filename; link.click(); window.setTimeout(() => URL.revokeObjectURL(url), 1_000); return { report: current, method: "downloaded" as const }; }