Skip to content

feat: pass onComplete/onFail callbacks through createJobHooks useRun #139

@coji

Description

@coji

Context

useJobRun supports onStart, onComplete, onFail callbacks, but createJobHooks wraps it as:

useRun: (runId: string | null) => {
  return useJobRun<InferOutput<TJob>>({ api, runId })
}

The callbacks are not exposed through the generated useRun hook.

Problem

When persisting runId across page reloads (e.g. via URL search params), consumers need to clear the persisted ID when the run reaches a terminal state. Without onComplete/onFail, they must use a useEffect watching isCompleted/isFailed, which is less clean:

const { runId, clearRunId } = usePersistedRunId('refreshRunId', fetcherRunId)
const { isCompleted, isFailed, ...rest } = durably.crawl.useRun(runId)

// Would be cleaner as: durably.crawl.useRun(runId, { onComplete: clearRunId })
useEffect(() => {
  if (isCompleted || isFailed) clearRunId()
}, [isCompleted, isFailed, clearRunId])

Proposed Change

Add optional second argument to the generated useRun:

useRun: (runId: string | null, options?: { onStart?: () => void; onComplete?: () => void; onFail?: () => void }) => {
  return useJobRun<InferOutput<TJob>>({ api, runId, ...options })
}

This is backward-compatible — existing callers without options continue to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions