PulseContainer
abstract class PulseContainer<Broadcast : PulseBroadcast, Unicast : PulseUnicast>(
viewModels: List<PulseViewModel<*, *, *, Broadcast, Unicast>>,
coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default,
)Coordinates multiple PulseViewModel instances. Provides broadcast delivery, child unicast handling, and view refresh.
The internal unicast collector uses Dispatchers.Default. Another dispatcher can be passed to the constructor.
Methods
broadcast(broadcast)
fun broadcast(broadcast: Broadcast)Delivers broadcast to every PulseViewModel registered at construction time. It calls each ViewModel's onReceive().
container.broadcast(AppBroadcast.UserLoggedOut)onReceived(unicast)
open fun onReceived(unicast: Unicast)Called when a registered PulseViewModel emits an unicast. PulseContainer collects each ViewModel's unicast flow internally and forwards each value to this hook.
override fun onReceived(unicast: AppUnicast) {
when (unicast) {
AppUnicast.SaveRequested -> broadcast(AppBroadcast.SaveStarted)
}
}refresh()
fun refresh()Bumps the container's internal key, and PulseHost re-creates every PulseContent block inside it. ViewModel state is preserved; only Compose state is discarded.
container.refresh()close()
fun close()Cancels the Container scope and stops collecting Unicast messages from the ViewModels. Call it when the Container is gone for good. rememberPulseContainer calls it for you when the owning ViewModelStore is cleared.
container.close()Example
class AppContainer(
viewModels: List<PulseViewModel<*, *, *, AppBroadcast, AppUnicast>>,
) : PulseContainer<AppBroadcast, AppUnicast>(viewModels = viewModels) {
override fun onReceived(unicast: AppUnicast) {
when (unicast) {
AppUnicast.SaveRequested -> broadcast(AppBroadcast.SaveStarted)
}
}
}
// Usage
val container = rememberPulseContainer {
AppContainer(viewModels = listOf(sidebarViewModel, contentViewModel))
}
// Send to all ViewModels
container.broadcast(AppBroadcast.Sync)
// Reconstruct view tree
container.refresh()