derived

fun <T> ViewModel.derived(launcher: CoroutineLauncher = if (this is CoroutineLauncher) this else SimpleCoroutineLauncher(viewModelScope), synchronous: Boolean = true, observer: AutoRunCallback<T>): StateFlow<T>
fun <T> LifecycleOwner.derived(launcher: CoroutineLauncher = if (this is CoroutineLauncher) this else LifecycleCoroutineLauncher(this), synchronous: Boolean = true, observer: AutoRunCallback<T>): StateFlow<T>

Creates a StateFlow that computes its value based on other StateFlows via an autoRun block.

This behaves like SharingStarted.Eagerly and computes the initial value by executing the observer function immediately.


fun <T> ViewModel.derived(initial: T, started: SharingStarted = SharingStarted.Eagerly, launcher: CoroutineLauncher = if (this is CoroutineLauncher) this else SimpleCoroutineLauncher(viewModelScope), flowTransformer: AutoRunFlowTransformer = { conflatedWorker(transform = it) }, dispatcher: CoroutineDispatcher = dispatchers.main, withLoading: MutableStateFlow<Int>? = if (this is CoroutineLauncher) launcher.loading else null, observer: CoAutoRunCallback<T>): StateFlow<T>
fun <T> LifecycleOwner.derived(initial: T, started: SharingStarted = SharingStarted.Eagerly, launcher: CoroutineLauncher = if (this is CoroutineLauncher) this else LifecycleCoroutineLauncher(this), flowTransformer: AutoRunFlowTransformer = { conflatedWorker(transform = it) }, dispatcher: CoroutineDispatcher = dispatchers.main, withLoading: MutableStateFlow<Int>? = if (this is CoroutineLauncher) launcher.loading else null, observer: CoAutoRunCallback<T>): StateFlow<T>

Creates a StateFlow that computes its value based on other StateFlows via a suspendable coAutoRun block.

You can use this to compute values on-demand only via SharingStarted.WhileSubscribed.

Parameters

initial

The initial value (until the first computation finishes).

started

When the value should be updated. Pass SharingStarted.WhileSubscribed to compute only on demand. Defaults to SharingStarted.Eagerly.

launcher

The CoroutineLauncher to use.

flowTransformer

How changes should be executed/collected. Defaults to conflatedWorker.

dispatcher

The CoroutineDispatcher to use. Defaults to dispatchers.main.

withLoading

Tracks loading state for the (re-)computation. Defaults to CoroutineLauncher.loading if this is a CoroutineLauncher or null otherwise.

observer

The callback which is used to track the observables.