KmpWorker
Main entry point for KMPWorker.
Platform-specific implementations:
Android: io.neuralheads.kmpworker.android.AndroidKmpWorker
iOS: io.neuralheads.kmpworker.ios.IOSKmpWorker
Quick start:
// Register handler
kmpWorker.register("sync-users") { repository.syncUsers() }
// Or register with execution context (taskId, retryCount, payload)
kmpWorker.register("upload") { ctx ->
if (ctx.isRetry) logger.warn("Retrying upload, attempt ${ctx.retryCount}")
uploader.upload(ctx.payload)
}
// Enqueue
kmpWorker.enqueue(TaskRequest(id = "sync-users", type = TaskType.OneTime))
// Observe
kmpWorker.observe("sync-users").collect { state -> println(state) }Functions
Cancels a previously scheduled task by its ID. Emits TaskState.Cancelled to any active observers.
Cancels multiple tasks by their IDs.
Cancels all tasks matching the given tag. Useful for cancelling groups of related tasks at once.
Builds and enqueues a TaskChain using a fluent DSL.
Clears all execution history records.
Schedules a background task. The task handler must be registered via register before calling enqueue.
Enqueues multiple tasks atomically. On Android, maps to WorkManager.enqueue(List<WorkRequest>).
Enqueues a TaskChain for sequential execution.
Enqueues a TaskChain with a ChainPolicy to handle duplicate chain IDs.
Returns execution history records for telemetry and analytics. Requires a TelemetryCollector to be installed.
Returns a Flow of TaskState updates for the chain as a whole. Emits TaskState.Success when ALL steps complete, or TaskState.Failed if any step fails.
Registers and enqueues a one-time task in a single call.
Registers and enqueues a periodic task using a Duration interval.
Registers a handler that receives a TaskExecutionContext with the task ID, retry count, payload, and tags at execution time.