coAutoRun

fun ViewModel.coAutoRun(launcher: CoroutineLauncher = if (this is CoroutineLauncher) this else SimpleCoroutineLauncher(viewModelScope), onChange: CoAutoRunOnChangeCallback<Unit>? = null, observer: CoAutoRunCallback<Unit>): CoAutoRunner<Unit>

Watches observables for changes. Often useful to keep things in sync.

This is a convenience function that immediately starts the AutoRunner.run cycle for you.

Returns the underlying AutoRunner. To stop watching, you should call AutoRunner.dispose. The AutoRunner is automatically disposed when the viewModelScope completes.

See AutoRunner for more details.

Parameters

onChange

Gets called when the observables change. If you provide a handler you have to manually call run.

observer

The callback which is used to track the observables.


fun LifecycleOwner.coAutoRun(launcher: CoroutineLauncher = if (this is CoroutineLauncher) this else LifecycleCoroutineLauncher(this), onChange: CoAutoRunOnChangeCallback<Unit>? = null, flowTransformer: AutoRunFlowTransformer = { conflatedWorker(transform = it) }, dispatcher: CoroutineDispatcher = dispatchers.main, withLoading: MutableStateFlow<Int>? = if (this is CoroutineLauncher) launcher.loading else null, observer: AutoRunCallback<Unit>): CoAutoRunner<Unit>

Watches observables for changes. Often useful to keep things in sync (e.g. ViewModel -> UI).

This only executes the observer between onStart/onStop.

Returns the underlying AutoRunner. To stop watching, you should call AutoRunner.dispose. The AutoRunner is automatically disposed on Activity.onDestroy/Fragment.onDestroyView.

See AutoRunner for more details.

Parameters

onChange

Gets called when the observables change. If you provide a handler you have to manually call run.

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.