import type { WorkspaceSearchResponse } from "./api"; // Cleanup cancels both the debounce and fetch, and also rejects late responses // from transports that finish after cancellation. export function scheduleWorkspaceSearch( query: string, request: (query: string, signal: AbortSignal) => Promise, onResult: (result: WorkspaceSearchResponse) => void, onError: (error: unknown) => void ) { const controller = new AbortController(); const timer = setTimeout(async () => { try { const result = await request(query, controller.signal); if (!controller.signal.aborted) onResult(result); } catch (error) { if (!controller.signal.aborted) onError(error); } }, 300); return () => { clearTimeout(timer); controller.abort(); }; }