How do I emit multiple flows synchronously?

My app has three flows, namely account, timeline, timelinePosition

val activeAccountFlow = accountDao
 .getActiveAccountFlow()
 .filterNotNull()
 .distinctUntilChanged { old, new -> old.id == new.id }
  
val timeline = activeAccountFlow
 .flatMapLatest { timelineDao.getStatusListWithFlow(it.id) }
 .stateIn(
   scope = viewModelScope,
   started = SharingStarted.WhileSubscribed(),
   initialValue = persistentListOf()
 )
  
  
val timelinePosition = activeAccountFlow
 .mapLatest { TimelinePosition(it.index, it.offset) }
 .stateIn(
   scope = viewModelScope,
   started = SharingStarted.WhileSubscribed(),
   initialValue = TimelinePosition()
 )

data class TimelinePosition(val index: Int = 0, val offset: Int = 0)

Where timeline, timeline Position flow depends on account flow

What I want to do now is: when the ID of the account changes, their values can be emitted together synchronously instead of asynchronously. What should I do?
The timeline should be Flow<List<…>> at the end, because I need to collect it in the UI part to get updates to the list, but it also needs to re-emit the entire flow depending on whether the account changes (because different accounts have different Lists , but the List of the same account will also be updated due to UI operations (such as paging requests))

This is a complex data flow and it bothered me for a long time and I don’t know what to do now :frowning:

I’m not exactly sure what do you mean by “synchronously” in this context. Do you mean that after changing the account ID, you need guarantee timeline and timelinePosition change at exactly the same time?

Yes, but i just solved this problem!

private val activeAccountFlow = accountDao
  .getActiveAccountFlow()
  .distinctUntilChanged { old, new -> old?.id == new?.id }
  .filterNotNull()

val homeCombinedFlow = activeAccountFlow
  .flatMapLatest { account ->
    val timelineFlow = timelineDao.getStatusListWithFlow(account.id)
    timelineFlow.map {
      HomeUserData(
        activeAccount = account,
        timeline = splitReorderStatus(it).toUiData().toImmutableList(),
        position = TimelinePosition(account.firstVisibleItemIndex, account.offset)
      )
    }
  }
  .stateIn(
    scope = viewModelScope,
    started = SharingStarted.Eagerly,
    initialValue = null
  )