revert chagnes to client change collector

This commit is contained in:
Alexander Nozik 2023-12-05 17:43:53 +03:00
parent 595512959c
commit 9fc6f1e34c
2 changed files with 33 additions and 41 deletions

View File

@ -2,8 +2,10 @@ package space.kscience.visionforge
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import space.kscience.dataforge.context.Plugin import space.kscience.dataforge.context.Plugin
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.MetaRepr import space.kscience.dataforge.meta.MetaRepr
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.parseAsName
/** /**
* A feedback client that communicates with a server and provides ability to propagate events and changes back to the model * A feedback client that communicates with a server and provides ability to propagate events and changes back to the model
@ -13,25 +15,24 @@ public interface VisionClient: Plugin {
public suspend fun sendEvent(targetName: Name, event: VisionEvent) public suspend fun sendEvent(targetName: Name, event: VisionEvent)
// public fun notifyPropertyChanged(visionName: Name, propertyName: Name, item: Meta?) public fun notifyPropertyChanged(visionName: Name, propertyName: Name, item: Meta?)
} }
public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: Meta?) {
notifyPropertyChanged(visionName, propertyName.parseAsName(true), item)
}
//public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: Meta?) { public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: Number) {
// notifyPropertyChanged(visionName, propertyName.parseAsName(true), item) notifyPropertyChanged(visionName, propertyName.parseAsName(true), Meta(item))
//} }
//
//public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: Number) { public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: String) {
// notifyPropertyChanged(visionName, propertyName.parseAsName(true), Meta(item)) notifyPropertyChanged(visionName, propertyName.parseAsName(true), Meta(item))
//} }
//
//public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: String) { public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: Boolean) {
// notifyPropertyChanged(visionName, propertyName.parseAsName(true), Meta(item)) notifyPropertyChanged(visionName, propertyName.parseAsName(true), Meta(item))
//} }
//
//public fun VisionClient.notifyPropertyChanged(visionName: Name, propertyName: String, item: Boolean) {
// notifyPropertyChanged(visionName, propertyName.parseAsName(true), Meta(item))
//}
public fun VisionClient.sendEvent(targetName: Name, payload: MetaRepr): Unit { public fun VisionClient.sendEvent(targetName: Name, payload: MetaRepr): Unit {
context.launch { context.launch {

View File

@ -65,20 +65,21 @@ public class JsVisionClient : AbstractPlugin(), VisionClient {
private fun Element.getFlag(attribute: String): Boolean = attributes[attribute]?.value != null private fun Element.getFlag(attribute: String): Boolean = attributes[attribute]?.value != null
// private val mutex = Mutex() private val mutex = Mutex()
private val rootChangeCollector = VisionChangeBuilder()
// /** /**
// * Communicate vision property changed from rendering engine to model * Communicate vision property changed from rendering engine to model
// */ */
// private fun notifyPropertyChanged(visionName: Name, propertyName: Name, item: Meta?) { override fun notifyPropertyChanged(visionName: Name, propertyName: Name, item: Meta?) {
// context.launch { context.launch {
// mutex.withLock { mutex.withLock {
// changeCollector.propertyChanged(visionName, propertyName, item) rootChangeCollector.propertyChanged(visionName, propertyName, item)
// } }
// } }
// } }
private val eventCollector by lazy { private val eventCollector by lazy {
MutableSharedFlow<Pair<Name, VisionEvent>>(meta["feedback.eventCache"].int ?: 100) MutableSharedFlow<Pair<Name, VisionEvent>>(meta["feedback.eventCache"].int ?: 100)
@ -139,7 +140,6 @@ public class JsVisionClient : AbstractPlugin(), VisionClient {
} }
} }
//Backward change propagation //Backward change propagation
var feedbackJob: Job? = null var feedbackJob: Job? = null
@ -148,29 +148,20 @@ public class JsVisionClient : AbstractPlugin(), VisionClient {
onopen = { onopen = {
val mutex = Mutex()
val changeCollector = VisionChangeBuilder()
feedbackJob = visionManager.context.launch { feedbackJob = visionManager.context.launch {
//launch a separate coroutine to send events to the backend //launch a separate coroutine to send events to the backend
eventCollector.filter { it.first == visionName }.onEach { eventCollector.filter { it.first == visionName }.onEach {
send(visionManager.jsonFormat.encodeToString(VisionEvent.serializer(), it.second)) send(visionManager.jsonFormat.encodeToString(VisionEvent.serializer(), it.second))
}.launchIn(this) }.launchIn(this)
//launch backward property propagation
vision.properties.changes.onEach { propertyName: Name ->
changeCollector.propertyChanged(visionName, propertyName, vision.properties[propertyName])
}.launchIn(this)
//aggregate atomic changes //aggregate atomic changes
while (isActive) { while (isActive) {
delay(feedbackAggregationTime.milliseconds) delay(feedbackAggregationTime.milliseconds)
if (!changeCollector.isEmpty()) { val visionChangeCollector = rootChangeCollector[name]
if (visionChangeCollector?.isEmpty() == false) {
mutex.withLock { mutex.withLock {
eventCollector.emit(visionName to changeCollector.deepCopy(visionManager)) eventCollector.emit(visionName to visionChangeCollector.deepCopy(visionManager))
changeCollector.reset() rootChangeCollector.reset()
} }
} }
} }