Skip to content

Commit

Permalink
Fix progress still displaying after cancelling tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Jan 27, 2025
1 parent 090d4db commit 5c5ad67
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import eu.darken.sdmse.common.debug.logging.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import kotlin.coroutines.EmptyCoroutineContext

fun <T : Progress.Client> T.updateProgressIcon(icon: Drawable) {
Expand Down Expand Up @@ -85,7 +87,7 @@ suspend fun <T : Progress.Host> T.forwardProgressTo(
onCompletion: (Progress.Data?) -> Progress.Data?,
) = progress
.onEach { new -> client.updateProgress { onUpdate(it, new) } }
.onCompletion { client.updateProgress { onCompletion(it) } }
.onCompletion { if (currentCoroutineContext().isActive) client.updateProgress { onCompletion(it) } }

suspend fun <T : Progress.Host, R> T.withProgress(
client: Progress.Client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,18 @@ class TaskManager @Inject constructor(
stage(taskId)
result = execute(taskId)

log(TAG) { "Result for $taskId is $result" }
log(TAG) { "Result for ${task.type}-$taskId is $result" }
} catch (e: Exception) {
if (e is CancellationException) {
log(TAG, INFO) { "execute(): Task was cancelled ($taskId): $task" }
log(TAG, INFO) { "execute(): Task was cancelled (${task.type}-$taskId): $task" }
} else {
log(TAG, ERROR) { "execute(): Execution failed ($taskId): $task\n${e.asLog()}" }
log(TAG, ERROR) { "execute(): Execution failed (${task.type}-$taskId): $task\n${e.asLog()}" }
}
error = e
} finally {
updateTasks {
this[taskId]!!.tool.updateProgress { null }
log(TAG) { "Releasing resource lock for $taskId" }
log(TAG) { "Releasing resource lock for ${task.type}-$taskId" }
this[taskId]!!.resourceLock!!.close()
this[taskId] = this[taskId]!!.copy(
completedAt = Instant.now(),
Expand Down

0 comments on commit 5c5ad67

Please sign in to comment.