minor refactoring

This commit is contained in:
Alexander Nozik 2024-02-18 18:34:26 +03:00
parent 4197b4bb61
commit e850ca4145
8 changed files with 72 additions and 20 deletions

View File

@ -37,21 +37,23 @@ public abstract class AbstractAction<T : Any, R : Any>(
* Update part of the data set using provided data
*
* @param source the source data tree in case we need several data items to update
* @param meta the metadata used for the whole data tree
* @param updatedData an updated item
*/
protected open fun DataSink<R>.update(
source: DataTree<T>,
meta: Meta,
namedData: NamedData<T>,
){
updatedData: NamedData<T>,
) {
//by default regenerate the whole data set
generate(source,meta)
generate(source, meta)
}
@OptIn(DFInternal::class)
override fun execute(
dataSet: DataTree<T>,
meta: Meta,
): DataTree<R> = if(dataSet.isObservable()) {
): DataTree<R> = if (dataSet.isObservable()) {
MutableDataTree<R>(outputType, dataSet.updatesScope).apply {
generate(dataSet, meta)
dataSet.updates().onEach {
@ -64,8 +66,8 @@ public abstract class AbstractAction<T : Any, R : Any>(
close()
}
}
} else{
DataTree(outputType){
} else {
DataTree(outputType) {
generate(dataSet, meta)
}
}

View File

@ -86,8 +86,8 @@ internal class MapAction<T : Any, R : Any>(
data.forEach { mapOne(it.name, it.data, meta) }
}
override fun DataSink<R>.update(source: DataTree<T>, meta: Meta, namedData: NamedData<T>) {
mapOne(namedData.name, namedData.data, namedData.meta)
override fun DataSink<R>.update(source: DataTree<T>, meta: Meta, updatedData: NamedData<T>) {
mapOne(updatedData.name, updatedData.data, updatedData.meta)
}
}

View File

@ -77,8 +77,8 @@ internal class SplitAction<T : Any, R : Any>(
data.forEach { splitOne(it.name, it.data, meta) }
}
override fun DataSink<R>.update(source: DataTree<T>, meta: Meta, namedData: NamedData<T>) {
splitOne(namedData.name, namedData.data, namedData.meta)
override fun DataSink<R>.update(source: DataTree<T>, meta: Meta, updatedData: NamedData<T>) {
splitOne(updatedData.name, updatedData.data, updatedData.meta)
}
}

View File

@ -10,7 +10,7 @@ public interface NamedData<out T> : Named, Data<T> {
}
public operator fun NamedData<*>.component1(): Name = name
public operator fun <T: Any> NamedData<T>.component2(): Data<T> = data
public operator fun <T> NamedData<T>.component2(): Data<T> = data
private class NamedDataImpl<T>(
override val name: Name,

View File

@ -19,6 +19,10 @@ public annotation class MetaBuilderMarker
public interface MutableMetaProvider : MetaProvider, MutableValueProvider {
override fun get(name: Name): MutableMeta?
public operator fun set(name: Name, node: Meta?)
/**
* Set value with the given name. Does nothing if value is not changed.
*/
override fun setValue(name: Name, value: Value?)
}
@ -48,11 +52,13 @@ public interface MutableMeta : Meta, MutableMetaProvider {
}
override fun setValue(name: Name, value: Value?) {
if (value != getValue(name)) {
getOrCreate(name).value = value
}
}
/**
* Get existing node or create a new one
* Get an existing node or create a new one
*/
public fun getOrCreate(name: Name): MutableMeta
@ -198,10 +204,8 @@ public operator fun MutableMetaProvider.set(key: String, metas: Iterable<Meta>):
/**
* Update existing mutable node with another node. The rules are following:
* * value replaces anything
* * node updates node and replaces anything but node
* * node list updates node list if number of nodes in the list is the same and replaces anything otherwise
* Update the existing mutable node with another node.
* Values that are present in the current provider and are missing in [meta] are kept.
*/
public fun MutableMetaProvider.update(meta: Meta) {
meta.valueSequence().forEach { (name, value) ->
@ -222,7 +226,7 @@ public fun <M : MutableTypedMeta<M>> MutableTypedMeta<M>.edit(name: Name, builde
getOrCreate(name).apply(builder)
/**
* Set a value at a given [name]. If node does not exist, create it.
* Set a value at a given [name]. If a node does not exist, create it.
*/
public operator fun <M : MutableTypedMeta<M>> MutableTypedMeta<M>.set(name: Name, value: Value?) {
edit(name) {
@ -367,6 +371,21 @@ public fun MutableMeta.append(name: Name, value: Value): Unit = append(name, Met
public fun MutableMeta.append(key: String, value: Value): Unit = append(Name.parse(key), value)
/**
* Update all items that exist in the [newMeta] and remove existing items that are missing in [newMeta].
* This produces the same result as clearing all items and updating blank meta with a [newMeta], but does not
* produce unnecessary invalidation events (if they are supported).
*/
public fun MutableMeta.reset(newMeta: Meta) {
//remove old items
(items.keys - newMeta.items.keys).forEach {
remove(it.asName())
}
newMeta.items.forEach { (token, item)->
set(token, item)
}
}
/**
* Create a mutable copy of this meta. The copy is created even if the Meta is already mutable
*/

View File

@ -76,6 +76,8 @@ internal class MetaBuilder(
override fun set(name: Name, node: Meta?) {
//skip setting if value has not changed
if(node == get(name)) return
when (name.length) {
0 -> error("Can't set a meta with empty name")
1 -> {
@ -89,7 +91,7 @@ internal class MetaBuilder(
}
else -> {
getOrCreate(name.first().asName()).set(name.cutFirst(), node)
getOrCreate(name.first().asName())[name.cutFirst()] = node
}
}
}

View File

@ -61,4 +61,33 @@ class MetaTest {
assertEquals(null, indexed["8"])
assertEquals(12, indexed["12"].int)
}
@Test
fun reset() {
val oldMeta = MutableMeta {
"a" put {
"value" put "aValue"
}
"b" put {
"value" put "bValue"
}
"c" put {
"value" put "cValue"
}
}
val newMeta = Meta {
"a" put {
"value" put "aValue"
}
"b" put {
"value" put "bValue"
}
"d" put {
"value" put "dValue"
}
}
oldMeta.reset(newMeta)
println(oldMeta)
assertEquals(setOf("a", "b", "d"), oldMeta.items.keys.map { it.toString() }.toSet())
}
}

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists