返回 DeepSeek-Reasonix
useCommittedCommand.ts
根目录 / desktop / frontend / src / lib / useCommittedCommand.ts
1 import { useMemo } from "react";
2 import { useCommittedSlot, type CommittedSlot } from "./useCommittedSlot";
3
4 export type { CommandCancellationReason, CommandOutcome } from "./commandOutcome";
5
6 type AnyCommand = (...args: never[]) => unknown;
7
8 function bindCommittedCommand<Command extends AnyCommand>(slot: CommittedSlot<Command>) {
9 return (...args: Parameters<Command>): ReturnType<Command> | undefined => {
10 if (slot.phase !== "ready") return undefined;
11 return slot.value?.(...args) as ReturnType<Command> | undefined;
12 };
13 }
14
15 /** Stable event entry; DOM ref attachment is a separate mutation-phase contract. */
16 export function useCommittedCommand<Command extends AnyCommand>(command: Command): Command {
17 const slot = useCommittedSlot(command);
18 return useMemo(() => bindCommittedCommand(slot), [slot]) as Command;
19 }
20
20 lines TYPESCRIPT