You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageHandler.kt 701B

1234567891011121314151617
  1. package com.dmdirc.ktirc.io
  2. import com.dmdirc.ktirc.events.IrcEvent
  3. import com.dmdirc.ktirc.messages.MessageProcessor
  4. import kotlinx.coroutines.channels.ReceiveChannel
  5. import kotlinx.coroutines.channels.consumeEach
  6. class MessageHandler(private val processors: Collection<MessageProcessor>, private val eventHandler: (IrcEvent) -> Unit) {
  7. suspend fun processMessages(messages: ReceiveChannel<IrcMessage>) {
  8. messages.consumeEach { it.process().forEach(eventHandler) }
  9. }
  10. private fun IrcMessage.process() = this.getProcessor()?.process(this) ?: emptyList()
  11. private fun IrcMessage.getProcessor() = processors.firstOrNull { it.commands.contains(this.command) }
  12. }