Skip to content

Commit

Permalink
Rename to AwaitAllScope
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed May 28, 2024
1 parent f1168d4 commit b331238
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import kotlinx.coroutines.async as coroutinesAsync
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.awaitAll as coroutinesAwaitAll
import kotlinx.coroutines.coroutineScope
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

public suspend fun <A> await(
block: suspend AwaitScope.() -> A
): A = coroutineScope { block(AwaitScope(this)) }
public suspend fun <A> awaitAll(
block: suspend AwaitAllScope.() -> A
): A = coroutineScope { block(AwaitAllScope(this)) }

public suspend fun <A> CoroutineScope.await(
block: suspend AwaitScope.() -> A
): A = block(AwaitScope(this))
public suspend fun <A> CoroutineScope.awaitAll(
block: suspend AwaitAllScope.() -> A
): A = block(AwaitAllScope(this))

/**
* Within an [AwaitScope], any call to [kotlinx.coroutines.Deferred.await]
* Within an [AwaitAllScope], any call to [kotlinx.coroutines.Deferred.await]
* causes all the other [Deferred] in the same block to be awaited too.
* That way you can get more concurrency without having to sacrifice
* readability.
Expand All @@ -46,7 +46,7 @@ public suspend fun <A> CoroutineScope.await(
* }
* ```
*/
public class AwaitScope(
public class AwaitAllScope(
private val scope: CoroutineScope
): CoroutineScope by scope {
private val tasks: Atomic<List<Deferred<*>>> = Atomic(emptyList())
Expand All @@ -65,7 +65,7 @@ public class AwaitScope(
private val deferred: Deferred<T>
): Deferred<T> by deferred {
override suspend fun await(): T {
tasks.getAndSet(emptyList()).awaitAll()
tasks.getAndSet(emptyList()).coroutinesAwaitAll()
return deferred.await()
}
}
Expand Down

0 comments on commit b331238

Please sign in to comment.