blob: 5174c45a8d82fc1df5e6bed9f6865d18485b7dd4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# Kairos
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.
## About FRP
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`.
### Details for nerds
`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](https://en.wikipedia.org/wiki/Denotational_semantics)):
you can view the semantics for `Kairos` [here](docs/semantics.md).
## Usage
First, stand up a new `KairosNetwork`. All reactive events and state is kept
consistent within a single network.
``` kotlin
val coroutineScope: CoroutineScope = ...
val network = coroutineScope.launchKairosNetwork()
```
You can use the `KairosNetwork` to stand-up a network of reactive events and
state. Events are modeled with `Events`, and states with `State`.
``` kotlin
suspend fun activate(network: KairosNetwork) {
network.activateSpec {
val input = network.mutableEvents<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: State<Int> = input.foldState { _, i -> i + 1 }
// Observe events to perform side-effects in reaction to them
input.observe {
println("Got event ${count.sample()} at time: ${System.currentTimeMillis()}")
}
}
}
```
`KairosNetwork.activateSpec` will suspend indefinitely; cancelling the invocation
will tear-down all effects and obervers running within the lambda.
## Resources
- [Cheatsheet for those coming from Kotlin Flow](docs/flow-to-kairos-cheatsheet.md)
|