Transmissions
A Transmission is the fundamental unit of information in the Transmission library. All communication between components flows through these transmission types.
The Three Types
sealed interface Transmission {
sealed interface Message : Transmission // anything a transformer handles
interface Signal : Message // from outside the router, such as the UI
interface Effect : Message // between transformers
interface Data : Transmission // to observers
}
Message is the common parent of signals and effects: on<T> in transformer { } accepts either kind, and
on<T> rejects Data at compile time. Because Message is sealed, an exhaustive when over Transmission still
needs only Signal, Effect and Data branches.
Signal
Signals represent input from UI or external events that should trigger business logic processing.
Characteristics
- Entry point for user interactions
- Sent to a router with
router.process(signal) - Distributed to relevant Transformers
- Should be immutable data classes or objects
Examples
// Simple signal
data object RefreshSignal : Transmission.Signal
// Signal with data
data class SearchSignal(val query: String) : Transmission.Signal
// Sealed interface for grouped signals
sealed interface CounterSignal : Transmission.Signal {
data object Increment : CounterSignal
data object Decrement : CounterSignal
data class SetValue(val value: Int) : CounterSignal
}
From the Shop Sample
sealed interface CatalogSignal : Transmission.Signal {
data object Load : CatalogSignal
data class Search(val query: String) : CatalogSignal
data class SelectCategory(val category: Category?) : CatalogSignal
data class AddToCart(val productId: String) : CatalogSignal
}
sealed interface CheckoutSignal : Transmission.Signal {
data object Start : CheckoutSignal
data class SubmitAddress(val address: Address) : CheckoutSignal
data class SubmitPayment(val payment: PaymentDetails) : CheckoutSignal
data object Cancel : CheckoutSignal
}
Effect
Effects represent side effects or intermediate processing steps that can trigger additional business logic.
Characteristics
- Created from Signals or other Effects
- Can trigger further processing
- Represent intermediate states in business logic
- Can be sent between Transformers
Examples
// Logging effect
data class LogEffect(val message: String, val level: LogLevel) : Transmission.Effect
// Navigation effect
data class NavigateEffect(val destination: String) : Transmission.Effect
// Network effect
sealed interface NetworkEffect : Transmission.Effect {
data class LoadData(val url: String) : NetworkEffect
data class SaveData(val data: Any) : NetworkEffect
}
From the Shop Sample
Effects carry events between transformers, including ones in other routers:
/** Published by the catalog router; the cart router receives it through the global router. */
sealed interface CartEffect : Transmission.Effect {
data class AddItem(val productId: String) : CartEffect
}
/** Sent by the promotions transformer to the cart only, as targeted effects. */
sealed interface PromoEffect : Transmission.Effect {
data class Applied(val promo: Promo) : PromoEffect
data class Rejected(val code: String, val reason: String) : PromoEffect
}
Data
Data represents the final result of business logic processing, ready for consumption by UI or other components.
Characteristics
- Final output of Transformer processing
- Consumed by UI components
- Should represent complete, ready-to-use information
- Flows through the router's data stream
Examples
// Simple data
data class UserData(val id: String, val name: String) : Transmission.Data
// UI state data
data class CounterData(val count: Int, val isLoading: Boolean) : Transmission.Data
// List data
data class ItemListData(val items: List<Item>) : Transmission.Data
From the Shop Sample
Data is usually one immutable state per screen, with derived values as properties:
data class CartState(
val lines: List<CartLine> = emptyList(),
val promo: Promo? = null,
val promoError: String? = null,
) : Transmission.Data {
val itemCount: Int get() = lines.sumOf { it.quantity }
val subtotalCents: Long get() = lines.sumOf { it.totalCents }
val discountCents: Long get() = promo?.let { subtotalCents * it.percentOff / 100 } ?: 0
val totalCents: Long get() = subtotalCents - discountCents
}
Flow Relationships
The relationship between these types follows predictable patterns:
graph TD
UI[UI Layer] --> |User Input| S[Signal]
S --> |Processing| E1[Effect]
S --> |Direct Result| D1[Data]
E1 --> |Further Processing| E2[Effect]
E1 --> |Final Result| D2[Data]
E2 --> |Chain Processing| E3[Effect]
E2 --> |Final Result| D3[Data]
D1 --> |Consumption| UI
D2 --> |Consumption| UI
D3 --> |Consumption| UI
Processing in Transformers
A transformer handles signals and effects with on<T>, and produces data and further effects:
fun searchTransformer(api: SearchApi) = transformer("search") {
on<SearchSignal> { signal ->
publish(LogEffect("Searching for: ${signal.query}", LogLevel.INFO)) // an effect
emit(SearchResultsData(api.search(signal.query))) // data for observers
}
on<LogEffect> { effect ->
logger.log(effect.level, effect.message)
if (effect.level == LogLevel.ERROR) publish(NotificationEffect("Error occurred"))
}
}
The deprecated classic API handles these with onSignal<T>, onEffect<T> and send(data) inside configure { };
see Handlers.
Best Practices
Signal Design
- Keep signals simple and focused
- Use sealed interfaces for related signals
- Include only necessary data
- Make them immutable
// Good
sealed interface UserSignal : Transmission.Signal {
data class Login(val credentials: Credentials) : UserSignal
data object Logout : UserSignal
}
// Avoid
data class UserSignal(
val action: String, // String-based actions are error-prone
val data: Any? // Generic data is not type-safe
) : Transmission.Signal
Effect Design
- Use effects for side effects and intermediate processing
- Chain effects for complex workflows
- Keep effects focused on single responsibilities
// Good - focused effects
data class ValidateInputEffect(val input: String) : Transmission.Effect
data class SaveDataEffect(val data: UserData) : Transmission.Effect
// Better - sealed interface for related effects
sealed interface UserEffect : Transmission.Effect {
data class Validate(val input: String) : UserEffect
data class Save(val data: UserData) : UserEffect
data class Delete(val id: String) : UserEffect
}
Data Design
- Make data complete and ready for consumption
- Include all necessary information for UI
- Keep data immutable
// Good - complete UI state
data class ProfileData(
val user: User,
val isLoading: Boolean,
val error: String?
) : Transmission.Data
// Avoid - incomplete information
data class ProfileData(val userId: String) : Transmission.Data // UI needs more info