Fix task poller callback staleness and normalize filings query
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { getTask } from '@/lib/api';
|
||||
import type { Task } from '@/lib/types';
|
||||
|
||||
@@ -12,6 +12,11 @@ type UseTaskPollerInput = {
|
||||
|
||||
export function useTaskPoller({ taskId, intervalMs = 2200, onTerminalState }: UseTaskPollerInput) {
|
||||
const [task, setTask] = useState<Task | null>(null);
|
||||
const onTerminalStateRef = useRef(onTerminalState);
|
||||
|
||||
useEffect(() => {
|
||||
onTerminalStateRef.current = onTerminalState;
|
||||
}, [onTerminalState]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!taskId) {
|
||||
@@ -33,7 +38,7 @@ export function useTaskPoller({ taskId, intervalMs = 2200, onTerminalState }: Us
|
||||
setTask(latest);
|
||||
|
||||
if (latest.status === 'completed' || latest.status === 'failed') {
|
||||
onTerminalState?.(latest);
|
||||
onTerminalStateRef.current?.(latest);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
@@ -53,7 +58,7 @@ export function useTaskPoller({ taskId, intervalMs = 2200, onTerminalState }: Us
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
}, [taskId, intervalMs, onTerminalState]);
|
||||
}, [taskId, intervalMs]);
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user