Working with State
KVision gives you sophisticated but also easy to use tools to work with application state. They are designed with the reactive paradigm and based on io.kvision.state.ObservableState and io.kvision.state.MutableState interfaces. A lot of KVision classes implement one or both of these interfaces:
ObservableValue<T>- a single value observableObservableList<T>- an observable list of elementsObservableSet<T>- an observable set of elementsReduxStore- full-featured Redux state containerall form components (e.g.
Text,TextInput,Select,SelectInput, ...)
Every component implementing ObservableState interface can notify its subscribers about state changes. You can subscribe manually by using the subscribe function:
fun subscribe(observer: (S) -> Unit): () -> Unitor you can use automatic data binding with bind function:
val observable = ObservableValue("test")
div().bind(observable) { state ->
+state
}You can bind directly your data producers (observables) with data consumers (observers):
val text = text(label = "Enter something")
div().bind(text) {
+"You entered: $it"
}All form components implement MutableState interface. At the same time every form component can be bidirectionally bound to the external MutableStateinstance (e.g. ObservableValue).
Yo can of course easily bind two form components with each other.
Flows
With additional extension functions defined in kvision-state-flow module, you can also use Kotlin Flow (including StateFlow and SharedFlow) and its operators to declare data bindings between different components and data stores.
Sub-stores
If case of a complex data store, you can easily create sub-stores, using sub() extension function. It's a way to partition your data and optimize rendering, because sub-store will notify its observers only when the specified part of the data changes.
Collections binding
You can use bindEach() function if you want do render data contained in a collection. This function uses Myers differencing algorithm to minimize number of changes to the component tree and optimize rendering.
Last updated