From eefc036dcb7b3174eef304b7feaf98f5c8f1674a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 21 Dec 2020 09:49:42 +0300 Subject: [PATCH] Remove `attachChildren`. Move group children to constructor --- .../kotlin/hep/dataforge/vision/Vision.kt | 1 + .../hep/dataforge/vision/VisionChange.kt | 7 +---- .../hep/dataforge/vision/VisionGroup.kt | 10 ------- .../hep/dataforge/vision/VisionGroupBase.kt | 19 ++++++++------ .../hep/dataforge/vision/VisionManager.kt | 9 ++----- .../hep/dataforge/vision/solid/SolidGroup.kt | 26 ++++--------------- .../dataforge/vision/solid/SolidManager.kt | 11 ++++---- .../dataforge/vision/solid/SolidReference.kt | 8 ------ .../dataforge/vision/solid/transform/UnRef.kt | 4 ++- 9 files changed, 28 insertions(+), 67 deletions(-) diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/Vision.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/Vision.kt index ba585b46..eda9e70b 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/Vision.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/Vision.kt @@ -62,6 +62,7 @@ public interface Vision : Described { * Flow of property invalidation events. It does not contain property values after invalidation since it is not clear * if it should include inherited properties etc. */ + @OptIn(ExperimentalCoroutinesApi::class) public val propertyChanges: Flow get() = callbackFlow { coroutineScope { onPropertyChange(this) { diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionChange.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionChange.kt index 7dddd655..cbff844c 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionChange.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionChange.kt @@ -66,12 +66,7 @@ public class VisionChange( public val vision: Vision? = null, @Serializable(MetaSerializer::class) public val properties: Meta? = null, public val children: Map? = null, -) { - init { - (vision as? VisionGroup)?.attachChildren() - } - -} +) public inline fun VisionChange(manager: VisionManager, block: VisionChangeBuilder.() -> Unit): VisionChange = VisionChangeBuilder().apply(block).isolate(manager) diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt index 75855eb1..0f3967af 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroup.kt @@ -45,16 +45,6 @@ public interface VisionGroup : Provider, Vision, VisionContainer { } } - /** - * A fix for serialization bug that writes all proper parents inside the tree after deserialization - */ - public fun attachChildren() { - children.values.forEach { - it.parent = this - (it as? VisionGroup)?.attachChildren() - } - } - public companion object { public const val STYLE_TARGET: String = "style" } diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt index 0752b98b..6cc2b97b 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionGroupBase.kt @@ -11,23 +11,26 @@ import kotlinx.serialization.Transient /** * Abstract implementation of mutable group of [Vision] + * + * @param childrenInternal Internal mutable container for group children */ @Serializable @SerialName("vision.group") -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] - */ - @SerialName("children") - protected val childrenInternal: MutableMap = LinkedHashMap() +public open class VisionGroupBase( + @SerialName("children") internal val childrenInternal: MutableMap = LinkedHashMap(), +) : VisionBase(), MutableVisionGroup { /** * A map of top level named children */ override val children: Map get() = childrenInternal + init { + childrenInternal.values.forEach { + it.parent = this + } + } + override suspend fun notifyPropertyChanged(propertyName: Name) { super.notifyPropertyChanged(propertyName) for (obj in this) { diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionManager.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionManager.kt index eb34ed84..9a831799 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionManager.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionManager.kt @@ -30,16 +30,11 @@ public class VisionManager(meta: Meta) : AbstractPlugin(meta) { serializersModule = this@VisionManager.serializersModule } - public fun decodeFromString(string: String): Vision = jsonFormat.decodeFromString(visionSerializer, string).also { - (it as? VisionGroup)?.attachChildren() - } + public fun decodeFromString(string: String): Vision = jsonFormat.decodeFromString(visionSerializer, string) public fun encodeToString(vision: Vision): String = jsonFormat.encodeToString(visionSerializer, vision) - public fun decodeFromJson(json: JsonElement): Vision = - jsonFormat.decodeFromJsonElement(visionSerializer, json).also { - (it as? VisionGroup)?.attachChildren() - } + public fun decodeFromJson(json: JsonElement): Vision = jsonFormat.decodeFromJsonElement(visionSerializer, json) public fun encodeToJsonElement(vision: Vision): JsonElement = jsonFormat.encodeToJsonElement(visionSerializer, vision) diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt index c9d1654a..e2abc4e8 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidGroup.kt @@ -23,21 +23,17 @@ public interface PrototypeHolder { /** * Represents 3-dimensional Visual Group + * @param prototypes A container for templates visible inside this group */ @Serializable @SerialName("group.solid") -public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { +public class SolidGroup( + @Serializable(Prototypes.Companion::class) @SerialName("prototypes") private var prototypes: MutableVisionGroup? = null, +) : VisionGroupBase(), Solid, PrototypeHolder { override val descriptor: NodeDescriptor get() = Solid.descriptor - /** - * A container for templates visible inside this group - */ - @Serializable(Prototypes.Companion::class) - @SerialName("prototypes") - internal var prototypes: MutableVisionGroup? = null - /** * Ger a prototype redirecting the request to the parent if prototype is not found */ @@ -60,13 +56,6 @@ public class SolidGroup : VisionGroupBase(), Solid, PrototypeHolder { override var scale: Point3D? = null - override fun attachChildren() { - prototypes?.parent = this - prototypes?.attachChildren() - super.attachChildren() - } - - // /** // * TODO add special static group to hold statics without propagation // */ @@ -108,16 +97,11 @@ public fun VisionContainerBuilder.group(name: String, action: SolidGroup @Serializable(Prototypes.Companion::class) internal class Prototypes( children: Map = emptyMap(), -) : VisionGroupBase(), PrototypeHolder { +) : VisionGroupBase(children as? MutableMap ?: children.toMutableMap()), PrototypeHolder { init { - childrenInternal.putAll(children) - } - - override fun attachChildren() { children.values.forEach { it.parent = parent - (it as? VisionGroup)?.attachChildren() } } diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt index 5f3c59bc..fa67fe5c 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidManager.kt @@ -7,7 +7,10 @@ import hep.dataforge.context.PluginTag import hep.dataforge.meta.Meta import hep.dataforge.names.Name import hep.dataforge.names.toName -import hep.dataforge.vision.* +import hep.dataforge.vision.Vision +import hep.dataforge.vision.VisionBase +import hep.dataforge.vision.VisionGroupBase +import hep.dataforge.vision.VisionManager import hep.dataforge.vision.VisionManager.Companion.VISION_SERIALIZER_MODULE_TARGET import kotlinx.serialization.PolymorphicSerializer import kotlinx.serialization.json.Json @@ -67,10 +70,6 @@ public class SolidManager(meta: Meta) : AbstractPlugin(meta) { public fun encodeToString(solid: Solid): String = jsonForSolids.encodeToString(PolymorphicSerializer(Vision::class), solid) - fun decodeFromString(str: String): Solid = jsonForSolids.decodeFromString(PolymorphicSerializer(Solid::class), str).also { - if(it is VisionGroup){ - it.attachChildren() - } - } + public fun decodeFromString(str: String): Solid = jsonForSolids.decodeFromString(PolymorphicSerializer(Solid::class), str) } } diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt index 6430fc3d..63cd0672 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt @@ -82,10 +82,6 @@ public class SolidReferenceGroup( includeDefaults: Boolean, ): MetaItem<*>? = getRefProperty(name, inherit, includeStyles, includeDefaults) - override fun attachChildren() { - //do nothing - } - override val descriptor: NodeDescriptor get() = prototype.descriptor @@ -142,10 +138,6 @@ public class SolidReferenceGroup( TODO("Not yet implemented") } - override fun attachChildren() { - //do nothing - } - override val descriptor: NodeDescriptor get() = prototype.descriptor } diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/UnRef.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/UnRef.kt index cb0ac4e2..8bc81ff1 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/UnRef.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/transform/UnRef.kt @@ -26,7 +26,9 @@ internal object UnRef : VisualTreeTransform() { } private fun MutableVisionGroup.unref(name: Name) { - (this as? SolidGroup)?.prototypes?.set(name, null) + (this as? SolidGroup)?.prototypes{ + set(name, null) + } children.filter { (it.value as? SolidReferenceGroup)?.templateName == name }.forEach { (key, value) -> val reference = value as SolidReferenceGroup val newChild = mergeChild(reference, reference.prototype)