Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 .changeset/calm-hooks-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/react-hooks": patch
---

Fix `onComplete` callback firing prematurely when the realtime stream disconnects before the run finishes.
8 changes: 6 additions & 2 deletions 8 packages/react-hooks/src/hooks/useRealtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ export function useRealtimeRun<TTask extends AnyTask>(
const hasCalledOnCompleteRef = useRef(false);

// Effect to handle onComplete callback
// Only call onComplete when the run has actually finished (has finishedAt),
// not just when the subscription stream ends (which can happen due to network issues)
useEffect(() => {
if (isComplete && run && options?.onComplete && !hasCalledOnCompleteRef.current) {
if (isComplete && run?.finishedAt && options?.onComplete && !hasCalledOnCompleteRef.current) {
options.onComplete(run, error);
hasCalledOnCompleteRef.current = true;
}
Expand Down Expand Up @@ -313,8 +315,10 @@ export function useRealtimeRunWithStreams<
const hasCalledOnCompleteRef = useRef(false);

// Effect to handle onComplete callback
// Only call onComplete when the run has actually finished (has finishedAt),
// not just when the subscription stream ends (which can happen due to network issues)
useEffect(() => {
if (isComplete && run && options?.onComplete && !hasCalledOnCompleteRef.current) {
if (isComplete && run?.finishedAt && options?.onComplete && !hasCalledOnCompleteRef.current) {
options.onComplete(run, error);
hasCalledOnCompleteRef.current = true;
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.