jsCommon.io.nacular.doodle.dom.DynamicProperty.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of browser Show documentation
Show all versions of browser Show documentation
A pure Kotlin, UI framework for the Web and Desktop
package io.nacular.doodle.dom
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
internal class DynamicProperty(private val name: String, private val onError: ((Throwable) -> String)? = null): ReadWriteProperty {
override fun getValue(thisRef: T, property: KProperty<*>): String = when (onError) {
null -> (thisRef[name] as? JsString).toString()
else -> try { (thisRef[name] as? JsString).toString() } catch (throwable: Throwable) { onError.invoke(throwable) }
}
override fun setValue(thisRef: T, property: KProperty<*>, value: String) {
try {
thisRef[name] = value.toJsString()
} catch (throwable: Throwable) {
onError?.invoke(throwable)
}
}
}
internal class OptionalDynamicProperty(private val name: String, private val onError: ((Throwable) -> String?)? = null): ReadWriteProperty {
override fun getValue(thisRef: T, property: KProperty<*>): String? = when (onError) {
null -> (thisRef[name] as? JsString).toString()
else -> try { (thisRef[name] as? JsString).toString() } catch (throwable: Throwable) { onError.invoke(throwable) }
}
override fun setValue(thisRef: T, property: KProperty<*>, value: String?) {
try {
value?.toJsString()?.let {
thisRef[name] = it
}
} catch (throwable: Throwable) {
onError?.invoke(throwable)
}
}
}