Now using coroutines in ThreadedModule.

This commit is contained in:
Erik C. Thauvin 2021-09-15 13:03:01 -07:00
parent f4c6bf48fa
commit 25a0850b71
5 changed files with 20 additions and 10 deletions

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ -47,12 +48,14 @@ import java.util.Date
* Handles posts to pinboard.in.
*/
object PinboardUtils {
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
/**
* Adds a pin.
*/
@JvmStatic
fun addPin(poster: PinboardPoster, ircServer: String, entry: EntryLink) = runBlocking {
withContext(Dispatchers.Default) {
withContext(dispatcher) {
poster.addPin(
entry.link,
entry.title,
@ -68,7 +71,7 @@ object PinboardUtils {
*/
@JvmStatic
fun deletePin(poster: PinboardPoster, entry: EntryLink) = runBlocking {
withContext(Dispatchers.Default) {
withContext(dispatcher) {
poster.deletePin(entry.link)
}
}
@ -78,7 +81,7 @@ object PinboardUtils {
*/
@JvmStatic
fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) = runBlocking {
withContext(Dispatchers.Default) {
withContext(dispatcher) {
with(entry) {
if (oldUrl != link) {
poster.deletePin(oldUrl)

View file

@ -31,6 +31,8 @@
*/
package net.thauvin.erik.mobibot.modules
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.mobibot.Mobibot
/**
@ -44,7 +46,11 @@ abstract class ThreadedModule(bot: Mobibot) : AbstractModule(bot) {
isPrivate: Boolean
) {
if (isEnabled && args.isNotEmpty()) {
Thread { run(sender, cmd, args, isPrivate) }.start()
runBlocking {
launch {
run(sender, cmd, args, isPrivate)
}
}
} else {
helpResponse(sender, isPrivate)
}