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 `FrpNetwork`. All reactive events and state is kept 26consistent within a single network. 27 28``` kotlin 29val coroutineScope: CoroutineScope = ... 30val frpNetwork = coroutineScope.newFrpNetwork() 31``` 32 33You can use the `FrpNetwork` to stand-up a network of reactive events and state. 34Events are modeled with `TFlow` (short for "transactional flow"), and state 35`TState` (short for "transactional state"). 36 37``` kotlin 38suspend fun activate(network: FrpNetwork) { 39 network.activateSpec { 40 val input = network.mutableTFlow<Unit>() 41 // Launch a long-running side-effect that emits to the network 42 // every second. 43 launchEffect { 44 while (true) { 45 input.emit(Unit) 46 delay(1.seconds) 47 } 48 } 49 // Accumulate state 50 val count: TState<Int> = input.fold { _, i -> i + 1 } 51 // Observe events to perform side-effects in reaction to them 52 input.observe { 53 println("Got event ${count.sample()} at time: ${System.currentTimeMillis()}") 54 } 55 } 56} 57``` 58 59`FrpNetwork.activateSpec` will suspend indefinitely; cancelling the invocation 60will tear-down all effects and obervers running within the lambda. 61 62## Resources 63 64- [Cheatsheet for those coming from Kotlin Flow](docs/flow-to-kairos-cheatsheet.md) 65