Skip to content

State

State is the data a transformer owns and publishes. With transformer { }, declare it with state(...): the result is a StateFlow whose every change is published to observers and readable by other transformers.

State in the Builder

object Cart : Contracts() {
    val state by state(CartState())
}

fun cartTransformer() = transformer(Cart) {
    val cart = state(Cart.state)

    on<CartSignal.RemovePromo> { cart.update { it.copy(promo = null) } }
    handle(Cart.clear) { cart.value = CartState() }
}
  • Assign or update: cart.value = … or cart.update { … }. Each change is published; assigning an equal value publishes nothing.
  • Initial value: declared once, on the handle: val state by state(CartState()). The owner starts from it (state(Cart.state, other) overrides it), and observers hold it until the owner has published. It is published when the transformer joins a router, before any message is handled.
  • Readers: other transformers call Cart.state(); UI code uses router.state(Cart.state), which starts from the current value.
  • Without a handle: state(initial) publishes by type only, for state nobody reads by name.
  • Concurrency: change state only from this transformer's handlers and commands, which never run concurrently.

Reading State

From Write Gets
The owning transformer cart.value The current value
Another transformer's handler Cart.state() The owner's current value, from this router or a global one
UI code router.state(Cart.state) A StateFlow that starts from the current value and follows every change
UI code, changes only router.stream(Cart.state) Every value published from now on
A test awaitState(Cart.state) The value once everything sent so far has been handled

Choosing a State Shape

  • Immutable data classes: make state a data class that implements Transmission.Data, and change it with copy. Equality then decides whether a change is published.
  • Defaults: give every property a default, as CartState() does, so the initial value is just the constructor.
  • Derived values: compute them from stored ones (CartState.totalCents from its lines and promo) rather than storing them, so they can't disagree.
  • One state per feature: a single state handle per Contracts object is usually enough. Split it only when parts change at very different rates.
  • Private working values: a plain var inside the builder is safe for values nobody else reads, such as the catalog's unfiltered product list, because handlers never run concurrently.
fun catalogTransformer(repository: ProductRepository) = transformer(Catalog) {
    var allProducts: List<Product> = emptyList()           // private, never published
    val state = state(Catalog.state)        // published

    on<CatalogSignal.Load> {
        state.update { it.copy(loading = true) }
        allProducts = repository.products()
        state.update { it.copy(loading = false, products = allProducts) }
    }
}

Nullable State

state<T?>() with a null initial value publishes nothing until the first non-null value; readers see null until then. Setting it back to null is not published.

Classic API (Deprecated)

Classic transformers create state with dataHolder(initialValue, contract). That API is deprecated. The builder's state(...) is built on the same mechanism, so moving to it changes no behaviour.

Classic (deprecated) Builder
Contract.dataHolder<T>() val state by state(initial) in a Contracts object
dataHolder(initial, contract) state(handle), with the initial value on the handle
holder.update { }, holder.getValue() state.update { }, state.value
getData(contract) in another transformer Cart.state()
streamDataAsState(contract, ...) router.state(handle)

The old reference is kept in Classic API: DataHolder.