DataCollector

A generic collector that asynchronously waits for a complete set of values, as defined by the properties of a provided data class, and assembles them into a new instance of that class.

This version of the collector uses a zip-based approach and is best suited for sequential workflows.

Recommended Usage

For the current version, the recommended use case is for a single collection cycle. This functionality is guaranteed to work reliably. After a single collection completes (or fails), the instance is cancelled and its resources are released. To collect another set of data, you must create a new instance of the DataCollector.

Lifecycle and Concurrency

This class manages its own CoroutineScope. A new Job() is added to the scope's context (dispatcher + Job() + exceptionHandler) to create a self-contained, cancellable lifecycle.

This version is "fail-fast": any error during an emit will cancel the entire collector. The user is responsible for creating a new instance to try again. Data emitted before a restart will be lost.

Parameters

T

The type of the data class to be collected and constructed. Must have a primary constructor.

classType

data class T used as a template to determine the required properties and their types via reflection.

onResult

A callback lambda that is invoked with the result. It receives a populated instance of T on success, or a Throwable on failure.

collectorCount

The number of times to collect a complete set of data. null (the default) means it will collect continuously until cancel() is called.

dispatcher

The CoroutineDispatcher on which the collection and result callback will be executed. Defaults to Dispatchers.Default.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun cancel()

Immediately cancels all ongoing collection jobs and releases all internal resources.

Link copied to clipboard
fun <P> emit(property: KProperty1<T, P>, value: P)

Emits a value for a specific property of the target data class T.