返回 DeepSeek-Reasonix
usePromptStop.ts
根目录 / desktop / frontend / src / lib / usePromptStop.ts
1 import { useCallback, useRef, useState } from "react";
2
3 /** Stop captures its source synchronously and has a lock independent of answers. */
4 export function usePromptStop(onStop: () => void | Promise<void>) {
5 const pending = useRef(false);
6 const [stopping, setStopping] = useState(false);
7 const [stopFailed, setStopFailed] = useState(false);
8 const stopTask = useCallback(() => {
9 if (pending.current) return;
10 pending.current = true;
11 setStopping(true);
12 setStopFailed(false);
13 const failed = () => {
14 pending.current = false;
15 setStopping(false);
16 setStopFailed(true);
17 };
18 try {
19 // Calling before the first await keeps committed commands on the tab
20 // whose Stop button was clicked, even if navigation happens next.
21 void Promise.resolve(onStop()).catch(failed);
22 } catch {
23 failed();
24 }
25 }, [onStop]);
26 return { stopping, stopFailed, stopTask };
27 }
28
28 lines TYPESCRIPT