Skip to content

Watching Tasks

While Streaming Updates monitors a single task, watchTasks / TaskWatcher lets you subscribe to updates across all tasks — useful for building dashboards, monitoring systems, or audit logs.

import { watchTasks } from '@requence/task'
const stop = watchTasks({
since: new Date(),
onUpdate(update) {
console.log(`[${update.type}] Task ${update.context.taskId}`)
},
})
// Later — stop watching
stop()

watchTasks returns a function that stops the watcher when called. It is also an AsyncIterable:

const watcher = watchTasks({ since: new Date() })
for await (const update of watcher) {
console.log(update.type, update.context.taskId)
if (shouldStop) {
watcher() // stop watching
break
}
}
OptionTypeDescription
sinceDateRequired. Only receive updates after this timestamp.
accessTokenstringAccess token (falls back to env / config file)
filter.onlystring[]Only receive specific update types (e.g. ['taskEnd', 'taskError'])
onUpdatefunctionCallback for each update
onConnectfunctionCalled when the connection is established
onReconnectingfunctionCalled when the connection is being re-established
onErrorfunctionCalled on connection errors

Each update includes an incomplete flag. When True, it means the watcher connected after the task had already started — the taskStart event was missed. Use this to decide whether to ignore or partially process the update.