|
1 |
| -import { useState, useCallback, useEffect } from "react"; |
2 |
| -import { ResourceReference, PromptReference } from "@modelcontextprotocol/sdk/types.js"; |
| 1 | +import { useState, useCallback, useEffect, useRef } from "react"; |
| 2 | +import { |
| 3 | + ResourceReference, |
| 4 | + PromptReference, |
| 5 | +} from "@modelcontextprotocol/sdk/types.js"; |
3 | 6 |
|
4 | 7 | interface CompletionState {
|
5 | 8 | completions: Record<string, string[]>;
|
6 | 9 | loading: Record<string, boolean>;
|
7 |
| - error: Record<string, string | null>; |
| 10 | +} |
| 11 | + |
| 12 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 13 | +function debounce<T extends (...args: any[]) => PromiseLike<void>>( |
| 14 | + func: T, |
| 15 | + wait: number, |
| 16 | +): (...args: Parameters<T>) => void { |
| 17 | + let timeout: ReturnType<typeof setTimeout>; |
| 18 | + return function (...args: Parameters<T>) { |
| 19 | + clearTimeout(timeout); |
| 20 | + timeout = setTimeout(() => func(...args), wait); |
| 21 | + }; |
8 | 22 | }
|
9 | 23 |
|
10 | 24 | export function useCompletionState(
|
11 | 25 | handleCompletion: (
|
12 | 26 | ref: ResourceReference | PromptReference,
|
13 | 27 | argName: string,
|
14 | 28 | value: string,
|
| 29 | + signal?: AbortSignal, |
15 | 30 | ) => Promise<string[]>,
|
16 | 31 | completionsSupported: boolean = true,
|
| 32 | + debounceMs: number = 300, |
17 | 33 | ) {
|
18 | 34 | const [state, setState] = useState<CompletionState>({
|
19 | 35 | completions: {},
|
20 | 36 | loading: {},
|
21 |
| - error: {}, |
22 | 37 | });
|
23 | 38 |
|
| 39 | + const abortControllerRef = useRef<AbortController | null>(null); |
| 40 | + |
| 41 | + const cleanup = useCallback(() => { |
| 42 | + if (abortControllerRef.current) { |
| 43 | + abortControllerRef.current.abort(); |
| 44 | + abortControllerRef.current = null; |
| 45 | + } |
| 46 | + }, []); |
| 47 | + |
| 48 | + // Cleanup on unmount |
| 49 | + useEffect(() => { |
| 50 | + return cleanup; |
| 51 | + }, [cleanup]); |
| 52 | + |
24 | 53 | const clearCompletions = useCallback(() => {
|
| 54 | + cleanup(); |
25 | 55 | setState({
|
26 | 56 | completions: {},
|
27 | 57 | loading: {},
|
28 |
| - error: {}, |
29 | 58 | });
|
30 |
| - }, []); |
| 59 | + }, [cleanup]); |
31 | 60 |
|
32 | 61 | const requestCompletions = useCallback(
|
33 |
| - async (ref: ResourceReference | PromptReference, argName: string, value: string) => { |
34 |
| - if (!completionsSupported) { |
35 |
| - return; |
36 |
| - } |
| 62 | + debounce( |
| 63 | + async ( |
| 64 | + ref: ResourceReference | PromptReference, |
| 65 | + argName: string, |
| 66 | + value: string, |
| 67 | + ) => { |
| 68 | + if (!completionsSupported) { |
| 69 | + return; |
| 70 | + } |
37 | 71 |
|
38 |
| - setState((prev) => ({ |
39 |
| - ...prev, |
40 |
| - loading: { ...prev.loading, [argName]: true }, |
41 |
| - error: { ...prev.error, [argName]: null }, |
42 |
| - })); |
| 72 | + cleanup(); |
| 73 | + |
| 74 | + const abortController = new AbortController(); |
| 75 | + abortControllerRef.current = abortController; |
43 | 76 |
|
44 |
| - try { |
45 |
| - const values = await handleCompletion(ref, argName, value); |
46 |
| - setState((prev) => ({ |
47 |
| - ...prev, |
48 |
| - completions: { ...prev.completions, [argName]: values }, |
49 |
| - loading: { ...prev.loading, [argName]: false }, |
50 |
| - })); |
51 |
| - } catch (err) { |
52 |
| - const error = err instanceof Error ? err.message : String(err); |
53 | 77 | setState((prev) => ({
|
54 | 78 | ...prev,
|
55 |
| - loading: { ...prev.loading, [argName]: false }, |
56 |
| - error: { ...prev.error, [argName]: error }, |
| 79 | + loading: { ...prev.loading, [argName]: true }, |
57 | 80 | }));
|
58 |
| - } |
59 |
| - }, |
60 |
| - [handleCompletion, completionsSupported], |
| 81 | + |
| 82 | + try { |
| 83 | + const values = await handleCompletion( |
| 84 | + ref, |
| 85 | + argName, |
| 86 | + value, |
| 87 | + abortController.signal, |
| 88 | + ); |
| 89 | + |
| 90 | + if (!abortController.signal.aborted) { |
| 91 | + setState((prev) => ({ |
| 92 | + ...prev, |
| 93 | + completions: { ...prev.completions, [argName]: values }, |
| 94 | + loading: { ...prev.loading, [argName]: false }, |
| 95 | + })); |
| 96 | + } |
| 97 | + } catch (err) { |
| 98 | + if (!abortController.signal.aborted) { |
| 99 | + setState((prev) => ({ |
| 100 | + ...prev, |
| 101 | + loading: { ...prev.loading, [argName]: false }, |
| 102 | + })); |
| 103 | + } |
| 104 | + } finally { |
| 105 | + if (abortControllerRef.current === abortController) { |
| 106 | + abortControllerRef.current = null; |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + debounceMs, |
| 111 | + ), |
| 112 | + [handleCompletion, completionsSupported, cleanup, debounceMs], |
61 | 113 | );
|
62 | 114 |
|
63 | 115 | // Clear completions when support status changes
|
|
0 commit comments