import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import type { Source, StreamEvent } from "../shared/ipc"; import VirtualLog from "./components/VirtualLog"; type SourceFilter = "all" | Source; const CAP = 3000; const sources: Array<{ id: SourceFilter; label: string }> = [ { id: "all", label: "全部" }, { id: "build", label: "build" }, { id: "network", label: "network" }, { id: "tools", label: "tools" }, ]; function numberFrom(testId: string): number { const text = document.querySelector(`[data-testid="${testId}"]`) ?.textContent ?? "0"; const match = text.match(/\d+/); return match ? Number(match[0]) : 0; } export default function App() { const [events, setEvents] = useState([]); const [received, setReceived] = useState(0); const [emitted, setEmitted] = useState(0); const [running, setRunning] = useState(false); const [paused, setPaused] = useState(false); const [pausedCount, setPausedCount] = useState(0); const [filterSource, setFilterSource] = useState("all"); const [query, setQuery] = useState(""); const [following, setFollowing] = useState(true); const [exportInfo, setExportInfo] = useState("尚未导出"); const pausedRef = useRef(false); const frozenLength = useRef(null); const scrollRef = useRef(null); const eventsRef = useRef([]); eventsRef.current = events; useEffect(() => { const unsubscribe = window.api.stream.onBatch((batch) => { setReceived((value) => value + batch.events.length); setEmitted(batch.emitted); if (pausedRef.current) { setPausedCount((value) => value + batch.events.length); } setEvents((previous) => [...previous, ...batch.events]); }); return unsubscribe; }, []); useEffect(() => { if (events.length > CAP) { setEvents((previous) => previous.slice(previous.length - CAP)); } }, [events]); const base = useMemo(() => { if (!paused) return events; const limit = frozenLength.current ?? events.length; return events.slice(0, limit); }, [events, paused]); const filtered = useMemo(() => { const text = query.trim().toLowerCase(); return base.filter( (event) => (filterSource === "all" || event.source === filterSource) && (text.length === 0 || event.text.toLowerCase().includes(text)), ); }, [base, filterSource, query]); useEffect(() => { if (!following || paused) return; const element = scrollRef.current; if (!element) return; const frame = requestAnimationFrame(() => { element.scrollTop = element.scrollHeight; }); return () => cancelAnimationFrame(frame); }, [events.length, filtered.length, following, paused]); const pauseView = useCallback((value: boolean) => { pausedRef.current = value; if (value) { frozenLength.current = eventsRef.current.length; setPaused(true); setPausedCount(0); return; } frozenLength.current = null; setPaused(false); setPausedCount(0); setFollowing(true); }, []); const reset = useCallback(() => { setEvents([]); setReceived(0); setEmitted(0); setPausedCount(0); setExportInfo("尚未导出"); setFollowing(true); pausedRef.current = false; frozenLength.current = null; setPaused(false); }, []); const start = useCallback(async () => { await window.api.stream.start(); setRunning(true); }, []); const stop = useCallback(async () => { const stats = await window.api.stream.stop(); setRunning(false); setEmitted(stats.emitted); }, []); const exportEvents = useCallback(async () => { const result = await window.api.stream.exportEvents(eventsRef.current); setExportInfo( `已导出 ${result.lines} 行 · ${result.bytes} 字节 · ${result.file}`, ); }, []); useEffect(() => { window.__setFilter = (source) => setFilterSource(source); window.__setQuery = (value) => { setQuery(value); setFollowing(true); }; window.__pause = () => pauseView(true); window.__prepareShots = async () => { reset(); setFilterSource("all"); setQuery(""); await window.api.stream.start(); setRunning(true); }; window.__reset = () => { reset(); setFilterSource("all"); setQuery(""); }; window.__smoke = async () => { const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const click = (testId: string): void => { document .querySelector(`[data-testid="${testId}"]`) ?.click(); }; await window.api.stream.stop(); window.__reset?.(); await wait(120); click("start"); await wait(450); const receivedAtPause = numberFrom("received"); click("pause-toggle"); await wait(450); const pausedBuffered = numberFrom("paused-count"); click("pause-toggle"); await wait(120); const receivedAfterResume = numberFrom("received"); const stats = await window.api.stream.stop(); await wait(250); const received = numberFrom("received"); const emitted = stats.emitted; window.__setFilter?.("build"); await wait(160); const rows = Array.from( document.querySelectorAll('[data-testid="log-row"]'), ); const buildOnly = rows.length > 0 && rows.every((row) => row.getAttribute("data-source") === "build"); const visibleRows = rows.length; window.__setFilter?.("all"); await wait(120); const scroller = document.querySelector( '[data-testid="scroll-container"]', ); if (scroller) { scroller.scrollTop = 0; scroller.dispatchEvent(new Event("scroll", { bubbles: true })); } await wait(120); const followButton = document.querySelector( '[data-testid="follow"]', ); const followPaused = followButton?.textContent?.includes("回到最新") ?? false; click("follow"); await wait(150); const atBottom = scroller !== null && scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < 30; const followRestored = followPaused && atBottom; click("export"); await wait(350); const exportText = document.querySelector('[data-testid="export-result"]') ?.textContent ?? ""; const linesMatch = exportText.match(/已导出 (\d+) 行/); const exportLines = linesMatch ? Number(linesMatch[1]) : 0; const bytesMatch = exportText.match(/(\d+) 字节/); const exportBytes = bytesMatch ? Number(bytesMatch[1]) : 0; return { pausedBuffered, receivedAtPause, receivedAfterResume, received, emitted, buildOnly, followRestored, exportLines, exportBytes, visibleRows, }; }; }, [pauseView, reset]); const handleScroll = (element: HTMLDivElement): void => { const distance = element.scrollHeight - element.scrollTop - element.clientHeight; if (distance > 24) { if (following) setFollowing(false); } else if (!following) { setFollowing(true); } }; const stored = events.length; const dropped = Math.max(0, received - events.length); const pausedLabel = paused ? `已暂停 · 新增 ${pausedCount} 条` : "实时跟随"; return (

多路事件流工作台

{running ? "运行中" : "已停止"} {paused ? ( <> 已暂停 · 新增{" "} {pausedCount} 条 ) : ( pausedLabel )}
{[ ["接收", received, "received"], ["保留", stored, "stored"], ["显示", filtered.length, "shown"], ["丢弃", dropped, "dropped"], ].map(([label, value, id]) => (
{label}
{value}
))}
主进程已发
{emitted}
显示 {filtered.length} / 保留 {stored} / 接收 {received}

上滑自动暂停跟随;点「回到最新」恢复。暂停视图时列表不移动,但计数继续增长。

); } declare global { interface Window { api: { stream: { start: () => Promise<{ started: boolean }>; stop: () => Promise<{ emitted: number; running: boolean }>; stats: () => Promise<{ emitted: number; running: boolean }>; exportEvents: ( events: StreamEvent[], ) => Promise<{ file: string; lines: number; bytes: number }>; onBatch: ( callback: (batch: { events: StreamEvent[]; emitted: number }) => void, ) => () => void; }; }; __setFilter?: (source: SourceFilter) => void; __setQuery?: (value: string) => void; __pause?: () => void; __prepareShots?: () => Promise; __reset?: () => void; __smoke?: () => Promise; } }