Skip to content

//reactivestate-core/com.ensody.reactivestate/ReactiveViewModel

ReactiveViewModel

[common]\ @ExperimentalReactiveStateApi

abstract class ReactiveViewModel(val context: ReactiveViewModelContext) : BaseReactiveState<ErrorEvents>

Base class for ViewModels which provide a context for common APIs across ViewModels.

In particular, the ReactiveViewModelContext.preInit hook can be used to launch coroutines via a CoroutineLauncher directly from the ViewModel constructor, so you can have nicer error handling and loading indicators.

Note that context is open so you can define your own subclass with a customized context class, providing additional useful attributes specific to your app.

Constructors

ReactiveViewModel [common]
constructor(context: ReactiveViewModelContext)

Properties

Name Summary
context [common]
open val context: ReactiveViewModelContext
This is open to allow overriding with a more specific type.
eventNotifier [common]
open override val eventNotifier: EventNotifier<ErrorEvents>
launcherScope [common]
abstract val launcherScope: CoroutineScope
The underlying CoroutineScope of this launcher.
loading [common]
abstract val loading: MutableValueFlow<Int>
The default loading tracker.

Functions

Name Summary
attachTo [common]
fun ReactiveState<out ErrorEvents>.attachTo(owner: Any)
autoRun [common]
fun CoroutineLauncher.autoRun(onChange: AutoRunOnChangeCallback<Unit>? = null, observer: AutoRunCallback<Unit>): AutoRunner<Unit>
Watches observables for changes. Often useful to keep things in sync (e.g. CoroutineLauncher -> UI).
coAutoRun [common]
fun CoroutineLauncher.coAutoRun(onChange: CoAutoRunOnChangeCallback<Unit>? = null, flowTransformer: AutoRunFlowTransformer = { conflatedWorker(transform = it) }, dispatcher: CoroutineDispatcher = dispatchers.main, withLoading: MutableValueFlow<Int>? = loading, observer: CoAutoRunCallback<Unit>): CoAutoRunner<Unit>
Watches observables for changes. Often useful to keep things in sync (e.g. CoroutineLauncher -> UI).
derived [common]
fun <T> CoroutineLauncher.derived(synchronous: Boolean = true, cache: Boolean = synchronous, observer: AutoRunCallback<T>): StateFlow<T>
Creates a StateFlow that computes its value based on other StateFlows via an autoRun block.
[common]
fun <T> CoroutineLauncher.derived(initial: T, started: SharingStarted = SharingStarted.Eagerly, flowTransformer: AutoRunFlowTransformer = { conflatedWorker(transform = it) }, dispatcher: CoroutineDispatcher = dispatchers.main, withLoading: MutableValueFlow<Int>? = loading, observer: CoAutoRunCallback<T>): StateFlow<T>
Creates a StateFlow that computes its value based on other StateFlows via a suspendable coAutoRun block.
invokeOnCompletion [common]
fun CoroutineLauncher.invokeOnCompletion(handler: CompletionHandler): Disposable
Helper for adding a completion handler to a CoroutineLauncher.
launch [common]
open fun launch(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, withLoading: MutableValueFlow<Int>? = loading, onError: suspend (Throwable) -> Unit? = null, block: suspend CoroutineScope.() -> Unit): Job
Launches a coroutine. Mark long-running coroutines by setting withLoading to loading state.
onError [common]
open override fun onError(error: Throwable)
rawLaunch [common]
open fun rawLaunch(context: CoroutineContext, start: CoroutineStart, block: suspend CoroutineScope.() -> Unit): Job
Launches a coroutine without any error handling or loading state tracking.
track [common]
open suspend fun track(withLoading: MutableValueFlow<Int>? = loading, onError: suspend (Throwable) -> Unit? = null, block: suspend () -> Unit)
Tracks a suspension block’s loading state and errors.