ReaKt
INFO
ReaKt is currently in beta
and may have minor API changes.
ReaKt is a reactivity library for Kotlin based off of SolidJS. It currently includes Signal
s, Memo
s, Effect
s, and can be extended on very easily with ReactiveContext
s and EventPublisher
s.
Installing
The repository can be found here
Dependency
- Group: net.bladehunt
- Artifact ID: reakt
- Published versions can be found here
Signals
Signals are simple data stores that publish events to its subscribers upon updates. They are automatically subscribed to by ReactiveContext
s.
Usage:
kotlin
// Signal Delegation
val signal = Signal(0)
Effect {
val value by signal
println(value) // 0, 1,...5
}
var value by signal
for (i in 1..5) {
value = i
}
// Invoking
val signal = Signal(0)
Effect {
println(signal()) // 0, 1,...5
}
for (i in 1..5) {
signal.value = i
}
Effects
Effects are subscribers that execute the function after receiving events. Effects are run once when initialized in order to subscribe to any publishers in the block.
Usage:
kotlin
var signal = Signal(0)
Effect {
println(signal()) // Subscription happens here
} // This block will be re-run every time the signal gets updated
for (i in 1..5) {
signal.value = i
}
Memos
Memos are transformers that take inputs from publishers and produce an output.
Usage:
kotlin
var signal = Signal(0)
val memo = Memo {
signal() + 1
} // This block will be re-run every time the signal gets updated, producing a new output
Effect {
println(memo()) // 1, 2, 3, 4, 5, 6
}
for (i in 1..5) {
signal.value = i
}