示例工程 14-react-bootstrap

14-react-bootstrap/app/.tsc/src/App.js

javascript · 10 KB

import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { useIpcOnce } from "./hooks/useIpcOnce";
const rendererLoadedAt = Date.now();
let hmrUpdates = 0;
if (import.meta.hot) {
    import.meta.hot.on("vite:afterUpdate", () => {
        hmrUpdates += 1;
    });
}
const tabs = [
    { id: "strict", label: "StrictMode" },
    { id: "architecture", label: "目录分层" },
    { id: "hmr", label: "HMR 边界" },
];
const emptyCounts = {
    counts: {},
    idem: { requests: 0, records: 0 },
};
function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
function CountCard({ label, value, hint, testId, }) {
    return (_jsxs("div", { className: "card", children: [_jsx("div", { className: "card-label", children: label }), _jsx("div", { className: "card-value", "data-testid": testId, children: value }), _jsx("p", { className: "hint", children: hint })] }));
}
function NaiveProbe({ refresh }) {
    useEffect(() => {
        void window.api.bump("naive").then(refresh);
    }, [refresh]);
    return null;
}
function RefProbe({ refresh }) {
    useIpcOnce(() => {
        void window.api.bump("ref").then(refresh);
    });
    return null;
}
function IdempotentProbe({ refresh }) {
    useEffect(() => {
        void window.api.idem("strict-mount").then(refresh);
    }, [refresh]);
    return null;
}
function ProbeSet({ refresh }) {
    return (_jsxs(_Fragment, { children: [_jsx(NaiveProbe, { refresh: refresh }), _jsx(RefProbe, { refresh: refresh }), _jsx(IdempotentProbe, { refresh: refresh })] }));
}
function EventProbe({ refresh }) {
    return (_jsx("button", { className: "mini", "data-testid": "event-button", onClick: () => {
            void window.api.bump("event").then(refresh);
        }, children: "\u70B9\u6211\u6267\u884C\u4E00\u6B21 IPC" }));
}
function StrictTab({ counts, revision, refresh, }) {
    return (_jsxs("div", { className: "section", children: [_jsx("h2", { children: "StrictMode \u4E0B\u7684\u526F\u4F5C\u7528" }), _jsxs("p", { className: "hint", children: ["\u5F00\u53D1\u6A21\u5F0F\u4E0B StrictMode \u4F1A\u6302\u8F7D \u2192 \u6E05\u7406 \u2192 \u518D\u6302\u8F7D\uFF1B\u5199\u5728", " ", _jsx("code", { children: "useEffect" }), " \u91CC\u7684 IPC \u4F1A\u88AB\u8C03\u7528\u4E24\u6B21\u3002\u4E0B\u9762\u56DB\u4E2A\u63A2\u9488\u5C55\u793A\u95EE\u9898\u4E0E\u4E09\u79CD\u4FEE\u6CD5\u3002"] }), _jsx(ProbeSet, { refresh: refresh }, revision), _jsxs("div", { className: "grid summary", children: [_jsx(CountCard, { label: "\u76F4\u63A5\u8C03\u7528\uFF08\u53CD\u4F8B\uFF09", value: counts.counts.naive ?? 0, hint: "effect \u91CC\u76F4\u63A5 bump\uFF0C\u5F00\u53D1\u6A21\u5F0F 2 \u6B21", testId: "naive-count" }), _jsx(CountCard, { label: "useRef \u95E8\u95E9", value: counts.counts.ref ?? 0, hint: "\u540C\u4E00\u4E2A\u5B9E\u4F8B\u53EA\u653E\u884C\u4E00\u6B21", testId: "ref-count" }), _jsx(CountCard, { label: "\u5E42\u7B49\u952E records", value: counts.idem.records, hint: `请求 ${counts.idem.requests} 次,只落库 1 条`, testId: "idem-count" }), _jsx(CountCard, { label: "\u4E8B\u4EF6\u89E6\u53D1", value: counts.counts.event ?? 0, hint: "\u4E0D\u5728\u6302\u8F7D\u65F6\u6267\u884C\uFF0C\u70B9\u51FB\u624D\u8C03\u7528", testId: "event-count" })] }), _jsxs("div", { className: "row", children: [_jsx(EventProbe, { refresh: refresh }), _jsx("span", { className: "hint", children: "\u4E8B\u4EF6\u89E6\u53D1\u662F\u7B2C\u4E09\u79CD\u4FEE\u6CD5\uFF1A\u628A\u526F\u4F5C\u7528\u4ECE effect \u79FB\u5230\u7528\u6237\u52A8\u4F5C\u3002" })] }), _jsxs("table", { className: "table", "data-testid": "strategy-table", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { children: "\u4FEE\u6CD5" }), _jsx("th", { children: "\u9002\u7528" }), _jsx("th", { children: "\u4EE3\u4EF7" })] }) }), _jsxs("tbody", { children: [_jsxs("tr", { children: [_jsx("td", { children: "useRef \u95E8\u95E9" }), _jsx("td", { children: "\u786E\u5B9E\u53EA\u9700\u8981\u6302\u8F7D\u65F6\u6267\u884C\u4E00\u6B21" }), _jsx("td", { children: "\u7EC4\u4EF6\u5B9E\u4F8B\u91CD\u5EFA\u4F1A\u91CD\u65B0\u6267\u884C" })] }), _jsxs("tr", { children: [_jsx("td", { children: "\u5E42\u7B49\u952E" }), _jsx("td", { children: "\u8BF7\u6C42\u53EF\u91CD\u653E\uFF0C\u4E3B\u8FDB\u7A0B\u80FD\u53BB\u91CD" }), _jsx("td", { children: "\u9700\u8981\u8BBE\u8BA1\u7A33\u5B9A\u7684 opId" })] }), _jsxs("tr", { children: [_jsx("td", { children: "\u4E8B\u4EF6\u89E6\u53D1" }), _jsx("td", { children: "\u64CD\u4F5C\u672C\u6765\u5C31\u7531\u7528\u6237\u53D1\u8D77" }), _jsx("td", { children: "\u4E0D\u80FD\u81EA\u52A8\u5B8C\u6210\u521D\u59CB\u5316" })] })] })] })] }));
}
function ArchitectureTab() {
    const tree = `src/
├── main.tsx              # createRoot + StrictMode
├── App.tsx               # 页签与探针编排
├── components/
│   └── (本章把探针留在 App.tsx)
├── hooks/
│   └── useIpcOnce.ts     # 只执行一次的 IPC
├── lib/
│   └── api.ts            # window.api 类型声明
└── styles.css            # 最小原生 CSS,ch15 再上 Tailwind`;
    return (_jsxs("div", { className: "section", children: [_jsx("h2", { children: "\u6E32\u67D3\u8FDB\u7A0B\u76EE\u5F55\u5206\u5C42" }), _jsxs("div", { className: "two-col", children: [_jsx("pre", { className: "code tree-code", "data-testid": "tree-code", children: tree }), _jsxs("div", { children: [_jsxs("div", { className: "panel", children: [_jsx("div", { className: "panel-label", children: "\u4E3A\u4EC0\u4E48\u5148\u5206\u5C42\u518D\u4E0A\u7EC4\u4EF6\u5E93" }), _jsx("p", { className: "hint", children: "\u5165\u53E3\u53EA\u7BA1\u6302\u8F7D\uFF1B\u9875\u9762\u7BA1\u7F16\u6392\uFF1Bhooks \u7BA1\u590D\u7528\uFF1Blib \u7BA1\u5951\u7EA6\u3002ch15 \u7684 Tailwind \u4E0E ch16 \u7684\u7EC4\u4EF6\u5C42\u53EA\u6539\u6837\u5F0F\u4E0E\u7EC4\u4EF6\u76EE\u5F55\uFF0C\u4E0D\u52A8 IPC\u3002" })] }), _jsxs("div", { className: "panel", children: [_jsx("div", { className: "panel-label", children: "CSP \u4E0E\u5165\u53E3" }), _jsxs("p", { className: "hint", children: ["\u5F00\u53D1\u6001 Vite \u6CE8\u5165\u6837\u5F0F\u9700\u8981 ", _jsx("code", { children: "style-src 'unsafe-inline'" }), "\uFF1B \u751F\u4EA7\u6784\u5EFA\u8F93\u51FA\u72EC\u7ACB CSS \u540E\u5E94\u6536\u7D27\u5230 ", _jsx("code", { children: "'self'" }), "\uFF0C\u7B2C 46 \u7AE0\u590D\u6838\u3002"] })] })] })] })] }));
}
function HmrTab({ info }) {
    return (_jsxs("div", { className: "section", children: [_jsx("h2", { children: "HMR \u53EA\u4F5C\u7528\u4E8E\u6E32\u67D3\u8FDB\u7A0B" }), _jsxs("div", { className: "grid summary", children: [_jsx(CountCard, { label: "main PID", value: info?.mainPid ?? "—", hint: "\u6539 renderer \u4E0D\u4F1A\u6362 PID", testId: "main-pid" }), _jsx(CountCard, { label: "renderer \u52A0\u8F7D", value: new Date(rendererLoadedAt).toLocaleTimeString("zh-CN", {
                            hour12: false,
                        }), hint: "HMR \u540E\u6A21\u5757\u65F6\u95F4\u4F1A\u53D8" }), _jsx(CountCard, { label: "HMR", value: import.meta.hot ? "已连接" : "生产构建", hint: "\u53EA\u6709 dev server \u624D\u6709", testId: "hmr-status" }), _jsx(CountCard, { label: "HMR \u66F4\u65B0", value: hmrUpdates, hint: "\u672C\u6B21\u4F1A\u8BDD\u7684 afterUpdate \u6B21\u6570" })] }), _jsxs("div", { className: "panel", children: [_jsx("div", { className: "panel-label", children: "\u73AF\u5883" }), _jsx("pre", { className: "code", "data-testid": "env-code", children: JSON.stringify({
                            DEV: import.meta.env.DEV,
                            MODE: import.meta.env.MODE,
                            protocol: location.protocol,
                            electron: info?.electron ?? "—",
                            chrome: info?.chrome ?? "—",
                            node: info?.node ?? "—",
                        }, null, 2) })] }), _jsxs("p", { className: "hint", children: ["\u624B\u52A8\u9A8C\u8BC1\uFF1A\u4FDD\u6301\u7A97\u53E3\u6253\u5F00\uFF0C\u4FEE\u6539\u672C\u6587\u4EF6\u91CC\u7684\u4EFB\u610F\u6587\u6848\uFF1B\u9875\u9762\u70ED\u66F4\u65B0\uFF0Cmain PID \u4E0D\u53D8\u3002 \u6539 ", _jsx("code", { children: "main/index.ts" }), " \u4F1A\u91CD\u542F\u5E94\u7528\uFF0CPID \u53D8\u5316\u3002"] })] }));
}
if (import.meta.env.DEV) {
    window.__mountError = () => {
        createRoot(document.getElementById("missing-root"));
    };
}
export default function App() {
    const [tab, setTab] = useState("strict");
    const [counts, setCounts] = useState(emptyCounts);
    const [info, setInfo] = useState(null);
    const [revision, setRevision] = useState(0);
    const refresh = useCallback(async () => {
        setCounts(await window.api.counts());
    }, []);
    useEffect(() => {
        void refresh();
        void window.api.info().then(setInfo);
    }, [refresh]);
    useEffect(() => {
        window.__setTab = (next) => setTab(next);
        window.__snapshot = () => ({
            tab,
            counts: counts.counts,
            idem: counts.idem,
        });
        window.__smoke = async () => {
            await window.api.reset();
            setRevision((value) => value + 1);
            await sleep(400);
            const next = await window.api.counts();
            setCounts(next);
            setTab("strict");
            return {
                naive: next.counts.naive ?? 0,
                ref: next.counts.ref ?? 0,
                idemRequests: next.idem.requests,
                idemRecords: next.idem.records,
                event: next.counts.event ?? 0,
            };
        };
    }, [counts, tab]);
    return (_jsxs("div", { children: [_jsxs("header", { className: "header", children: [_jsxs("div", { children: [_jsx("h1", { "data-testid": "app-title", children: "React \u63A5\u5165\u5B9E\u9A8C\u5BA4" }), _jsx("p", { className: "subtitle", children: "React 19 \u00B7 StrictMode \u00B7 HMR \u00B7 \u76EE\u5F55\u5206\u5C42" })] }), _jsx("nav", { className: "tabs", children: tabs.map((item) => (_jsx("button", { className: `tab${tab === item.id ? " active" : ""}`, "data-testid": `tab-${item.id}`, onClick: () => setTab(item.id), children: item.label }, item.id))) })] }), _jsxs("main", { className: "content", children: [tab === "strict" && (_jsx(StrictTab, { counts: counts, revision: revision, refresh: refresh })), tab === "architecture" && _jsx(ArchitectureTab, {}), tab === "hmr" && _jsx(HmrTab, { info: info })] })] }));
}
//# sourceMappingURL=App.js.map