Transformer Communication
Transformers never hold references to each other. They communicate through messages, queries and commands, within a
router and across routers that called global().
Ways to Communicate
| You want to | Write | Runs |
|---|---|---|
| Send data to observers | emit(data) |
Immediately |
| Broadcast an effect | publish(effect) |
In each handling transformer's mailbox |
| Send an effect to one transformer | publish(effect, to = Cart) |
In that transformer's mailbox |
| Read another transformer's state | Cart.state() |
Immediately, from the owner's current value |
| Ask for a computed value | Cart.summary(), Catalog.productById(id) |
In the caller's coroutine |
| Trigger an operation elsewhere | Cart.clear() |
In the owner's mailbox, in call order |
| Wait for the next message | awaitSignal<T>() |
Gives up the turn until a T arrives |
How the Shop Uses Them
The shop's catalog, cart and checkout live in three routers that called global():
| From | To | How | Why |
|---|---|---|---|
| Catalog | Cart | publish(CartEffect.AddItem(id)), a broadcast effect crossing routers |
Add a product to the cart |
| Cart | Catalog | Catalog.productById(id), a cached query in another router |
Price the product |
| Promotions | Cart | publish(PromoEffect.Applied(promo), to = Cart), a targeted effect |
Only the cart needs the outcome |
| Checkout | Cart | Cart.summary() |
Read the cart before checkout |
| Checkout | Cart | Cart.clear() |
Empty the cart after the order |
| Checkout | Checkout | awaitSignal<SubmitAddress>() |
Wait for the user's next step |
fun promotionTransformer() = transformer("promotions") {
on<CartSignal.ApplyPromo> { signal ->
val code = signal.code.trim().uppercase()
val percentOff = PROMO_CODES[code]
val outcome = if (percentOff == null) PromoEffect.Rejected(code, "unknown code")
else PromoEffect.Applied(Promo(code, percentOff))
publish(outcome, to = Cart)
}
}
The lab router does not call global(): publishing the same CartEffect.AddItem from it leaves the cart untouched,
which the sample's Lab screen shows.
Choosing a Way
- Something happened, and others may care: broadcast an effect with
publish(effect). The sender doesn't know who listens, so features stay decoupled. The catalog'sAddItemis an example. - Only one feature needs to know: send a targeted effect with
publish(effect, to = Cart), as promotions do. - You need a value now: call a query, such as
Cart.summary(). Itsanswerblock runs in your coroutine, so keep it to reading state. - You need another feature to change its state: call a command, such as
Cart.clear(). It runs in the owner's mailbox, in order with its own handlers, so the owner's state stays consistent. - You are waiting for the user:
awaitSignal<T>()in a handler, as the checkout does.
Failures
- A query nobody serves throws
ContractNotFoundExceptionnaming the handle. Catch it in the handler if the feature is optional; otherwise it reaches the caller'sonError. - An exception in an
answerblock is thrown to the caller, since the block runs in the caller's coroutine. - An exception in a command or in an effect handler is reported to the owner's
onError. The sender is not told; reply with an effect if the sender needs the outcome, as promotions reply withPromoEffect.Rejected.
Classic API (Deprecated)
The classic communication calls are deprecated. The builder's verbs and handles are built on them, so behaviour doesn't change when you migrate.
| Classic (deprecated) | Builder |
|---|---|
send(data) |
emit(data) |
publish(effect) |
publish(effect) |
send(effect, identity) |
publish(effect, to = Cart) |
getData(contract) |
Cart.state() |
compute(contract), compute(contract, args) |
Cart.summary(), Catalog.productById(id) |
execute(contract), execute(contract, args) |
Cart.clear(), Orders.cancel(id) |
pauseOn(checkpoint) / validate(checkpoint, value) |
awaitSignal<T>() |
The old reference is kept in Classic API: Transformer Communication.