Skip to content

Classic API: TransmissionRouter and RouterViewModel

Deprecated

This is the classic API, which is deprecated. It keeps working in 2.x, but new code should use the builder API described in TransmissionRouter.

Routers can also be created with TransmissionRouter { } and cleared with router.clear(), and ViewModels can extend RouterViewModel, which owns a router and offers stream helpers. Both keep working.

Basic Setup

Simple Router Creation

val router = TransmissionRouter {
    addTransformerSet(setOf(
        UserTransformer(),
        AuthTransformer(),
        DataTransformer()
    ))
}

With Custom Configuration

val router = TransmissionRouter {
    addTransformerSet(transformers)
    addDispatcher(Dispatchers.IO) 
    setCapacity(Capacity.Custom(256))
}

Creation API

Create routers with the builder function from com.yigitozgumus.transmission.router.builder.TransmissionRouter:

fun TransmissionRouter(
    identity: Contract.Identity = Contract.identity(),
    scope: TransmissionRouterBuilderScope.() -> Unit,
): TransmissionRouter

Important builder options:

  • addTransformerSet(setOf(...)): install a static, non-empty transformer set.
  • addLoader(loader): install a TransformerSetLoader for deferred loading during initialization.
  • addDispatcher(dispatcher): set the router coroutine dispatcher.
  • setCapacity(capacity): set internal channel capacity.
  • overrideInitialization(): disable automatic initialization; call initialize(loader) later.
  • registerToGlobalRouter(enabled): opt in/out of process-wide router registration. Disabled by default.
  • validateGlobalContracts(enabled): fail initialization when another globally registered router exposes duplicate data/computation/execution contracts. Disabled by default.

Builder Configuration

Adding Transformers

// Direct transformer set
val router = TransmissionRouter {
    addTransformerSet(setOf(
        CounterTransformer(),
        LoggingTransformer()
    ))
}

// Using a loader for lazy initialization
val router = TransmissionRouter {
    addLoader(MyTransformerSetLoader())
}

Setting Capacity

Capacity sets how many data and effect items the router keeps for observers (streamData, streamEffect) that have not subscribed yet or are falling behind. When the buffer is full the oldest items are dropped, so a slow or missing observer never stalls a transformer. Use Capacity.Unlimited to never drop. Transformers themselves always receive every signal and effect: their mailboxes are not bounded by capacity.

val router = TransmissionRouter {
    addTransformerSet(transformers)
    setCapacity(Capacity.Custom(256)) // keep up to 256 items for slow observers
}

// Available capacity options
Capacity.Default
Capacity.Custom(128) // value must be between 0 and 256
Capacity.Unlimited

Custom Dispatcher

val router = TransmissionRouter {
    addTransformerSet(transformers)
    addDispatcher(Dispatchers.IO) // Use IO dispatcher for network/database operations
}

Manual Initialization

val router = TransmissionRouter {
    overrideInitialization() // Disable auto-initialization
}

// Later, initialize manually
router.initialize(myTransformerSetLoader)

Processing Transmissions

Processing Signals

// Process signals from UI
router.process(UserLoginSignal(credentials))
router.process(RefreshDataSignal)
router.process(CounterSignal.Increment)

Processing Effects

// Process effects (typically from other transformers)
router.process(LoggingEffect("Manual log entry"))
router.process(NotificationEffect("Custom notification"))

Data Streams

Observing All Data

// Collect all data from all transformers
lifecycleScope.launch {
    router.streamData().collect { data ->
        when (data) {
            is UserData -> updateUserUI(data)
            is CounterData -> updateCounterUI(data)
            is ErrorData -> showError(data)
        }
    }
}

Filtering Specific Data Types

// Observe only user data
lifecycleScope.launch {
    router.streamData<UserData>()
        .collect { userData ->
            updateUserProfile(userData)
        }
}

// Observe multiple specific types
lifecycleScope.launch {
    router.streamData()
        .filter { it is UserData || it is ProfileData }
        .collect { data ->
            when (data) {
                is UserData -> handleUserData(data)
                is ProfileData -> handleProfileData(data) 
            }
        }
}

Effect Streams

Access to the effect stream for monitoring:

lifecycleScope.launch {
    router.streamEffect<LoggingEffect>()
        .collect { effect ->
            println("Log: ${effect.message}")
        }
}

Query Helper

The router provides a query helper for debugging and monitoring:

val queryHelper: QueryHandler = router.queryHelper

// Use for debugging transformer states
// (Specific usage depends on your debugging needs)

Advanced Features

Custom Transformer Set Loader

For complex applications, you might want to load transformers dynamically:

