ConditionalValue

open class ConditionalValue<T>(initValue: T)

A coroutine-based conditional waiter backed by MutableStateFlow.

Holds a value of type T and allows coroutines to suspend until the value satisfies an arbitrary predicate, with optional timeout support.

Every set notifies waiters, even when the new value equals the current one or is the same object changed in place, so each waiter re-checks its predicate. Waiters observe only the latest value: a value replaced before a waiter sees it can be missed, so wait for conditions that stay true once reached.

Parameters

initValue

the initial value.

Type Parameters

T

the type of the monitored value.

Inheritors

Constructors

Link copied to clipboard
constructor(initValue: T)

Functions

Link copied to clipboard
fun get(): T

Returns the current value.

Link copied to clipboard
fun set(value: T)

Sets the value and notifies waiting coroutines. It does not suspend, so any thread can call it.

Link copied to clipboard
suspend fun waitUntil(timeoutDuration: Duration = Duration.INFINITE, predicate: (T) -> Boolean): Boolean

Suspends until predicate holds for the current value or timeoutDuration elapses.