All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsCommon.io.nacular.doodle.dom.DynamicProperty.kt Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy