TaskMonitor
Broadcasts task state changes across the application.
Features
replay=1: New collectors immediately receive the last known state for a task.extraBufferCapacity=64: Emitters never block due to slow collectors.Persistent EventStore: Install an EventStore via install to persist terminal states to disk. Call replayPendingEvents at app startup to recover missed completions.
Usage
// App startup:
TaskMonitor.install(SqlDelightEventStore(database))
TaskMonitor.replayPendingEvents() // replay missed completions from last session
// Observe:
TaskMonitor.observe("sync-users").collect { state -> ... }
// Emit:
TaskMonitor.emit("sync-users", TaskState.Running())Functions
Emits TaskState.Cancelled for the given task ID and removes any stored events.
Emits a new state for the given task ID. Terminal states are automatically persisted to the EventStore if installed. Safe to call from any coroutine context.
Installs an EventStore that will receive all terminal state events. Terminal states are: TaskState.Success, TaskState.Cancelled, and TaskState.Failed with willRetry = false.
Returns a Flow of ALL task state updates as (taskId, TaskState) pairs. Useful for global dashboards, monitoring, and TaskChainExecutor.
Prunes persisted events older than olderThanMillis ms that have already been replayed. Call once per session to keep storage bounded (e.g., at app startup after replay). No-op if no EventStore is installed.
Replays all persisted terminal events that have not yet been delivered. Call at app startup (e.g., in Application.onCreate or AppDelegate) to ensure the UI reflects task completions that happened while the app was terminated.