class CustomTransformerSetLoader : TransformerSetLoader {
    override suspend fun load(): Set<Transformer> {
        return buildSet {
            // Load based on configuration
            if (isFeatureEnabled("auth")) {
                add(AuthTransformer())
            }
            if (isFeatureEnabled("analytics")) {
                add(AnalyticsTransformer()) 
            }
            // Always include core transformers
            add(CoreTransformer())
        }
    }
}

val router = TransmissionRouter {
    addLoader(CustomTransformerSetLoader())
}

Multiple Routers and Global Routing

Routers are isolated by default. Routers that opt in with registerToGlobalRouter() join the process-wide global router, which allows:

  • broadcast effects from one router to be received by other registered routers;
  • targeted effects to cross routers when the target transformer identity exists in another router;
  • unresolved data/computation/execution queries to fall back to other registered routers.
val authRouter = TransmissionRouter(Contract.identity("auth-router")) {
    addTransformerSet(authTransformers)
    registerToGlobalRouter()
}

val dataRouter = TransmissionRouter(Contract.identity("data-router")) {
    addTransformerSet(dataTransformers)
    registerToGlobalRouter()
}

Only routers that opt in see each other. A transformer in one registered router can query contracts owned by another; the result is routed back to the asking router.

Enable duplicate-contract validation when global query ownership must be deterministic:

val router = TransmissionRouter {
    addTransformerSet(featureTransformers)
    registerToGlobalRouter()
    validateGlobalContracts()
}

Lifecycle Management

Proper Cleanup

class MyRepository {
    private val router = TransmissionRouter { 
        addTransformerSet(transformers)
    }

    fun cleanup() {
        router.clear() // Cleans up all transformers and coroutines
    }
}

// In Android ViewModel
class MyViewModel : ViewModel() {
    private val router = TransmissionRouter { /* config */ }

    override fun onCleared() {
        router.clear()
        super.onCleared()
    }
}

Integration with Dependency Injection

Hilt Example

@Module
@InstallIn(SingletonComponent::class)
object TransmissionModule {

    @Provides
    @Singleton
    fun provideTransformers(): Set<Transformer> = setOf(
        UserTransformer(),
        AuthTransformer(),
        DataTransformer()
    )

    @Provides
    @Singleton
    fun provideTransmissionRouter(
        transformers: Set<Transformer>
    ): TransmissionRouter = TransmissionRouter {
        addTransformerSet(transformers)
        setCapacity(Capacity.Custom(256))
    }
}

Koin Example

val transmissionModule = module {
    single { UserTransformer() }
    single { AuthTransformer() }
    single { DataTransformer() }

    single<Set<Transformer>> { 
        setOf(get<UserTransformer>(), get<AuthTransformer>(), get<DataTransformer>()) 
    }

    single { 
        TransmissionRouter {
            addTransformerSet(get<Set<Transformer>>())
            setCapacity(Capacity.Custom(256))
        }
    }
}

Error Handling

The router includes built-in error handling:

// Transformer errors are isolated and don't crash the router
// Individual transformers can override onError() to handle their own errors

class SafeTransformer : Transformer() {
    override fun onError(throwable: Throwable) {
        // Handle transformer-specific errors
        publish(ErrorEffect("Transformer error: ${throwable.message}"))
    }
}

Performance Considerations

Capacity Settings

// For high-throughput applications
val router = TransmissionRouter {
    addTransformerSet(transformers)
    setCapacity(Capacity.Custom(256)) // or Capacity.Unlimited for extreme cases
}

// For memory-constrained environments
val router = TransmissionRouter {
    addTransformerSet(transformers) 
    setCapacity(Capacity.Custom(16))
}

Dispatcher Selection

// For CPU-intensive work
val router = TransmissionRouter {
    addTransformerSet(transformers)
    addDispatcher(Dispatchers.Default)
}

// For IO-intensive work
val router = TransmissionRouter {
    addTransformerSet(transformers)
    addDispatcher(Dispatchers.IO)
}

// For UI-related work (use sparingly)
val router = TransmissionRouter {
    addTransformerSet(transformers)
    addDispatcher(Dispatchers.Main.immediate)
}

RouterViewModel

RouterViewModel is a convenience wrapper around TransmissionRouter that extends androidx.lifecycle.ViewModel. It's fully Kotlin Multiplatform compatible and provides automatic setup, lifecycle management, and simplified stream handling across all platforms.

Key Features

  • Automatic Setup: Router is configured and initialized automatically
  • Lifecycle Management: Router cleanup is handled in onCleared()
  • Stream Collection: Data and effect streams are automatically collected in viewModelScope
  • Hook Methods: Override onData, onEffect, onProcessSignal, and onProcessEffect for custom logic
  • Query Support: Built-in access to QueryHandler for querying transformer states
  • StateFlow Helpers: Built-in methods for converting streams to StateFlow
  • Error Handling: Built-in error handling with onError callback

