diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionContainer.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionContainer.kt index 975e170a..7ac01b74 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionContainer.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionContainer.kt @@ -24,7 +24,7 @@ public interface MutableVisionContainer { * A serializable representation of [Vision] children container */ public interface VisionChildren : VisionContainer { - public val group: Vision? + public val parent: Vision? public val keys: Set @@ -35,7 +35,7 @@ public interface VisionChildren : VisionContainer { public operator fun get(token: NameToken): Vision? override fun getChild(name: Name): Vision? = when (name.length) { - 0 -> group + 0 -> parent 1 -> get(name.first()) else -> get(name.first())?.children?.getChild(name.cutFirst()) } @@ -44,7 +44,7 @@ public interface VisionChildren : VisionContainer { public const val STATIC_TOKEN_BODY: String = "@static" public fun empty(owner: Vision): VisionChildren = object : VisionChildren { - override val group: Vision get() = owner + override val parent: Vision get() = owner override val keys: Set get() = emptySet() override val changes: Flow get() = emptyFlow() override fun get(token: NameToken): Vision? = null @@ -64,7 +64,7 @@ public inline fun VisionChildren.forEach(block: (NameToken, Vision) -> Unit) { public interface MutableVisionChildren : VisionChildren, MutableVisionContainer { - public override val group: MutableVisionGroup + public override val parent: MutableVisionGroup public operator fun set(token: NameToken, value: Vision?) @@ -85,7 +85,7 @@ public interface MutableVisionChildren : VisionChildren, MutableVisionContainer< else -> { val currentParent = get(name.first()) if (currentParent != null && currentParent !is MutableVisionGroup) error("Can't assign a child to $currentParent") - val parent: MutableVisionGroup = currentParent as? MutableVisionGroup ?: group.createGroup().also { + val parent: MutableVisionGroup = currentParent as? MutableVisionGroup ?: parent.createGroup().also { set(name.first(), it) } parent.children.setChild(name.cutFirst(), child) @@ -125,7 +125,7 @@ public fun MutableVisionContainer.setChild( ): Unit = setChild(str?.parseAsName(), vision) internal abstract class VisionChildrenImpl( - override val group: MutableVisionGroup, + override val parent: MutableVisionGroup, ) : MutableVisionChildren { private val updateJobs = HashMap() @@ -140,7 +140,7 @@ internal abstract class VisionChildrenImpl( return items!! } - private val scope: CoroutineScope? get() = group.manager?.context + private val scope: CoroutineScope? get() = parent.manager?.context override val keys: Set get() = items?.keys ?: emptySet() @@ -170,9 +170,9 @@ internal abstract class VisionChildrenImpl( } else { (items ?: buildItems())[token] = value //check if parent already exists and is different from the current one - if (value.parent != null && value.parent != group) error("Can't reassign parent Vision for $value") + if (value.parent != null && value.parent != parent) error("Can't reassign parent Vision for $value") //set parent - value.parent = group + value.parent = parent //start update jobs (only if the vision is rooted) scope?.let { scope -> val job = value.children?.changes?.onEach { diff --git a/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt b/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt index a4bfa7b9..d806c278 100644 --- a/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt +++ b/visionforge-server/src/main/kotlin/space/kscience/visionforge/server/VisionServer.kt @@ -1,10 +1,7 @@ package space.kscience.visionforge.server import io.ktor.http.* -import io.ktor.server.application.Application -import io.ktor.server.application.call -import io.ktor.server.application.install -import io.ktor.server.application.log +import io.ktor.server.application.* import io.ktor.server.cio.CIO import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.embeddedServer @@ -18,6 +15,7 @@ import io.ktor.server.routing.* import io.ktor.server.util.getOrFail import io.ktor.server.websocket.WebSockets import io.ktor.server.websocket.webSocket +import io.ktor.util.pipeline.Pipeline import io.ktor.websocket.Frame import kotlinx.coroutines.channels.consumeEach import kotlinx.coroutines.flow.collect @@ -270,6 +268,11 @@ public class VisionServer internal constructor( } } +public fun

, B : Any, F : Any> P.require( + plugin: Plugin, + configure: B.() -> Unit = {}, +): F = pluginOrNull(plugin) ?: install(plugin, configure) + /** * Attach VisionForge server application to given server */ @@ -278,8 +281,8 @@ public fun Application.visionServer( webServerUrl: Url, path: String = DEFAULT_PAGE, ): VisionServer { - install(WebSockets) - install(CORS) { + require(WebSockets) + require(CORS) { anyHost() } @@ -287,7 +290,7 @@ public fun Application.visionServer( // install(CallLogging) // } - val serverRoute = install(Routing).createRouteFromPath(path) + val serverRoute = require(Routing).createRouteFromPath(path) serverRoute { static { 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 b54e3ab6..80db0b3e 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 @@ -124,7 +124,7 @@ public class SolidReference( override val children: VisionChildren get() = object : VisionChildren { - override val group: Vision get() = this@SolidReference + override val parent: Vision get() = this@SolidReference override val keys: Set get() = prototype.children?.keys ?: emptySet() @@ -198,7 +198,7 @@ internal class SolidReferenceChild( override val children: VisionChildren = object : VisionChildren { - override val group: Vision get() = this@SolidReferenceChild + override val parent: Vision get() = this@SolidReferenceChild override val keys: Set get() = prototype.children?.keys ?: emptySet()