Router
A router connects transformers: it delivers signals and effects, answers queries, and streams data and effects to observers. Create it from a coroutine scope so it lives exactly as long as the scope.
Creating a Router
class CartViewModel(cart: Transformer, promotions: Transformer) : ViewModel() {
private val router = viewModelScope.router("cart-router", cart, promotions) { global() }
val state: StateFlow<CartState> = router.state(Cart.state)
fun applyPromo(code: String) = router.process(CartSignal.ApplyPromo(code))
}
- Lifecycle:
scope.router(name, transformers…)clears the router and its transformers when the scope ends; there is nothing to clear by hand and no base class to extend. - Global routing: routers are isolated by default. Call
global()on every router that should exchange effects and queries with other routers. - Options: the trailing block takes the options below.
Sending and Observing
| You want to | Write |
|---|---|
| Send a signal or effect | router.process(message) (in caller order; buffered until the router is ready) |
| Follow a feature's state | router.state(handle): a StateFlow starting from the current value, wherever the owner is |
| Every new value of a state | router.stream(handle) |
| Data or effects by type | router.streamData<T>(), router.streamEffect<T>() |
| Query from outside a transformer | router.queryHelper.compute(handle.contract) |
Observer streams keep up to Capacity items for observers that have not subscribed yet or are behind, then drop the
oldest; transformers are never slowed down by observers. router.state(...) always reflects the latest value.
In the Shop
Each screen's ViewModel owns one scoped router. Catalog, cart and checkout call global(), so the catalog's
AddItem effect reaches the cart and checkout can query the cart; the lab router stays isolated. Transformers are
created per ViewModel in the Koin module:
val viewModelModule = module {
viewModel { CatalogViewModel(catalogTransformer(get())) }
viewModel { CartViewModel(cartTransformer(), promotionTransformer()) }
viewModel { CheckoutViewModel(checkoutTransformer(get())) }
}
Options
viewModelScope.router("catalog-router", catalogTransformer(repository)) {
global()
setCapacity(Capacity.Custom(128))
}
| Option | Effect |
|---|---|
global() |
Joins the global router: exchanges effects and queries with other routers that joined it |
setCapacity(capacity) |
Buffer size of the router's internal channels; Capacity.Default is 64, Custom(n) takes 0 to 256, Unlimited never drops |
addDispatcher(dispatcher) |
Where the router's own coroutines run; pass the test dispatcher in tests |
validateGlobalContracts() |
Fails when two global routers serve the same state, query or command, instead of the first one winning |
Several Routers
Give each screen or feature its own router, and call global() only on routers that must reach each other. The
shop's catalog, cart and checkout routers are global; the lab router is not, so effects published in it never leave
it.
- Effects: a broadcast
publish(effect)reaches matching handlers in its own router and in every other global router. A targetedpublish(effect, to = Cart)reaches only the owner ofCart, in this router or another global one. - Queries, state reads and commands: resolved in the caller's router first, then in the global routers.
- Observed state:
router.state(Cart.state)follows the owner ofCart.statein this router or another global one, and picks it up if that router is created later. Until then it holds the handle's initial value. An isolated router only sees its own transformers' state. - Duplicates: if two global routers serve the same handle, the first registered wins, unless
validateGlobalContracts()is on.
Outside a ViewModel
scope.router(...) works with any CoroutineScope that has a Job, such as an application scope or a test's
backgroundScope. The router is cleared when that scope ends. On platforms without ViewModels, create a scope you
cancel yourself:
class CartController(cart: Transformer) {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
private val router = scope.router("cart", cart)
val state = router.state(Cart.state)
fun close() = scope.cancel()
}
Classic API (Deprecated)
Creating routers with TransmissionRouter { } and clearing them by hand, and extending RouterViewModel, are
deprecated. They keep working in 2.x.
| Classic (deprecated) | Builder |
|---|---|
TransmissionRouter { addTransformerSet(...) } + router.clear() |
scope.router(name, transformers…) |
registerToGlobalRouter() |
global() |
class MyViewModel : RouterViewModel(transformers) |
class MyViewModel : ViewModel() with viewModelScope.router(...) |
streamDataAsState(contract, scope, initial) |
router.state(handle) |
streamData(contract) |
router.stream(handle) |
The old reference, including RouterViewModel, is kept in
Classic API: TransmissionRouter and RouterViewModel.