Result

class Result<T : Any>(name: String, serializer: KSerializer<T>, stateHandle: SavedStateHandle, scope: CoroutineScope, json: Json = Json, default: T? = null, workerDispatcher: CoroutineDispatcher = Dispatchers.Default) : ViewModelState<T> (source)

Persists payloads; loading and errors are transient. Restored non-null data starts as Success.

Constructors

Link copied to clipboard
constructor(name: String, serializer: KSerializer<T>, stateHandle: SavedStateHandle, scope: CoroutineScope, json: Json = Json, default: T? = null, workerDispatcher: CoroutineDispatcher = Dispatchers.Default)

Properties

Link copied to clipboard
val data: StateFlow<T?>

Saved payload stream, independent of execution status.

Link copied to clipboard

Key owned by this state; its raw SavedStateHandle representation is a JSON string.

Link copied to clipboard
val scope: CoroutineScope

Scope controlling operations, usually viewModelScope.

Link copied to clipboard
val workerDispatcher: CoroutineDispatcher

Dispatcher for repository work, transformations and serialization.

Functions

Link copied to clipboard
fun cancel()

Stops the active operation without clearing state or completing its observers.

Link copied to clipboard
fun flow(): StateFlow<DataResult<T>>

One stable stream across all executions, with payload and status from one snapshot.

Link copied to clipboard
fun get(): T?

Reads the restored or latest saved payload.

Link copied to clipboard

Clears the payload without disconnecting observers.

Link copied to clipboard
fun load(func: suspend () -> Flow<DataResult<T>>): Job

Always starts a new request. To reuse restored data, check get() before calling.

Link copied to clipboard
fun <A> loadMapped(transform: suspend (A) -> T, func: suspend () -> Flow<DataResult<A>>): Job

Maps payloads while preserving each result's status and error.

Link copied to clipboard
fun <A> loadReducing(reduce: suspend (T?, A) -> T, func: suspend () -> Flow<DataResult<A>>): Job

Reduces each non-null payload into the current saved state.

Link copied to clipboard
fun <R> select(transform: (T?) -> R): StateFlow<R>

Synchronous projection for cheap work. Use selectAsync for expensive computations.

Link copied to clipboard
fun <R> selectAsync(initialValue: R, transform: suspend (T?) -> R): StateFlow<R>

Computes an expensive derived state asynchronously; reads reuse its last completed value.

Link copied to clipboard
open override fun set(value: T?)

Synchronous write for small values. Throws on codec failure, preserving the prior state. Prefer setAsync for expensive payloads; this method runs on the calling thread.

fun set(value: DataResult<T>)

Results without payload retain the last saved data; invalidate explicitly clears it.

open override fun set(value: T?, distinct: Boolean)

Skips equal payload encoding while still resetting transient status.

Link copied to clipboard
fun setAsync(value: T?): Job

Prepares a detached saved snapshot on workerDispatcher, then commits on the owner scope. Replaces the active operation. Prefer this to synchronous set for expensive payloads. Plain failures propagate to the scope; result holders expose an Error with the old payload.