package com.shr4pnel.ferretirc.irc import com.shr4pnel.ferretirc.net.messages.ClientMessage import com.shr4pnel.ferretirc.net.messages.ServerMessage import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.onSubscription import kotlinx.coroutines.flow.takeWhile class Server( private val msgBuffer: SharedFlow, private val outgoingMessages: Channel, private val scope: CoroutineScope, ) { lateinit var features: ISupportFeatures private set var channels: Set = setOf() private set suspend fun fetchChannels(): Set { val buffer = mutableSetOf() msgBuffer .onSubscription { outgoingMessages.send(ClientMessage.List()) } .filterIsInstance() .takeWhile { it !is ServerMessage.Numeric.RPL_LISTEND } .filterIsInstance() .collect { buffer.add(IRCChannel(it.chanName, it.clientCount, it.topic)) } channels = buffer.toSortedSet(compareBy { it.chanName }) return channels } fun fetchFeatures() { } }