phase 5 part 1: typescript, vue, react, ssr(Next.js 16) topics with lessons, scenarios, interviews, verified examples
This commit is contained in:
1 parent
7a32d2c9a6
commit
7b5cae7d24
39 files changed
+2482
-48
No files matched your search
@@ -0,0 +1,35 @@
|
||||
// 不自动运行(依赖 react,需要在 React 项目中使用)
|
||||
// 用 useReducer 管理任务状态,用 useEffect 订阅服务端事件,并在卸载 / 任务变化时清理。
|
||||
import { useEffect, useReducer } from "react";
|
||||
import { initialState, taskReducer, type TaskEvent } from "./taskReducer.ts";
|
||||
|
||||
export function TaskPanel({ taskId, subscribe }: {
|
||||
taskId: string;
|
||||
subscribe: (id: string, onEvent: (e: TaskEvent) => void) => () => void; // 返回取消订阅函数
|
||||
}) {
|
||||
const [state, dispatch] = useReducer(taskReducer, initialState);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch({ type: "submitted", id: taskId });
|
||||
const unsubscribe = subscribe(taskId, dispatch); // dispatch 引用稳定,不需要放进依赖数组
|
||||
return unsubscribe; // 清理:taskId 变化或组件卸载时取消订阅,避免旧任务的事件串进来
|
||||
}, [taskId, subscribe]);
|
||||
|
||||
return (
|
||||
<section aria-live="polite">
|
||||
<p>状态:{state.status}</p>
|
||||
<ol>
|
||||
{state.steps.map((s) => (
|
||||
<li key={s.name}>{s.done ? "✓" : "…"} {s.name}</li>
|
||||
))}
|
||||
</ol>
|
||||
{state.status === "failed" && (
|
||||
<p role="alert">
|
||||
{state.error} <button onClick={() => dispatch({ type: "retry" })}>重试</button>
|
||||
</p>
|
||||
)}
|
||||
{state.result && <a href={state.result.url}>{state.result.title}</a>}
|
||||
{state.status === "running" && <button onClick={() => dispatch({ type: "cancel" })}>取消</button>}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"type":"module"}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 运行:node taskReducer-demo.ts —— 验证乱序 / 重复 / 迟到事件被丢弃,失败后可重试
|
||||
import { initialState, taskReducer, type TaskEvent } from "./taskReducer.ts";
|
||||
|
||||
const events: TaskEvent[] = [
|
||||
{ type: "submitted", id: "T1" },
|
||||
{ type: "progress", seq: 1, step: "查询数据", done: false },
|
||||
{ type: "progress", seq: 2, step: "查询数据", done: true },
|
||||
{ type: "progress", seq: 1, step: "查询数据", done: false }, // 重连后收到的旧事件:丢弃
|
||||
{ type: "progress", seq: 3, step: "生成图表", done: true },
|
||||
{ type: "succeeded", seq: 4, result: { title: "9 月报表", url: "/reports/9" } },
|
||||
{ type: "failed", seq: 5, error: "迟到的失败事件", retryable: false }, // 终态之后:丢弃
|
||||
];
|
||||
const final = events.reduce(taskReducer, initialState);
|
||||
console.log(final.status, final.steps, final.result);
|
||||
if (final.status !== "succeeded" || final.steps.length !== 2 || !final.steps.every((x) => x.done)) {
|
||||
throw new Error("断言失败");
|
||||
}
|
||||
const failed = [{ type: "submitted", id: "T2" }, { type: "failed", seq: 1, error: "超时", retryable: true }, { type: "retry" }]
|
||||
.reduce((st, ev) => taskReducer(st, ev as TaskEvent), initialState);
|
||||
if (failed.status !== "queued" || failed.error !== null) throw new Error("断言失败");
|
||||
console.log("重试后:", failed.status);
|
||||
@@ -0,0 +1,54 @@
|
||||
// 长任务(例如 Agent 生成报表)的前端状态机:用 reducer 管理,所有状态转换集中在一处,便于测试。
|
||||
// 自检:node taskReducer-demo.ts
|
||||
|
||||
export type TaskStatus = "queued" | "running" | "waiting_input" | "succeeded" | "failed" | "cancelled";
|
||||
|
||||
export interface TaskState {
|
||||
id: string | null;
|
||||
status: TaskStatus | "idle";
|
||||
steps: { name: string; done: boolean }[];
|
||||
result: { title: string; url: string } | null;
|
||||
error: string | null;
|
||||
version: number; // 服务端事件的序号:丢弃过期 / 乱序的事件
|
||||
}
|
||||
|
||||
export type TaskEvent =
|
||||
| { type: "submitted"; id: string }
|
||||
| { type: "progress"; seq: number; step: string; done: boolean }
|
||||
| { type: "need_input"; seq: number; question: string }
|
||||
| { type: "succeeded"; seq: number; result: { title: string; url: string } }
|
||||
| { type: "failed"; seq: number; error: string; retryable: boolean }
|
||||
| { type: "cancel" }
|
||||
| { type: "retry" };
|
||||
|
||||
export const initialState: TaskState = { id: null, status: "idle", steps: [], result: null, error: null, version: 0 };
|
||||
|
||||
const FINAL: ReadonlySet<TaskState["status"]> = new Set(["succeeded", "failed", "cancelled"]);
|
||||
|
||||
export function taskReducer(s: TaskState, e: TaskEvent): TaskState {
|
||||
switch (e.type) {
|
||||
case "submitted":
|
||||
return { ...initialState, id: e.id, status: "queued" };
|
||||
case "cancel":
|
||||
return FINAL.has(s.status) ? s : { ...s, status: "cancelled" };
|
||||
case "retry":
|
||||
return s.status === "failed" ? { ...s, status: "queued", error: null } : s;
|
||||
default:
|
||||
// 服务端事件:终态之后、或者序号不大于当前版本的事件一律丢弃(重连后可能收到重复事件)
|
||||
if (FINAL.has(s.status) || e.seq <= s.version) return s;
|
||||
}
|
||||
switch (e.type) {
|
||||
case "progress": {
|
||||
const steps = s.steps.some((x) => x.name === e.step)
|
||||
? s.steps.map((x) => (x.name === e.step ? { ...x, done: e.done } : x))
|
||||
: [...s.steps, { name: e.step, done: e.done }];
|
||||
return { ...s, status: "running", steps, version: e.seq };
|
||||
}
|
||||
case "need_input":
|
||||
return { ...s, status: "waiting_input", version: e.seq };
|
||||
case "succeeded":
|
||||
return { ...s, status: "succeeded", result: e.result, version: e.seq };
|
||||
case "failed":
|
||||
return { ...s, status: "failed", error: e.retryable ? `${e.error}(可重试)` : e.error, version: e.seq };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user