Remove duplicating property update notification in VisionBase

This commit is contained in:
Alexander Nozik 2021-01-13 12:38:42 +03:00
parent 4820082248
commit 19513656bd
4 changed files with 26 additions and 30 deletions

View File

@ -30,7 +30,7 @@ public val StringValueChooser: FunctionalComponent<ValueChooserProps> =
val keyDown: (Event) -> Unit = { event -> val keyDown: (Event) -> Unit = { event ->
if (event.type == "keydown" && event.asDynamic().key == "Enter") { if (event.type == "keydown" && event.asDynamic().key == "Enter") {
value = (event.target as HTMLInputElement).value value = (event.target as HTMLInputElement).value
if(value!= props.item.string) { if (value != props.item.string) {
props.valueChanged?.invoke(value.asValue()) props.valueChanged?.invoke(value.asValue())
} }
} }
@ -50,16 +50,14 @@ public val StringValueChooser: FunctionalComponent<ValueChooserProps> =
@JsExport @JsExport
public val BooleanValueChooser: FunctionalComponent<ValueChooserProps> = public val BooleanValueChooser: FunctionalComponent<ValueChooserProps> =
functionalComponent("BooleanValueChooser") { props -> functionalComponent("BooleanValueChooser") { props ->
var checkedValue by useState(props.item.boolean ?: false)
val handleChange: (Event) -> Unit = { val handleChange: (Event) -> Unit = {
val newValue = (it.target as HTMLInputElement).checked val newValue = (it.target as HTMLInputElement).checked
checkedValue = newValue
props.valueChanged?.invoke(newValue.asValue()) props.valueChanged?.invoke(newValue.asValue())
} }
styledInput(type = InputType.checkBox) { styledInput(type = InputType.checkBox) {
attrs { attrs {
this.attributes["indeterminate"] = (checkedValue == null).toString() //this.attributes["indeterminate"] = (props.item == null).toString()
checked = checkedValue defaultChecked = props.item.boolean ?: false
onChangeFunction = handleChange onChangeFunction = handleChange
} }
} }
@ -68,24 +66,24 @@ public val BooleanValueChooser: FunctionalComponent<ValueChooserProps> =
@JsExport @JsExport
public val NumberValueChooser: FunctionalComponent<ValueChooserProps> = public val NumberValueChooser: FunctionalComponent<ValueChooserProps> =
functionalComponent("NumberValueChooser") { props -> functionalComponent("NumberValueChooser") { props ->
var value by useState(props.item.string ?: "") var innerValue by useState(props.item.string ?: "")
val keyDown: (Event) -> Unit = { event -> val keyDown: (Event) -> Unit = { event ->
if (event.type == "keydown" && event.asDynamic().key == "Enter") { if (event.type == "keydown" && event.asDynamic().key == "Enter") {
value = (event.target as HTMLInputElement).value innerValue = (event.target as HTMLInputElement).value
val number = value.toDoubleOrNull() val number = innerValue.toDoubleOrNull()
if (number == null) { if (number == null) {
console.error("The input value $value is not a number") console.error("The input value $innerValue is not a number")
} else { } else {
props.valueChanged?.invoke(number.asValue()) props.valueChanged?.invoke(number.asValue())
} }
} }
} }
val handleChange: (Event) -> Unit = { val handleChange: (Event) -> Unit = {
value = (it.target as HTMLInputElement).value innerValue = (it.target as HTMLInputElement).value
} }
styledInput(type = InputType.number) { styledInput(type = InputType.number) {
attrs { attrs {
this.value = value value = innerValue
onKeyDownFunction = keyDown onKeyDownFunction = keyDown
onChangeFunction = handleChange onChangeFunction = handleChange
props.descriptor?.attributes?.get("step").string?.let { props.descriptor?.attributes?.get("step").string?.let {

View File

@ -34,16 +34,16 @@ internal data class PropertyListener(
@SerialName("vision") @SerialName("vision")
public open class VisionBase(internal var properties: Config? = null) : Vision { public open class VisionBase(internal var properties: Config? = null) : Vision {
init { // init {
//used during deserialization only // //used during deserialization only
properties?.onChange(this) { name, oldItem, newItem -> // properties?.onChange(this) { name, oldItem, newItem ->
if (oldItem != newItem) { // if (oldItem != newItem) {
scope.launch { // scope.launch {
notifyPropertyChanged(name) // notifyPropertyChanged(name)
} // }
} // }
} // }
} // }
@Transient @Transient
override var parent: VisionGroup? = null override var parent: VisionGroup? = null
@ -54,13 +54,13 @@ public open class VisionBase(internal var properties: Config? = null) : Vision {
protected fun getOrCreateConfig(): Config { protected fun getOrCreateConfig(): Config {
if (properties == null) { if (properties == null) {
val newProperties = Config() val newProperties = Config()
newProperties.onChange(this) { name, oldItem, newItem -> // newProperties.onChange(this) { name, oldItem, newItem ->
if (oldItem != newItem) { // if (oldItem != newItem) {
scope.launch { // scope.launch {
notifyPropertyChanged(name) // notifyPropertyChanged(name)
} // }
} // }
} // }
properties = newProperties properties = newProperties
} }
return properties!! return properties!!

View File

@ -42,7 +42,6 @@ public abstract class MeshThreeFactory<in T : Solid>(
//add listener to object properties //add listener to object properties
obj.onPropertyChange(three.updateScope) { name -> obj.onPropertyChange(three.updateScope) { name ->
println("Property $name of mesh ${mesh.name} updated")
when { when {
name.startsWith(Solid.GEOMETRY_KEY) -> { name.startsWith(Solid.GEOMETRY_KEY) -> {
val oldGeometry = mesh.geometry as BufferGeometry val oldGeometry = mesh.geometry as BufferGeometry

View File

@ -47,7 +47,6 @@ public object ThreeReferenceFactory : ThreeFactory<SolidReferenceGroup> {
//TODO apply child properties //TODO apply child properties
obj.onPropertyChange(three.updateScope) { name-> obj.onPropertyChange(three.updateScope) { name->
println("Property $name of reference ${object3D.name} updated")
if (name.firstOrNull()?.body == REFERENCE_CHILD_PROPERTY_PREFIX) { if (name.firstOrNull()?.body == REFERENCE_CHILD_PROPERTY_PREFIX) {
val childName = name.firstOrNull()?.index?.toName() ?: error("Wrong syntax for reference child property: '$name'") val childName = name.firstOrNull()?.index?.toName() ?: error("Wrong syntax for reference child property: '$name'")
val propertyName = name.cutFirst() val propertyName = name.cutFirst()