KVar

class KVar<T>(initialValue: T) : KVal<T> (source)

A KVar is an observable container for a value of type T. It must be initialized with initialValue, and this can then be modified by setting the KVar.value property. Listeners may be added using KVar.addListener, and these will be called whenever the value is changed.

From within Kweb's DSL, you can use the ElementCreator.kvar function to create a KVar without needing to import KVar, and which will also call KVar.close when this part of the DOM is cleaned up.

Constructors

Link copied to clipboard
constructor(initialValue: T)

Properties

Link copied to clipboard
val KVar<URL>.path: KVar<String>
Link copied to clipboard

Given the URI specification:

Link copied to clipboard
Link copied to clipboard
val KVar<URL>.query: KVar<String?>
Link copied to clipboard
open override var value: T

The current value of this KVar. Setting this property to a different value will notify all listeners, but if the new value is the same as the old value then it will be ignored.

Functions

Link copied to clipboard
fun addListener(listener: (T, T) -> Unit): Long

Add a listener to this KVar. The listener will be called whenever the value property changes.

Link copied to clipboard
infix fun KVar<String>.attr(s: String): KVal<String>
Link copied to clipboard
open override fun close()

fun close(reason: CloseReason)

Close this KVal, and notify all handlers that it has been closed.

Link copied to clipboard
operator fun <T : Any> KVar<List<T>>.get(pos: Int): KVar<T>

KVar extensions

operator fun <K : Any, V : Any> KVar<Map<K, V>>.get(k: K): KVar<V?>
Link copied to clipboard
fun <O> map(mapper: (T) -> O): KVal<O>

Create another KVal that is a mapping of this KVal. The mapping function will be called whenever this KVal changes, and the new KVal will be updated with the result of the mapping function.

fun <O> map(reversibleFunction: ReversibleFunction<T, O>): KVar<O>

Create another KVar that is a bi-directional mapping of this KVar. ReversibleFunction.invoke will be called whenever this KVar changes, and the new KVar will be updated with the result of this mapping function.

Link copied to clipboard
fun <O : Any> KVar<O?>.notNull(default: O? = null, invertDefault: Boolean = true): KVar<O>

Bi-directionally map a KVar with nullable type to its non-nullable equivalent.

Link copied to clipboard
fun onClose(handler: () -> Unit)

Add a handler to be called when this KVal is closed.

Link copied to clipboard
operator fun <O : Any> KVal<List<O>>.plus(other: KVal<List<O>>): KVal<List<O>>
Link copied to clipboard
inline fun <O, T> KVar<T>.property(property: KProperty1<T, O>): KVar<O>

Use reflection to create a KVar that bi-directionally maps to a mutable property of an object.

Link copied to clipboard
fun removeListener(handle: Long)

Remove a listener from this KVar. The listener will no longer be called when the value property changes.

Link copied to clipboard
fun <T : Any> KVal<List<T>>.subList(fromIx: Int, toIx: Int): KVal<List<T>>
fun <T : Any> KVar<List<T>>.subList(fromIx: Int, toIx: Int): KVar<List<T>>
Link copied to clipboard
Link copied to clipboard
open override fun toString(): String