Fix changesFlow issues

This commit is contained in:
Alexander Nozik 2020-12-06 21:14:28 +03:00
parent face7bfd0c
commit 8a4779e9c4
5 changed files with 37 additions and 13 deletions

View File

@ -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)
}

View File

@ -97,7 +97,7 @@ public interface MutableVisionGroup : VisionGroup, VisionContainerBuilder<Vision
// public operator fun set(name: Name, child: Vision?)
}
public operator fun <V: Vision> VisionContainer<V>.get(str: String?): V? = get(str?.toName() ?: Name.EMPTY)
public operator fun <V: Vision> VisionContainer<V>.get(str: String): V? = get(str.toName())
public operator fun <V: Vision> VisionContainerBuilder<V>.set(token: NameToken, child: V?): Unit = set(token.asName(), child)
public operator fun <V: Vision> VisionContainerBuilder<V>.set(key: String, child: V?): Unit = set(key.toName(), child)

View File

@ -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<NameToken, Vision> = LinkedHashMap()
protected val childrenInternal: MutableMap<NameToken, Vision> = LinkedHashMap()
/**
* A map of top level named children

View File

@ -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 ->
source.config.onChange(mutex){propertyName, oldItem, newItem->
if(oldItem!= newItem){
launch {
mutex.withLock {
targetVision().setProperty(propertyName, source.getProperty(propertyName))
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<Vision> = flow {
//emit initial visual tree
emit(this@flowChanges)
val mutex = Mutex()
var collector = VisionGroupBase()

View File

@ -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