Replace branch data builder with putAll

This commit is contained in:
Alexander Nozik 2024-02-19 14:26:33 +03:00
parent b575801983
commit db2da2027d
2 changed files with 18 additions and 24 deletions

View File

@ -5,7 +5,6 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.MutableMeta import space.kscience.dataforge.meta.MutableMeta
import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.asName
import space.kscience.dataforge.names.isEmpty import space.kscience.dataforge.names.isEmpty
@ -117,30 +116,25 @@ public fun <T> DataSink<T>.putAll(sequence: Sequence<NamedData<T>>) {
} }
public fun <T> DataSink<T>.putAll(tree: DataTree<T>) { public fun <T> DataSink<T>.putAll(tree: DataTree<T>) {
this.putAll(tree.asSequence()) putAll(tree.asSequence())
}
/**
* Update data with given node data and meta with node meta.
*/
@DFExperimental
public fun <T> MutableDataTree<T>.putAll(source: DataTree<T>) {
source.forEach {
put(it.name, it.data)
}
} }
/** /**
* Copy given data set and mirror its changes to this [DataSink] in [this@setAndObserve]. Returns an update [Job] * Copy given data set and mirror its changes to this [DataSink] in [this@setAndObserve]. Returns an update [Job]
*/ */
public fun <T : Any> DataSink<T>.putAllAndWatch(
branchName: Name = Name.EMPTY,
dataSet: ObservableDataTree<T>,
): Job {
putAll(branchName, dataSet)
return dataSet.updates().onEach {
put(branchName + it.name, it.data)
}.launchIn(dataSet.updatesScope)
}
@Deprecated("Use putAllAndWatch", ReplaceWith("putAllAndWatch(name, dataSet)"))
public fun <T : Any> DataSink<T>.watchBranch( public fun <T : Any> DataSink<T>.watchBranch(
name: Name, name: Name,
dataSet: ObservableDataTree<T>, dataSet: ObservableDataTree<T>,
): Job { ): Job = putAllAndWatch(name, dataSet)
putAll(name, dataSet)
return dataSet.updates().onEach {
put(name + it.name, it.data)
}.launchIn(dataSet.updatesScope)
}

View File

@ -43,7 +43,7 @@ internal class DataTreeBuilderTest {
wrap("b", "b") wrap("b", "b")
} }
wrap("root", "root") wrap("root", "root")
this.putAll(updateData) putAll(updateData)
} }
assertEquals("a", node["update.a"]?.await()) assertEquals("a", node["update.a"]?.await())
@ -55,7 +55,7 @@ internal class DataTreeBuilderTest {
val subNode = MutableDataTree<Int>() val subNode = MutableDataTree<Int>()
val rootNode = MutableDataTree<Int> { val rootNode = MutableDataTree<Int> {
watchBranch("sub".asName(), subNode) putAllAndWatch("sub".asName(), subNode)
} }
repeat(10) { repeat(10) {