This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
31 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
korio/src/commonMain/kotlin/com/soywiz/korio/async/WaitSubscriber.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.soywiz.korio.async | ||
|
||
import com.soywiz.korio.lang.* | ||
import kotlinx.coroutines.* | ||
import kotlin.jvm.* | ||
|
||
suspend fun <T> waitSubscriber(block: ((T) -> Unit) -> Cancellable): T { | ||
val deferred = CompletableDeferred<T>() | ||
@Suppress("JoinDeclarationAndAssignment") | ||
lateinit var cancellable: Cancellable | ||
cancellable = block { | ||
cancellable.cancel() | ||
deferred.complete(it) | ||
} | ||
try { | ||
return deferred.await() | ||
} catch (e: CancellationException) { | ||
cancellable.cancel() | ||
throw e | ||
} | ||
} | ||
|
||
suspend fun <T> waitSubscriberCloseable(block: ((T) -> Unit) -> Closeable): T = | ||
waitSubscriber { block(it).cancellable() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters