Skip to content

Commit

Permalink
Separate the part that waits for the computation result of the deferr…
Browse files Browse the repository at this point in the history
…ed list into an await method
  • Loading branch information
icepeppermint committed Jan 7, 2023
1 parent c369ab0 commit 0a34d1c
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,7 @@ class ComposedCoroutineCondition internal constructor(
cancel(ds)
throw e
}
val it = ds.iterator()
var value = it.next().await()
while (it.hasNext()) {
val next = it.next().await()
value = when (operator) {
AND -> value && next
OR -> value || next
}
}
value
ds.await()
}

private suspend fun completed(d: Deferred<Boolean>) = d.isCompleted && shortCircuit(operator, d.await())
Expand Down Expand Up @@ -110,6 +101,18 @@ class ComposedCoroutineCondition internal constructor(
return value()
}

private suspend fun MutableList<Deferred<Boolean>>.await() = with(iterator()) {
var value = next().await()
while (hasNext()) {
val next = next().await()
value = when (operator) {
AND -> value && next
OR -> value || next
}
}
value
}

override fun toString(): String {
if (!alias.isNullOrBlank()) return alias
assert(conditions.isNotEmpty())
Expand Down

0 comments on commit 0a34d1c

Please sign in to comment.