Basic Usage

Simple Implementation

class UserViewModel : RouterViewModel(
    setOf(
        UserTransformer(),
        AuthTransformer(),
        ProfileTransformer()
    )
) {
    // Create StateFlow from data streams
    val userState = streamDataAsState<UserData>(UserData.Empty)
    val authState = streamDataAsState<AuthData>(AuthData.LoggedOut)

    // Handle data updates
    override fun onData(data: Transmission.Data) {
        when (data) {
            is UserData -> logUserUpdate(data)
            is AuthData -> handleAuthChange(data)
        }
    }

    // Handle effects
    override fun onEffect(effect: Transmission.Effect) {
        when (effect) {
            is NavigationEffect -> navigateTo(effect.destination)
            is ErrorEffect -> showError(effect.message)
        }
    }

    // User actions
    fun login(credentials: Credentials) {
        processSignal(AuthSignal.Login(credentials))
    }

    fun updateProfile(profile: Profile) {
        processSignal(UserSignal.UpdateProfile(profile))
    }
}

Advanced Configuration

class FeatureViewModel : RouterViewModel(
    loader = FeatureTransformerSetLoader(),
    config = RouterViewModelConfig(
        capacity = Capacity.Custom(256),
        dispatcher = Dispatchers.IO,
        identity = Contract.identity("feature-router")
    )
) {
    // Type-safe stream access
    val errorStream = streamEffect<ErrorEffect>()
    val loadingData = streamData<LoadingData>()

    // Custom error handling
    override fun onError(throwable: Throwable) {
        logError("Router error", throwable)
        processEffect(ErrorEffect("System error occurred"))
    }

    // Post-processing hooks
    override fun onProcessSignal(signal: Transmission.Signal) {
        analytics.trackSignal(signal::class.simpleName)
    }

    // Query transformer states
    suspend fun getCurrentUserData(): UserData? {
        return queryHandler.getData(UserTransformer.dataContract)
    }
}

Configuration Options

RouterViewModelConfig

data class RouterViewModelConfig(
    val capacity: Capacity = Capacity.Default,           // Buffer capacity
    val dispatcher: CoroutineDispatcher = Dispatchers.Default, // Coroutine dispatcher
    val identity: Contract.Identity = Contract.identity(),      // Router identity
    val registerToGlobalRouter: Boolean = false,                // Join the global router
)

Usage with Configuration

// High-performance configuration
val config = RouterViewModelConfig(
    capacity = Capacity.Custom(256),
    dispatcher = Dispatchers.IO
)

class MyViewModel : RouterViewModel(transformers, config) {
    // Implementation
}

Stream Helpers

StateFlow Creation

State helpers share in viewModelScope and start eagerly, so data published while the UI is not collecting is not missed. Prefer the contract-based variant for data holders: it follows one holder and starts from its current value.

class ShoppingViewModel : RouterViewModel(shoppingTransformers) {
    // Follows one data holder; starts from its current value
    val cartState = streamDataAsState(Cart.state, CartData.Empty)

    // Latest data of a type, from any transformer
    val productsState = streamDataAsState<ProductListData>(ProductListData.Loading)

    // Nullable: empty until the first result is published
    val lastReceipt = streamDataAsState<ReceiptData?>(null)

    // Custom sharing behavior
    val userPreferences = streamDataAsState<UserPreferencesData>(
        initialValue = UserPreferencesData.Default,
        started = SharingStarted.WhileSubscribed(5_000)
    )
}

Type-Safe Stream Access

class ChatViewModel : RouterViewModel(chatTransformers) {
    // Access specific data types
    val messageFlow = streamData<MessageData>()
    val userStatusFlow = streamData<UserStatusData>()

    // Access specific effect types  
    val notificationEffects = streamEffect<NotificationEffect>()
    val navigationEffects = streamEffect<NavigationEffect>()

    init {
        // Custom stream handling
        viewModelScope.launch {
            navigationEffects.collect { effect ->
                handleNavigation(effect)
            }
        }
    }
}

Dependency Injection Integration

Hilt Example

@HiltViewModel
class MainViewModel @Inject constructor(
    private val transformerSetLoader: MainTransformerSetLoader
) : RouterViewModel(
    loader = transformerSetLoader,
    config = RouterViewModelConfig(capacity = Capacity.Custom(256))
) {
    val mainState = streamDataAsState<MainScreenData>(MainScreenData.Loading)

    fun refreshData() {
        processSignal(RefreshSignal)
    }
}

Koin Example

