Clean up, release v. 1.0.0
This commit is contained in:
35
src/jsMain/kotlin/nl/astraeus/komp/State.kt
Normal file
35
src/jsMain/kotlin/nl/astraeus/komp/State.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package nl.astraeus.komp
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class StateDelegate<T>(
|
||||
val komponent: Komponent,
|
||||
initialValue: T
|
||||
) {
|
||||
var value: T = initialValue
|
||||
|
||||
operator fun getValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>
|
||||
): T {
|
||||
return value
|
||||
}
|
||||
|
||||
operator fun setValue(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>,
|
||||
value: T
|
||||
) {
|
||||
if (this.value?.equals(value) != true) {
|
||||
this.value = value
|
||||
komponent.requestUpdate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> Komponent.state(
|
||||
initialValue: T
|
||||
): StateDelegate<T> = StateDelegate(
|
||||
this,
|
||||
initialValue
|
||||
)
|
||||
Reference in New Issue
Block a user