1# Kairos 2 3A functional reactive programming (FRP) library for Kotlin. 4 5This library is **experimental** and should not be used for general production 6code. The APIs within are subject to change, and there may be bugs. 7 8## About FRP 9 10Functional reactive programming is a type of reactive programming system that 11follows a set of clear and composable rules, without sacrificing consistency. 12FRP exposes an API that should be familiar to those versed in Kotlin `Flow`. 13 14### Details for nerds 15 16`Kairos` implements an applicative / monadic flavor of FRP, using a push-pull 17methodology to allow for efficient updates. 18 19"Real" functional reactive programming should be specified with denotational 20semantics ([wikipedia](https://en.wikipedia.org/wiki/Denotational_semantics)): 21you can view the semantics for `Kairos` [here](docs/semantics.md). 22 23## Usage 24 25First, stand up a new `KairosNetwork`. All reactive events and state is kept 26consistent within a single network. 27 28``` kotlin 29val coroutineScope: CoroutineScope = ... 30val network = coroutineScope.launchKairosNetwork() 31``` 32 33You can use the `KairosNetwork` to stand-up a network of reactive events and 34state. Events are modeled with `Events`, and states with `State`. 35 36``` kotlin 37suspend fun activate(network: KairosNetwork) { 38 network.activateSpec { 39 val input = network.mutableEvents<Unit>() 40 // Launch a long-running side-effect that emits to the network 41 // every second. 42 launchEffect { 43 while (true) { 44 input.emit(Unit) 45 delay(1.seconds) 46 } 47 } 48 // Accumulate state 49 val count: State<Int> = input.foldState { _, i -> i + 1 } 50 // Observe events to perform side-effects in reaction to them 51 input.observe { 52 println("Got event ${count.sample()} at time: ${System.currentTimeMillis()}") 53 } 54 } 55} 56``` 57 58`KairosNetwork.activateSpec` will suspend indefinitely; cancelling the invocation 59will tear-down all effects and obervers running within the lambda. 60 61## Resources 62 63- [Cheatsheet for those coming from Kotlin Flow](docs/flow-to-kairos-cheatsheet.md) 64