val viewModelModule = module {
    viewModel { 
        UserViewModel(
            transformerSet = get<Set<Transformer>>(),
            config = RouterViewModelConfig(
                dispatcher = get(named("ioDispatcher"))
            )
        )
    }
}

class UserViewModel(
    transformerSet: Set<Transformer>,
    config: RouterViewModelConfig
) : RouterViewModel(transformerSet, config) {
    // Implementation
}

Error Handling

class RobustViewModel : RouterViewModel(transformers) {
    override fun onError(throwable: Throwable) {
        when (throwable) {
            is NetworkException -> handleNetworkError(throwable)
            is ValidationException -> handleValidationError(throwable)
            else -> {
                logError("Unexpected router error", throwable)
                processEffect(ErrorEffect("Something went wrong"))
            }
        }
    }

    override fun onProcessSignal(signal: Transmission.Signal) {
        // Log all processed signals for debugging
        if (BuildConfig.DEBUG) {
            Log.d("RouterViewModel", "Processed signal: $signal")
        }
    }
}

Multiplatform Support

RouterViewModel is built using androidx.lifecycle.ViewModel which is fully multiplatform:

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope

This means you can use RouterViewModel across all supported platforms: - Android: Native AndroidX ViewModel support - iOS: Full lifecycle management via androidx.lifecycle-viewmodel - Desktop: JVM-based lifecycle management
- Web: Kotlin/JS lifecycle management

Platform-Specific Considerations

While RouterViewModel works on all platforms, you may want different configurations per platform:

// Platform-specific configurations
expect fun defaultRouterConfig(): RouterViewModelConfig

// androidMain
actual fun defaultRouterConfig() = RouterViewModelConfig(
    capacity = Capacity.Custom(256),
    dispatcher = Dispatchers.Main.immediate
)

// iosMain  
actual fun defaultRouterConfig() = RouterViewModelConfig(
    capacity = Capacity.Default,
    dispatcher = Dispatchers.Main
)

// Usage
class MyViewModel : RouterViewModel(
    transformers = myTransformers,
    config = defaultRouterConfig()
)

Comparison: Direct Router vs RouterViewModel

Direct TransmissionRouter

class ManualViewModel : ViewModel() {
    private val router = TransmissionRouter {
        addTransformerSet(transformers)
    }

    val dataState = router.streamData<MyData>()
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), MyData.Empty)

    init {
        viewModelScope.launch {
            router.streamEffect<MyEffect>().collect { effect ->
                handleEffect(effect)
            }
        }
    }

    fun processUserAction(action: MySignal) {
        router.process(action)
    }

    override fun onCleared() {
        router.clear()
        super.onCleared()
    }
}

RouterViewModel

class SimpleViewModel : RouterViewModel(transformers) {
    val dataState = streamDataAsState<MyData>(MyData.Empty)

    override fun onEffect(effect: Transmission.Effect) {
        if (effect is MyEffect) {
            handleEffect(effect)
        }
    }

    fun processUserAction(action: MySignal) {
        processSignal(action)
    }

    // Cleanup is automatic!
}

Best Practices for RouterViewModel

1. Use StateFlow for UI State

class GoodViewModel : RouterViewModel(transformers) {
    // ✅ Good: Direct StateFlow creation
    val uiState = streamDataAsState<UiState>(UiState.Loading)

    // ❌ Avoid: Manual StateFlow conversion in ViewModels
    // val uiState = streamData<UiState>()
    //     .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), UiState.Loading)
}

2. Handle Effects Properly

class EffectHandlingViewModel : RouterViewModel(transformers) {
    override fun onEffect(effect: Transmission.Effect) {
        when (effect) {
            is NavigationEffect -> {
                // Handle navigation - don't process signals here
                navigationManager.navigateTo(effect.destination)
            }
            is ValidationEffect -> {
                // Handle validation feedback
                showValidationError(effect.message)
            }
        }
    }
}

3. Use Configuration for Performance

// ✅ Good: Configure based on use case
class HighThroughputViewModel : RouterViewModel(
    transformers = heavyTransformers,
    config = RouterViewModelConfig(
        capacity = Capacity.Custom(256),
        dispatcher = Dispatchers.Default
    )
)

class IOIntensiveViewModel : RouterViewModel(
    transformers = networkTransformers,
    config = RouterViewModelConfig(
        dispatcher = Dispatchers.IO
    )
)

Best Practices

  1. Single Router per Feature: Use one router per logical feature or module
  2. Proper Cleanup: Always call router.clear() when done
  3. Appropriate Capacity: Choose capacity based on your app's throughput needs
  4. Error Handling: Implement proper error handling in transformers
  5. Stream Management: Use appropriate lifecycle scopes for data stream collection
  6. Testing: The router is designed to be easily testable - see Testing guide