Maciek
11/29/2021, 3:09 PMcallbackFlow with awaitClose work here?
fun CoroutineScope.onCancel(action: () -> Unit) {
callbackFlow<Unit> {
awaitClose(action)
}.launchIn(this)
}Joffrey
11/29/2021, 3:10 PMJob from the scope's context:
fun CoroutineScope.onCancel(action: () -> Unit) {
coroutineContext.job.invokeOnCompletion { ex ->
if (ex is CancellationException) {
// do stuff
}
}
}Maciek
11/29/2021, 3:11 PMJoffrey
11/29/2021, 3:12 PMinvokeOnCompletion that you could use, but I'm not very familiar with it, so please read the docs 😉Joffrey
11/29/2021, 3:15 PMJob in their contexts, in which case coroutineContext.job will fail. You can use coroutineContext[Job] instead if you want to get a nullable Job? instead and deal with it.
If there is no Job, there is also no structurred concurrency, so I guess your use case only makes sense on scopes with a Job, but still good to knowMaciek
11/29/2021, 3:16 PM