Skip to content

Kairo Coroutines

kairo-coroutines extends Kotlin’s coroutine library.

  • Provides Arrow’s coroutines library, which includes coroutine-safe concurrency primitives like CountDownLatch and CyclicBarrier.
  • Adds some convenient helper functions.

Install kairo-coroutines. You don’t need to install Kotlin or Arrow’s coroutines libraries separately — they’re included by default.

build.gradle.kts
dependencies {
implementation("software.airborne.kairo:kairo-coroutines")
}

Arrow’s concurrency primitives are coroutine-safe, unlike the JVM-default ones.

CountDownLatch allows for awaiting a given number of countdown signals.

val latch = CountDownLatch(10)
List(10) { latch.await() }

A CyclicBarrier is a synchronization mechanism that allows a set of coroutines to wait for each other to reach a certain point before continuing execution.

val barrier = CyclicBarrier(10)
List(10) { barrier.await() }

Kotlin’s emitAll() method only accepts another Flow. Kairo’s version accepts any Iterable.

Kotlin’s single() and singleOrNull() functions are excellent utilities, but singleOrNull() can be surprising because it returns null when there are multiple items.

singleNullOrThrow() returns the single element, or null if the flow is empty. If the flow has more than one element, throws IllegalArgumentException.

kairo-util already exposes this function with Array, Iterable, and Sequence receivers. This library exposes it with the Flow receiver.

emptyFlow<Int>().singleNullOrThrow()
// => null
flowOf(1).singleNullOrThrow()
// => 1
flowOf(1, 2).singleNullOrThrow()
// => IllegalArgumentException