Kairo Coroutines
kairo-coroutines extends Kotlin’s coroutine library.
- Provides Arrow’s coroutines library,
which includes coroutine-safe concurrency primitives like
CountDownLatchandCyclicBarrier. - Adds some convenient helper functions.
Installation
Section titled “Installation”Install kairo-coroutines.
You don’t need to install Kotlin or Arrow’s coroutines libraries separately —
they’re included by default.
dependencies { implementation("software.airborne.kairo:kairo-coroutines")}Arrow’s concurrency primitives
Section titled “Arrow’s concurrency primitives”Arrow’s concurrency primitives are coroutine-safe, unlike the JVM-default ones.
CountDownLatch
Section titled “CountDownLatch”CountDownLatch allows for awaiting a given number of countdown signals.
val latch = CountDownLatch(10)List(10) { latch.await() }CyclicBarrier
Section titled “CyclicBarrier”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() }Helper functions
Section titled “Helper functions”emitAll()
Section titled “emitAll()”Kotlin’s emitAll() method only accepts another Flow.
Kairo’s version accepts any Iterable.
singleNullOrThrow()
Section titled “singleNullOrThrow()”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