taskState()
Creates a reactive task execution tracker with automatic status, duration, and error tracking.
Signature
ts
function taskState<T = unknown>(opts?: { id?: string }): TaskState<T>Parameters
| Parameter | Type | Description |
|---|---|---|
opts | { id?: string } | Optional configuration. |
Returns
TaskState<T> — a task tracker with the following API:
| Method | Signature | Description |
|---|---|---|
run(fn) | (fn: (signal: AbortSignal) => T \ | Promise<T>) => Promise<T> |
get() | () => TaskMeta<T> | Returns the current metadata snapshot (status, result, error, duration, runCount). |
status | Store<TaskStatus> | Reactive store: 'idle' \ |
result | Store<T \ | undefined> |
error | Store<unknown \ | undefined> |
duration | Store<number \ | undefined> |
runCount | Store<number> | Reactive store of total run count. |
reset() | () => void | Reset to idle state and abort any running task. |
destroy() | () => void | Tear down all reactive stores. |
Basic Usage
ts
import { taskState } from 'callbag-recharge';
const task = taskState<string>();
await task.run((signal) => fetch('/api', { signal }).then(r => r.text()));
task.status.get(); // 'success'
task.duration.get(); // e.g. 120Options / Behavior Details
- Signal-first: The
run()callback receives anAbortSignalas its first argument for cooperative cancellation. - Companion stores: Each metadata field is an individual reactive store, independently subscribable.
- Generation tracking: Concurrent
reset()during arun()silently discards the stale result.