Transmission is a Kotlin Multiplatform library for asynchronous communication between business-logic components. Signals come in, transformers handle them, and data comes out. I have been working on it since early 2024.1
2.0 lives in my personal fork of the original library I built at Trendyol. I want to try different things here, at a faster pace.
Version 2.0 is now in beta. 2.0.0-beta02 is on Maven Central.
This is the biggest release since the library started. The runtime is rewritten, the public API has a new primary form, and the library moved to a new namespace. In this post I want to explain what changed and, more importantly, why.
Get your hot beverage of choice 👇🏻
Why a 2.0?
The 1.x API worked. Teams used it in production. But every time I wrote a new feature with it, I felt the same friction.
For a simple counter, I needed a Transformer subclass, an init block, a configure { } call, a data holder, a Contract in a companion object, and a RouterViewModel subclass to wire it together.
class CounterTransformer : Transformer() { private val count = dataHolder(Count(0), countContract)
init { configure { onSignal<Increment> { count.update { Count(it.value + 1) } } } }
companion object { val countContract = Contract.dataHolder<Count>() }}
class CounterViewModel : RouterViewModel(setOf(CounterTransformer())) { val count = streamDataAsState(CounterTransformer.countContract, Count(0))}There is one line of actual logic in this code. Everything else is ceremony.
If the interesting part is one line, why does the reader have to scan twenty?
The second source of friction was the runtime. In Deterministic Concurrency in Transmission I wrote about the tension between performance and predictability. Back then that was an experiment on my own fork. In 2.0 those experiments became the actual implementation.
The same counter in 2.0
data object Increment : Transmission.Signaldata class Count(val value: Int) : Transmission.Data
object Counter : Contracts() { val count by state(Count(0))}
fun counterTransformer() = transformer(Counter) { val count = state(Counter.count) on<Increment> { count.update { Count(it.value + 1) } }}
class CounterViewModel : ViewModel() { private val router = viewModelScope.router("counter", counterTransformer()) val count: StateFlow<Count> = router.state(Counter.count) fun increment() = router.process(Increment)}There are three ideas here. Let’s go through them one by one.
Contracts are objects now
In 1.x, a contract was a value you created with a factory and stored somewhere, usually in a companion object. The contract and the feature it belonged to were only connected by convention.
In 2.0 a feature declares what it offers in a Contracts object:
object Cart : Contracts() { val state by state(CartState()) val summary by query<CartState>() val clear by command()}Every handle is a delegated property, so it gets its name for free. Errors and logs read Cart.summary, not an anonymous key. This sounds small, but it changes debugging a lot. A ContractNotFoundException that names Catalog.productById tells you exactly what is missing.
And handles are called directly:
on<CartEffect.AddItem> { effect -> val product = Catalog.productById(effect.productId) ?: return@on cart.update { it.add(product) }}Compare this with compute(CatalogTransformer.productByIdContract, effect.productId). The new version reads like a normal function call, because from the handler’s perspective it is one.
Transformers are builders
A transformer is no longer a class you extend. It is a function that returns one:
fun cartTransformer() = transformer(Cart) { val cart = state(Cart.state)
on<CartEffect.AddItem> { effect -> /* ... */ }
answer(Cart.summary) { cart.value } handle(Cart.clear) { cart.value = CartState() }}onSignal and onEffect collapsed into a single on<T>. send became emit. Serving a query is answer, serving a command is handle.
The builder still produces a regular Transformer. This was important for me. Builder and classic transformers live in the same router, so you can migrate one feature at a time instead of rewriting everything in one go.
Routers follow scopes
RouterViewModel existed because a router needs a lifecycle. In 2.0 the router simply takes a CoroutineScope:
private val router = viewModelScope.router("counter", counterTransformer())val count = router.state(Counter.count)When the scope ends, the router is cleared. There is no base class to extend, so your ViewModel can extend whatever it needs to.
Goodbye checkpoints, hello awaitSignal
In Checkpoints I explained how we handled flows that need to wait for a condition we can’t compute ourselves, like user input. The solution worked, but it needed a checkpoint contract, a pauseOn call on one side and a validate call on the other.
2.0 replaces all of that with one function:
accepts<SubmitAddress>()accepts<SubmitPayment>()accepts<Cancel>()
on<Checkout.Start> { val address = awaitSignal<SubmitAddress>(cancelOn = Cancel::class) ?.address ?: return@on cancelled() state.update { it.copy(step = CheckoutStep.Payment, address = address) }
val payment = awaitSignal<SubmitPayment>(cancelOn = Cancel::class) ?.payment ?: return@on cancelled() // charge, clear the cart, publish OrderPlaced...}The whole checkout reads top to bottom, in one handler. No state machine spread over five handlers, no contracts just for synchronization.
While the handler waits, it gives up its turn, so the transformer keeps processing other messages. When the awaited signal arrives, the handler continues right after it. There are also timeout variants that return null when time runs out.
Checkpoints still work in 2.x, but they are deprecated and will be removed in 3.0. I am a bit sad about this one, since I wrote a whole post about them. But the new version is just better.
The runtime is boring now
This is the part most users will never see, and that is the point.
Every transformer now has a single mailbox. Messages to one transformer are handled one at a time, in arrival order. process, send and tryProcess share one ordered inbox. emit, publish and commands never suspend the handler.
What this gives you in practice:
- Plain
varstate inside a transformer is safe. Only one message runs at a time.2 - A failing handler doesn’t kill the transformer. It is reported to
onError, and the next message is handled as usual. - Signals sent before the router initializes are delivered. No more lost first events.
- Queries are fast. A
computeround trip went from tens of microseconds to under one.3
In the deterministic concurrency post I said I wanted the internals to be more concurrent while the public behavior became more boring. That is what happened. Same inputs, same outputs, same order.
Testing got its own DSL
The old TransmissionTest harness worked, but tests had a lot of setup. 2.0 comes with a DSL in transmission-test:
@Testfun `increment bumps the count`() = counterTransformer().test { send(Increment) assertEquals(Count(1), awaitState(Counter.count))}When a transformer depends on other features, you fake their handles:
@Testfun `address then payment places the order`() = checkoutTransformer(gateway, dispatcher).test(dispatcher) { fake(Cart.summary) { cart } val clearCart = fake(Cart.clear)
send(CheckoutSignal.Start) send(CheckoutSignal.SubmitAddress(address)) send(CheckoutSignal.SubmitPayment(goodCard))
assertEquals(CheckoutStep.Confirmed("TS-1001"), awaitState(Checkout.state).step) assertEquals(1, clearCart.calls)}Because the mailbox is FIFO, the three send calls back to back always work. The waiting handler registers before the next message is looked at. This is exactly the kind of guarantee that the runtime rewrite makes possible. When a test fails, the message lists everything the transformer saw, which saves a lot of guessing.
The breaking parts
I tried to keep the migration small, but some things had to break:
- New namespace. The library is now published as
com.yigitozgumus:transmission, and packages moved fromcom.trendyol.transmissiontocom.yigitozgumus.transmission. A find-and-replace on imports is enough. - Global routing is opt-in. Routers no longer join the global router by default. If your routers talk to each other, they need to say so.4
- Failures propagate. A throwing computation now fails the caller with the same exception instead of returning
null. - Platforms.
iosX64is gone, because the JetBrains libraries underneath dropped it. Android minSdk is now 23.5
The classic API keeps working in 2.x with deprecation warnings. The migration guide maps every classic call to its builder version.
The sample
The old counter and component samples are replaced with a small shop app for Android, desktop and iOS. It uses everything: multiple routers that opt in to talk to each other, cross-router queries, targeted effects, the awaitSignal checkout, error isolation and a live inspector.
./gradlew :samples:shop:desktopApp:runIf you want to see how the pieces fit together, this is the best place to start.
Closing
1.x proved that the model works: a router, transformers and contracts between them. 2.0 is about making that model cheaper to write, easier to read and safer to run.
There is still work before a stable 2.0.0. I want more feedback on the builder API before locking it in. If you try the beta, I would love to hear what feels wrong.
Less ceremony.
Same guarantees.
Cover photo by Chintan Vadgama on Unsplash.
Footnotes
-
The original library is published as
com.trendyol:transmission. The first post in this series, Transmission Library Fundamentals and Testing, covers the original API. ↩ -
This holds inside a single transformer. A query’s
answerblock is the exception: it runs in the caller’s coroutine, outside the owner’s mailbox, so it should only read state. ↩ -
Measured with the opt-in JVM benchmarks (
./gradlew :transmission:jvmTest -Pbenchmark). Before the direct query engine, a round trip took 22–54 µs. On my machine it now takes about 0.2 µs with 2 transformers and 0.1 µs with 102. ↩ -
A scope-bound router opts in with
global()inside its builder block. Classic routers callregisterToGlobalRouter(). ↩ -
2.0 is built with Kotlin 2.4.21, Android Gradle Plugin 9.5.3, Gradle 9.9.3, Compose Multiplatform 1.12.2 and kotlinx.coroutines 1.11.0. ↩
Tags: android , library , kotlin , kmp
Share