Skip to content

Commit

Permalink
[gateway] fix message state update, or not?
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Apr 4, 2024
1 parent f15d9c8 commit a325fd8
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.ktor.client.plugins.ClientRequestException
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.capcom.smsgateway.data.entities.Message
Expand All @@ -26,6 +26,8 @@ class GatewayModule(
private val settings: GatewaySettings,
) {
private var _api: GatewayApi? = null
private var _job: Job? = null

private val api
get() = _api ?: GatewayApi(
settings.privateUrl ?: GatewaySettings.PUBLIC_URL,
Expand All @@ -49,17 +51,15 @@ class GatewayModule(
PushService.register(context)
PullMessagesWorker.start(context)

scope.launch {
withContext(Dispatchers.IO) {
messagesService.events.events.collect { event ->
val event = event as? MessageStateChangedEvent ?: return@collect
if (event.source != MessageSource.Gateway) return@collect
_job = scope.launch {
messagesService.events.events.collect { event ->
val event = event as? MessageStateChangedEvent ?: return@collect
if (event.source != MessageSource.Gateway) return@collect

try {
sendState(event)
} catch (th: Throwable) {
th.printStackTrace()
}
try {
sendState(event)
} catch (th: Throwable) {
th.printStackTrace()
}
}
}
Expand All @@ -68,7 +68,8 @@ class GatewayModule(
fun isActiveLiveData(context: Context) = PullMessagesWorker.getStateLiveData(context)

fun stop(context: Context) {
scope.cancel()
_job?.cancel()
_job = null
PullMessagesWorker.stop(context)
this._api = null
}
Expand Down Expand Up @@ -207,6 +208,6 @@ class GatewayModule(

companion object {
private val job = SupervisorJob()
private val scope = CoroutineScope(job)
private val scope = CoroutineScope(job + Dispatchers.IO)
}
}

0 comments on commit a325fd8

Please sign in to comment.