Skip to content

How To Use

This guide builds a small counter with the current API: a Contracts object, a transformer built with transformer { }, and a router tied to a coroutine scope.

1. Define transmissions

Signals come from outside the router (the UI), effects travel between transformers, and data goes to observers.

import com.yigitozgumus.transmission.Transmission

data class Add(val amount: Int) : Transmission.Signal
data object Reset : Transmission.Signal

data class Milestone(val value: Int) : Transmission.Effect

data class Count(val value: Int) : Transmission.Data

2. Declare what a feature offers

A Contracts object lists the feature's state, queries and commands. Each handle is named after the object and the property (Counter.count), so logs and errors are readable without repeating names.

import com.yigitozgumus.transmission.Contracts

object Counter : Contracts() {
    val count by state(Count(0))
    val limit by query<Int>()
    val clear by command()
}

3. Build transformers

import com.yigitozgumus.transmission.transformer

fun counterTransformer() = transformer(Counter) {
    val count = state(Counter.count)      // a StateFlow that publishes every change

    on<Add> { signal ->
        count.update { Count(it.value + signal.amount) }
        if (count.value.value % 10 == 0) publish(Milestone(count.value.value))
    }
    on<Reset> { count.value = Count(0) }

    answer(Counter.limit) { 100 }                   // other transformers can ask: Counter.limit()
    handle(Counter.clear) { count.value = Count(0) } // other transformers can trigger: Counter.clear()
}

fun loggerTransformer() = transformer("logger") {
    on<Milestone> { effect -> println("Reached ${effect.value}") }
}

Handlers of one transformer run one at a time, in arrival order, so plain vars inside the builder need no locking. on<T> accepts signals and effects alike.

4. Create a router and use it

A router created with scope.router(...) lives as long as the scope. In a ViewModel, use viewModelScope:

import com.yigitozgumus.transmission.router
import com.yigitozgumus.transmission.state

class CounterViewModel : ViewModel() {
    private val router = viewModelScope.router("counter", counterTransformer(), loggerTransformer())

    val count: StateFlow<Count> = router.state(Counter.count)

    fun add(amount: Int) = router.process(Add(amount))
    fun reset() = router.process(Reset)
}

router.state(handle) starts from the holder's current value, so nothing published before the UI subscribes is missed. To observe any data or effect by type, use router.streamData<T>() and router.streamEffect<T>().

5. Talk to other transformers

Inside a handler, call handles like suspend functions. They work across routers too, for routers that call global():

fun bonusTransformer() = transformer("bonus") {
    on<Milestone> { effect ->
        if (effect.value < Counter.limit()) Counter.clear()
        val current = Counter.count()               // read another transformer's state
        emit(Count(current.value))                  // send data to observers
    }
}
You want to Write
Send data to observers emit(data)
Broadcast an effect publish(effect)
Send an effect to one transformer publish(effect, to = Counter)
Ask another transformer Counter.limit()
Trigger an operation elsewhere Counter.clear()
Wait for the user's next step awaitSignal<NextStep>() (see Handlers)

6. Test it

@Test
fun `adding ten publishes a milestone`() = counterTransformer(dispatcher).test(dispatcher) {
    send(Add(10))
    assertEquals(Count(10), awaitState(Counter.count))
    assertEquals(Milestone(10), awaitEffect<Milestone>())
}

See Testing for fakes and the other assertions.

Classic API (Deprecated)

Transformer classes that call configure { onSignal<T> { } }, Contract.dataHolder() / Contract.computation() contracts, routers built with TransmissionRouter { }, and ViewModels that extend RouterViewModel make up the classic API, which is deprecated. It keeps working in 2.x, and the two styles mix while you migrate: every handle exposes its classic contract as handle.contract. Each reference page ends with a table mapping classic calls to the builder API; the old reference is under Classic API.