Remove attachChildren. Move group children to constructor

This commit is contained in:
Alexander Nozik 2020-12-21 09:49:42 +03:00
parent 0259d4eb15
commit eefc036dcb
9 changed files with 28 additions and 67 deletions

View File

@ -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<Name> get() = callbackFlow<Name> {
coroutineScope {
onPropertyChange(this) {

View File

@ -66,12 +66,7 @@ public class VisionChange(
public val vision: Vision? = null,
@Serializable(MetaSerializer::class) public val properties: Meta? = null,
public val children: Map<Name, VisionChange>? = null,
) {
init {
(vision as? VisionGroup)?.attachChildren()
}
}
)
public inline fun VisionChange(manager: VisionManager, block: VisionChangeBuilder.() -> Unit): VisionChange =
VisionChangeBuilder().apply(block).isolate(manager)

View File

@ -45,16 +45,6 @@ public interface VisionGroup : Provider, Vision, VisionContainer<Vision> {
}
}
/**
* 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"
}

View File

@ -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<NameToken, Vision> = LinkedHashMap()
public open class VisionGroupBase(
@SerialName("children") internal val childrenInternal: MutableMap<NameToken, Vision> = LinkedHashMap(),
) : VisionBase(), MutableVisionGroup {
/**
* A map of top level named children
*/
override val children: Map<NameToken, Vision> get() = childrenInternal
init {
childrenInternal.values.forEach {
it.parent = this
}
}
override suspend fun notifyPropertyChanged(propertyName: Name) {
super.notifyPropertyChanged(propertyName)
for (obj in this) {

View File

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

View File

@ -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<Vision>.group(name: String, action: SolidGroup
@Serializable(Prototypes.Companion::class)
internal class Prototypes(
children: Map<NameToken, Vision> = emptyMap(),
) : VisionGroupBase(), PrototypeHolder {
) : VisionGroupBase(children as? MutableMap<NameToken, Vision> ?: children.toMutableMap()), PrototypeHolder {
init {
childrenInternal.putAll(children)
}
override fun attachChildren() {
children.values.forEach {
it.parent = parent
(it as? VisionGroup)?.attachChildren()
}
}

View File

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

View File

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

View File

@ -26,7 +26,9 @@ internal object UnRef : VisualTreeTransform<SolidGroup>() {
}
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)