Update DataSetBuilder API

This commit is contained in:
Alexander Nozik 2021-01-31 22:34:48 +03:00
parent e66ae408cd
commit 4d19d97c53
7 changed files with 39 additions and 21 deletions

View File

@ -21,3 +21,7 @@ kotlin {
}
}
}
readme{
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL
}

View File

@ -74,18 +74,6 @@ public suspend fun <T : Any> DataSetBuilder<T>.emit(name: String, data: Data<T>)
emit(name.toName(), data)
}
public suspend fun <T : Any> DataSetBuilder<T>.data(name: Name, data: T, meta: Meta = Meta.EMPTY) {
emit(name, Data.static(data, meta))
}
public suspend fun <T : Any> DataSetBuilder<T>.data(name: Name, data: T, block: MetaBuilder.() -> Unit = {}) {
emit(name, Data.static(data, Meta(block)))
}
public suspend fun <T : Any> DataSetBuilder<T>.data(name: String, data: T, block: MetaBuilder.() -> Unit = {}) {
emit(name.toName(), Data.static(data, Meta(block)))
}
public suspend fun <T : Any> DataSetBuilder<T>.emit(name: String, set: DataSet<T>) {
this.emit(name.toName(), set)
}
@ -97,6 +85,32 @@ public suspend fun <T : Any> DataSetBuilder<T>.emit(data: NamedData<T>) {
emit(data.name, data.data)
}
/**
* Produce lazy [Data] and emit it into the [DataSetBuilder]
*/
public suspend inline fun <reified T : Any> DataSetBuilder<T>.emitLazy(
name: String,
meta: Meta,
noinline producer: suspend () -> T,
) {
val data = Data(meta, block = producer)
emit(name, data)
}
/**
* Emit a static data with the fixed value
*/
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(name: String, data: T, meta: Meta = Meta.EMPTY): Unit =
emit(name, Data.static(data, meta))
public suspend fun <T : Any> DataSetBuilder<T>.emitStatic(
name: String,
data: T,
metaBuilder: MetaBuilder.() -> Unit,
) {
emit(name.toName(), Data.static(data, Meta(metaBuilder)))
}
/**
* Update data with given node data and meta with node meta.
*/

View File

@ -14,7 +14,7 @@ class ActionsTest {
val data: DataTree<Int> = runBlocking {
DataTree {
repeat(10) {
data(it.toString(), it)
emitStatic(it.toString(), it)
}
}
}

View File

@ -19,10 +19,10 @@ internal class DataTreeBuilderTest {
val node = DataTree<Any> {
emit("primary") {
data("a", "a")
data("b", "b")
emitStatic("a", "a")
emitStatic("b", "b")
}
data("root", "root")
emitStatic("root", "root")
populate(updateData)
}
@ -40,7 +40,7 @@ internal class DataTreeBuilderTest {
updateJob = launch {
repeat(10) {
delay(10)
data("value", it)
emitStatic("value", it)
}
delay(10)
}

View File

@ -49,7 +49,7 @@ class DataPropagationTest {
runBlocking {
data {
repeat(100) {
data("myData[$it]", it)
emitStatic("myData[$it]", it)
}
}
}

View File

@ -23,11 +23,11 @@ class FileDataTest {
val dataNode = runBlocking {
DataTree<String> {
emit("dir") {
data("a", "Some string") {
emitStatic("a", "Some string") {
"content" put "Some string"
}
}
data("b", "root data")
emitStatic("b", "root data")
meta {
"content" put "This is root meta node"
}

View File

@ -56,7 +56,7 @@ class SimpleWorkspaceTest {
data {
repeat(100) {
data("myData[$it]", it)
emitStatic("myData[$it]", it)
}
}