packages/SystemUI/utils/kairos/README.md
A functional reactive programming (FRP) library for Kotlin.
This library is experimental and should not be used for general production code. The APIs within are subject to change, and there may be bugs.
Functional reactive programming is a type of reactive programming system that
follows a set of clear and composable rules, without sacrificing consistency.
FRP exposes an API that should be familiar to those versed in Kotlin Flow.
Kairos implements an applicative / monadic flavor of FRP, using a push-pull
methodology to allow for efficient updates.
"Real" functional reactive programming should be specified with denotational
semantics (wikipedia):
you can view the semantics for Kairos here.
First, stand up a new FrpNetwork. All reactive events and state is kept
consistent within a single network.
val coroutineScope: CoroutineScope = ...
val frpNetwork = coroutineScope.newFrpNetwork()
You can use the FrpNetwork to stand-up a network of reactive events and state.
Events are modeled with TFlow (short for "transactional flow"), and state
TState (short for "transactional state").
suspend fun activate(network: FrpNetwork) {
network.activateSpec {
val input = network.mutableTFlow<Unit>()
// Launch a long-running side-effect that emits to the network
// every second.
launchEffect {
while (true) {
input.emit(Unit)
delay(1.seconds)
}
}
// Accumulate state
val count: TState<Int> = input.fold { _, i -> i + 1 }
// Observe events to perform side-effects in reaction to them
input.observe {
println("Got event ${count.sample()} at time: ${System.currentTimeMillis()}")
}
}
}
FrpNetwork.activateSpec will suspend indefinitely; cancelling the invocation
will tear-down all effects and obervers running within the lambda.