https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Laxystem

03/13/2024, 10:40 AM
Say I have a list of objects that each have their own coroutine scope, and I want to execute a function on every one of them - in a way that if their coroutine scope is cancelled, said function is cancelled too. However, if one of said functions fails - and not because it is cancelled - I want it to throw its exception, and cancel all the other ones. How would I do this such a thing? What I mean is, similarly to
awaitAll
- but when encountering a
CancellationException
, it just silently ignores it instead of cancelling all the other coroutines, similarly to
SupervisorJob
.
Found something that I think works -- just voiding cancellations. Don't like it tho.
Copy code
dependencies.map { (dependency, dependencyScope) ->
                dependencyScope.async {
                    try {
                        dependency.beforeDependentLoad(
                            instance = this@InstanceImpl,
                            extension,
                            dynamicallyTriggered = true
                        )
                    } catch (_: CancellationException) { }
                }
            }.awaitAll()
j

Joffrey

03/13/2024, 3:36 PM
Why do dependencies have each their own scopes?
l

Laxystem

03/13/2024, 3:51 PM
@Joffrey a dependency in this case is just an extension that happens to be a dependency of another. We want to have dynamically un/loadable extensions, and therefore each one has to have its own scope, for cancellabilty purposes.
Besides, it's an amazing way to know what extension executed what code.