diff --git a/demo/sat-demo/src/jvmMain/kotlin/ru/mipt/npm/sat/satServer.kt b/demo/sat-demo/src/jvmMain/kotlin/ru/mipt/npm/sat/satServer.kt index 6ac3e3c2..eea0a4bb 100644 --- a/demo/sat-demo/src/jvmMain/kotlin/ru/mipt/npm/sat/satServer.kt +++ b/demo/sat-demo/src/jvmMain/kotlin/ru/mipt/npm/sat/satServer.kt @@ -3,12 +3,19 @@ package ru.mipt.npm.sat import hep.dataforge.context.Global import hep.dataforge.names.asName +import hep.dataforge.vision.get import hep.dataforge.vision.server.visionModule +import hep.dataforge.vision.solid.Solid import hep.dataforge.vision.solid.SolidManager +import hep.dataforge.vision.solid.color import io.ktor.server.cio.CIO import io.ktor.server.engine.embeddedServer import io.ktor.util.KtorExperimentalAPI +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import kotlinx.html.script +import kotlin.random.Random @OptIn(KtorExperimentalAPI::class) fun main() { @@ -31,5 +38,13 @@ fun main() { vision("main".asName(), sat) } } + launch { + while (isActive){ + val currentLayer = Random.nextInt(10) + (sat["layer[$currentLayer]"] as? Solid)?.color(123) + delay(300) + (sat["layer[$currentLayer]"] as? Solid)?.color = null + } + } }.start(wait = true) } \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt index e2101b0c..6fd1c559 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt @@ -97,7 +97,7 @@ public interface MutableVisionGroup : VisionGroup, VisionContainerBuilder VisionContainer.get(str: String?): V? = get(str?.toName() ?: Name.EMPTY) +public operator fun VisionContainer.get(str: String): V? = get(str.toName()) public operator fun VisionContainerBuilder.set(token: NameToken, child: V?): Unit = set(token.asName(), child) public operator fun VisionContainerBuilder.set(key: String, child: V?): Unit = set(key.toName(), child) diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt index 767f0e5a..c7fb11c2 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt @@ -15,10 +15,10 @@ public open class VisionGroupBase : VisionBase(), MutableVisionGroup { /** * Internal mutable container for group children - * TODO made protected due to https://github.com/Kotlin/kotlinx.serialization/issues/1200 + * TODO made protected due to [https://github.com/Kotlin/kotlinx.serialization/issues/1200] */ @SerialName("children") - protected open val childrenInternal: MutableMap = LinkedHashMap() + protected val childrenInternal: MutableMap = LinkedHashMap() /** * A map of top level named children diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/visionChange.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/visionChange.kt index 76bd6775..9e8b2ad8 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/visionChange.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/visionChange.kt @@ -88,15 +88,25 @@ private fun CoroutineScope.collectChange( mutex: Mutex, target: () -> MutableVisionGroup, ) { - val targetVision: () -> Vision = { target().getOrCreate(name) } //Collect properties change - source.onPropertyChange(mutex) { propertyName -> - launch { - mutex.withLock { - targetVision().setProperty(propertyName, source.getProperty(propertyName)) + source.config.onChange(mutex){propertyName, oldItem, newItem-> + if(oldItem!= newItem){ + launch { + mutex.withLock { + target().getOrCreate(name).setProperty(propertyName, newItem) + } } } } +// source.onPropertyChange(mutex) { propertyName -> +// launch { +// mutex.withLock { +// target().getOrCreate(name).setProperty(propertyName, source.getProperty(propertyName,false)) +// } +// } +// } + + val targetVision: Vision = target().getOrCreate(name) if (source is VisionGroup) { check(targetVision is MutableVisionGroup) { "Collector for a group should be a group" } @@ -124,9 +134,6 @@ private fun CoroutineScope.collectChange( @DFExperimental public fun Vision.flowChanges(scope: CoroutineScope, collectionDuration: Duration): Flow = flow { - //emit initial visual tree - emit(this@flowChanges) - val mutex = Mutex() var collector = VisionGroupBase() diff --git a/visionforge-server/src/main/kotlin/hep/dataforge/vision/server/VisionServer.kt b/visionforge-server/src/main/kotlin/hep/dataforge/vision/server/VisionServer.kt index 8b53dc90..5ea07639 100644 --- a/visionforge-server/src/main/kotlin/hep/dataforge/vision/server/VisionServer.kt +++ b/visionforge-server/src/main/kotlin/hep/dataforge/vision/server/VisionServer.kt @@ -29,6 +29,7 @@ import io.ktor.routing.get import io.ktor.routing.route import io.ktor.routing.routing import io.ktor.server.engine.ApplicationEngine +import io.ktor.util.error import io.ktor.websocket.WebSockets import io.ktor.websocket.webSocket import kotlinx.coroutines.flow.collect @@ -129,8 +130,9 @@ public class VisionServer internal constructor( val json = visionManager.encodeToString(update) outgoing.send(Frame.Text(json)) } - } catch (ex: Exception) { - application.log.debug("Closed server socket for $name with exception $ex") + } catch (ex: Throwable) { + application.log.error("Closed server socket for $name with exception $ex") + application.log.error(ex) } } //Plots in their json representation