RunnableTransformScope


A TransformScope that manages a shared headless Composition and requires calling run to execute state transformations created via launchTransform.

Create a RunnableTransformScope, launch run in a coroutine, and pass it down as a TransformScope to components that only need to call launchTransform:

val transformScope = RunnableTransformScope()
scope.launch { transformScope.run() }
val viewModel = MyViewModel(transformScope = transformScope)

Summary

Public constructors

Cmn

Public functions

open DisposableState<T>
<T : Any?> launchTransform(
    initialState: T,
    container: SavedStateContainer,
    onUpdate: @Composable (T) -> T
)

Launches a Composable onUpdate transformation in this TransformScope and returns its result as a DisposableState.

Cmn
suspend Unit
run()

Starts the TransformScope and begins the process of updating the present state transformations.

Cmn

Public constructors

RunnableTransformScope

RunnableTransformScope()

Public functions

launchTransform

open fun <T : Any?> launchTransform(
    initialState: T,
    container: SavedStateContainer = SavedStateContainer(),
    onUpdate: @Composable (T) -> T
): DisposableState<T>

Launches a Composable onUpdate transformation in this TransformScope and returns its result as a DisposableState.

This function bridges reactive Compose state (or Flows collected as state) into a continuously updated State object outside a traditional Compose UI hierarchy. The transform block is automatically recomposed whenever any Compose state it reads is updated, and the newly returned value is pushed into the resulting State.

By default, this function uses an unattached SavedStateContainer. In order for androidx.compose.runtime.saveable.rememberSaveable inside onUpdate to work properly (saving and restoring state across configuration changes or process death), developers must provide their own SavedStateContainer, such as one obtained from SavedStateHandle.getOrCreateContainer:

class MyViewModel(
transformScope: TransformScope,
savedStateHandle: SavedStateHandle,
)
: ViewModel() {
val uiState: DisposableState<Int> = transformScope.launchTransform(
initialState = 0,
container = savedStateHandle.getOrCreateContainer("myTransform"),
) {
var count by rememberSaveable { mutableStateOf(0) }
count
}
}

The same is also possible with a androidx.savedstate.SavedStateRegistry via androidx.savedstate.SavedStateRegistry.getOrCreateContainer.

Calling DisposableState.dispose removes this transformation from the TransformScope so that it is no longer recomposed in the shared Composition.

Parameters
initialState: T

The initial value to populate the returned DisposableState before the first composition completes.

container: SavedStateContainer = SavedStateContainer()

The SavedStateContainer used to save and restore androidx.compose.runtime.saveable.rememberSaveable state. Developers must provide their own SavedStateContainer for rememberSaveable to persist and restore state properly.

onUpdate: @Composable (T) -> T

The Composable block that computes the updated value of type T.

Returns
DisposableState<T>

A DisposableState containing the latest result of the onUpdate block.

run

suspend fun run(): Unit

Starts the TransformScope and begins the process of updating the present state transformations.

Throws
IllegalStateException

if run is already active on this RunnableTransformScope.