From 8a41f185debf07a2cf193e6ef721049cd55e8a5a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 5 May 2021 22:24:47 +0300 Subject: [PATCH] Remove global VisionForge object --- .../visionforge/gdml/demo/GDMLAppComponent.kt | 11 +++++------ .../visionforge/solid/demo/ThreeDemoGrid.kt | 3 ++- .../visionforge/react/ThreeCanvasComponent.kt | 6 ++++-- .../visionforge/three/server/VisionServer.kt | 15 ++++----------- .../kscience/visionforge/solid/SolidReference.kt | 2 +- .../kscience/visionforge/solid/SolidPluginTest.kt | 3 ++- .../visionforge/solid/VisionUpdateTest.kt | 3 ++- 7 files changed, 20 insertions(+), 23 deletions(-) diff --git a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt index 9d60eaf4..60e676a0 100644 --- a/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt +++ b/demo/gdml/src/jsMain/kotlin/space/kscience/visionforge/gdml/demo/GDMLAppComponent.kt @@ -4,12 +4,10 @@ import kotlinx.browser.window import kotlinx.css.* import org.w3c.files.FileReader import org.w3c.files.get -import react.RProps -import react.child +import react.* import react.dom.h1 -import react.functionalComponent -import react.useState import space.kscience.dataforge.context.Context +import space.kscience.dataforge.context.fetch import space.kscience.dataforge.names.Name import space.kscience.gdml.Gdml import space.kscience.gdml.decodeFromString @@ -51,8 +49,9 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> selected = it } + val visionManager = useMemo({ props.context.fetch(Solids).visionManager }, arrayOf(props.context)) + fun loadData(name: String, data: String) { - val visionManager = props.context.plugins.fetch(Solids).visionManager val parsedVision = when { name.endsWith(".gdml") || name.endsWith(".xml") -> { val gdml = Gdml.decodeFromString(data) @@ -70,7 +69,7 @@ val GDMLApp = functionalComponent("GDMLApp") { props -> gridRow { flexColumn { - css{ + css { +"col-lg-9" height = 100.vh } diff --git a/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt b/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt index 65dd35db..b26e30aa 100644 --- a/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt +++ b/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/ThreeDemoGrid.kt @@ -10,6 +10,7 @@ import org.w3c.dom.Element import org.w3c.dom.HTMLDivElement import org.w3c.dom.HTMLElement import space.kscience.dataforge.context.Global +import space.kscience.dataforge.context.fetch import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.string @@ -25,7 +26,7 @@ class ThreeDemoGrid(element: Element) : VisionLayout { private val outputs: MutableMap = HashMap() - private val three = Global.plugins.fetch(ThreePlugin) + private val three = Global.fetch(ThreePlugin) init { element.clear() diff --git a/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt b/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt index 8a3758c8..74a1a61d 100644 --- a/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt +++ b/ui/react/src/main/kotlin/space/kscience/visionforge/react/ThreeCanvasComponent.kt @@ -6,6 +6,7 @@ import org.w3c.dom.Element import org.w3c.dom.HTMLElement import react.* import space.kscience.dataforge.context.Context +import space.kscience.dataforge.context.fetch import space.kscience.dataforge.names.Name import space.kscience.visionforge.solid.Solid import space.kscience.visionforge.solid.specifications.Canvas3DOptions @@ -33,10 +34,11 @@ public val ThreeCanvasComponent: FunctionalComponent = functio val elementRef = useRef(null) var canvas by useState(null) - useEffect(listOf(props.context, props.obj, props.options, elementRef)) { + val three: ThreePlugin = useMemo({props.context.fetch(ThreePlugin)}, arrayOf(props.context)) + + useEffect(listOf(props.obj, props.options, elementRef)) { if (canvas == null) { val element = elementRef.current as? HTMLElement ?: error("Canvas element not found") - val three: ThreePlugin = props.context.plugins.fetch(ThreePlugin) val newCanvas: ThreeCanvas = three.createCanvas(element, props.options ?: Canvas3DOptions.empty()) props.canvasCallback?.invoke(newCanvas) canvas = newCanvas diff --git a/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt b/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt index 52e37fc1..c52a84a1 100644 --- a/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt +++ b/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt @@ -25,6 +25,7 @@ import kotlinx.coroutines.withContext import kotlinx.html.* import kotlinx.html.stream.createHTML import space.kscience.dataforge.context.Context +import space.kscience.dataforge.context.fetch import space.kscience.dataforge.meta.* import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.Name @@ -40,16 +41,8 @@ import space.kscience.visionforge.html.fragment import space.kscience.visionforge.three.server.VisionServer.Companion.DEFAULT_PAGE import java.awt.Desktop import java.net.URI -import kotlin.collections.ArrayList -import kotlin.collections.HashMap -import kotlin.collections.List -import kotlin.collections.Map -import kotlin.collections.emptyList -import kotlin.collections.first -import kotlin.collections.forEach -import kotlin.collections.plus import kotlin.collections.set -import kotlin.time.milliseconds +import kotlin.time.Duration public enum class VisionServerDataMode { EMBED, @@ -144,7 +137,7 @@ public class VisionServer internal constructor( try { withContext(visionManager.context.coroutineContext) { - vision.flowChanges(visionManager, updateInterval.milliseconds).collect { update -> + vision.flowChanges(visionManager, Duration.milliseconds(updateInterval)).collect { update -> val json = visionManager.jsonFormat.encodeToString( VisionChange.serializer(), update @@ -277,7 +270,7 @@ public fun Application.visionServer(context: Context, route: String = DEFAULT_PA install(CallLogging) } - val visionManager = context.plugins.fetch(VisionManager) + val visionManager = context.fetch(VisionManager) routing { route(route) { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt index 6379e51f..acb0ce88 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/SolidReference.kt @@ -126,7 +126,7 @@ public class SolidReferenceGroup( val parentName = childName.cutLast() return if (parentName.isEmpty()) this@SolidReferenceGroup else ReferenceChild(parentName) } - set(value) { + set(_) { error("Setting a parent for a reference child is not possible") } diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPluginTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPluginTest.kt index fe8c4ad8..23a48a63 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPluginTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/SolidPluginTest.kt @@ -1,6 +1,7 @@ package space.kscience.visionforge.solid import space.kscience.dataforge.context.Global +import space.kscience.dataforge.context.fetch import space.kscience.dataforge.misc.DFExperimental import space.kscience.visionforge.get import kotlin.test.Test @@ -18,7 +19,7 @@ class SolidPluginTest { @DFExperimental @Test fun testPluginConverter() { - val visionManager = Global.plugins.fetch(Solids).visionManager + val visionManager = Global.fetch(Solids).visionManager val meta = visionManager.encodeToMeta(vision) val reconstructed = visionManager.decodeFromMeta(meta) as SolidGroup diff --git a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt index a96c7d49..2e87adf8 100644 --- a/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt +++ b/visionforge-solid/src/commonTest/kotlin/space/kscience/visionforge/solid/VisionUpdateTest.kt @@ -1,6 +1,7 @@ package space.kscience.visionforge.solid import space.kscience.dataforge.context.Global +import space.kscience.dataforge.context.fetch import space.kscience.dataforge.meta.MetaItem import space.kscience.dataforge.names.toName import space.kscience.visionforge.VisionChange @@ -10,7 +11,7 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class VisionUpdateTest { - val solidManager = Global.plugins.fetch(Solids) + val solidManager = Global.fetch(Solids) val visionManager = solidManager.visionManager @Test