Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.eingruenesbeb.secommandchangelogbot.managers
- import kotlinx.coroutines.channels.Channel
- object ChannelManager {
- // A map to store channels indexed by their IDs
- private val channelMap = mutableMapOf<String, Channel<*>>()
- // Initialize a channel of a specific type with a given ID
- fun <T> initializeChannel(id: String, capacity: Int = Channel.RENDEZVOUS): Channel<T> {
- if (channelMap.containsKey(id)) {
- throw IllegalArgumentException("Channel with ID $id already exists.")
- }
- val channel = Channel<T>(capacity)
- channelMap[id] = channel
- return channel
- }
- // Retrieve a channel of a specific type using its ID
- @Suppress("UNCHECKED_CAST")
- fun <T> getChannel(id: String): Channel<T>? {
- return channelMap[id] as? Channel<T>
- }
- // Close a channel of a specific type using its ID
- fun closeChannel(id: String) {
- val channel = channelMap[id]
- channel?.close()
- channelMap.remove(id)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement