Skip to content

Broadcast

Broadcast is PulseMVI's mechanism for delivering a typed message from a PulseContainer to all of its registered PulseStore instances simultaneously.

Defining a Broadcast

Implement PulseBroadcast with a sealed class:

kotlin
sealed class AppBroadcast : PulseBroadcast {
    data object Sync : AppBroadcast()
    data object UserLoggedOut : AppBroadcast()
    data class ThemeChanged(val isDark: Boolean) : AppBroadcast()
}

Sending a Broadcast

Call broadcast() on the Container:

kotlin
container.broadcast(AppBroadcast.ThemeChanged(isDark = true))

Receiving a Broadcast

Override onReceive() in each Store that needs to react:

kotlin
override fun onReceive(broadcast: AppBroadcast) {
    when (broadcast) {
        AppBroadcast.Sync -> syncData()
        AppBroadcast.UserLoggedOut -> update { AppState() }
        is AppBroadcast.ThemeChanged -> update { copy(isDark = broadcast.isDark) }
    }
}

Broadcast vs Event

BroadcastEvent
DirectionContainer → all StoresStore → UI
CardinalityOne-to-manyOne-to-one
PurposeCross-Store coordinationOne-time UI side effects
Type parameterPulseBroadcastPulseEvent

Example: Syncing Multiple Stores

kotlin
// Two Stores sharing the same Broadcast type
class HeaderStore : PulseStore<HeaderState, HeaderAction, HeaderEvent, AppBroadcast>(...) {
    override fun onReceive(broadcast: AppBroadcast) {
        if (broadcast is AppBroadcast.UserLoggedOut) update { HeaderState() }
    }
}

class SidebarStore : PulseStore<SidebarState, SidebarAction, SidebarEvent, AppBroadcast>(...) {
    override fun onReceive(broadcast: AppBroadcast) {
        if (broadcast is AppBroadcast.UserLoggedOut) update { SidebarState() }
    }
}

// One call resets both Stores
container.broadcast(AppBroadcast.UserLoggedOut)

Released under the Apache 2.0 License.