Remove global VisionForge object

This commit is contained in:
Alexander Nozik 2021-05-05 22:24:47 +03:00
parent 0961ae90b7
commit 8a41f185de
7 changed files with 20 additions and 23 deletions

View File

@ -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<GDMLAppProps>("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<GDMLAppProps>("GDMLApp") { props ->
gridRow {
flexColumn {
css{
css {
+"col-lg-9"
height = 100.vh
}

View File

@ -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<Solid> {
private val outputs: MutableMap<Name, ThreeCanvas> = HashMap()
private val three = Global.plugins.fetch(ThreePlugin)
private val three = Global.fetch(ThreePlugin)
init {
element.clear()

View File

@ -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<ThreeCanvasProps> = functio
val elementRef = useRef<Element?>(null)
var canvas by useState<ThreeCanvas?>(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

View File

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

View File

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

View File

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

View File

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