Skip to content

Commit

Permalink
fix timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 19, 2024
1 parent e085fa9 commit 32782dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class MediumScreenComponent(
override val rating: StateFlow<Int> = combine(
mediumSuccessState.mapNotNull {
it?.data?.entry?.score?.toInt()
},
}.flowOn(ioDispatcher()),
changedRating
) { t1, t2 ->
if (t2 > -1) {
Expand Down Expand Up @@ -338,16 +338,20 @@ class MediumScreenComponent(
}

return tokenResult.isSuccess.alsoTrue {
val query = MediaListEntryQuery(
id = Optional.present(mediaId.saveFirstOrNull() ?: mediaId.value)
)
val execution = CatchResult.timeout(5.seconds) {
apolloClient.query(query).execute()
}.asNullableSuccess()

execution?.data?.MediaList?.let { entry ->
changedRating.emit(entry.score?.toInt() ?: changedRating.value)
}
requestMediaListEntry()
}
}

private suspend fun requestMediaListEntry() {
val query = MediaListEntryQuery(
id = Optional.present(mediaId.saveFirstOrNull() ?: mediaId.value)
)
val execution = CatchResult.timeout(5.seconds) {
apolloClient.query(query).execute()
}.asNullableSuccess()

execution?.data?.MediaList?.let { entry ->
changedRating.emit(entry.score?.toInt() ?: changedRating.value)
}
}

Expand All @@ -362,6 +366,11 @@ class MediumScreenComponent(
}
}
} else {
val currentRating = rating.saveFirstOrNull() ?: rating.value
if (currentRating <= -1) {
requestMediaListEntry()
}

withMainContext {
onLoggedIn()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package dev.datlag.aniflow.model
import dev.datlag.aniflow.model.CatchResult.Companion.result
import dev.datlag.aniflow.model.CatchResult.Success
import dev.datlag.tooling.async.suspendCatching
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -121,8 +118,10 @@ sealed interface CatchResult<T> {
time: Duration,
block: suspend CoroutineScope.() -> T
): CatchResult<T & Any> = coroutineScope {
val result = suspendCatching {
withTimeout(time, block)
val result: Result<T> = try {
Result.success(withTimeout(time, block))
} catch (timeout: TimeoutCancellationException) {
Result.failure(timeout)
}

return@coroutineScope if (result.isFailure) {
Expand Down

0 comments on commit 32782dd

Please sign in to comment.