Trevor Stone
01/27/2023, 5:32 PMTrevor Stone
01/27/2023, 5:33 PMfun interface StringListScope {
fun addString(string: String)
}
@Composable
fun StringListBuilder(content: @Composable StringListScope.() -> Unit): List<String> = run {
(mutableListOf<String>()).also {
StringListScope { string: String -> it.add(string) }.content()
}
}
@Composable fun StringListScope.Hello(prefix: String) = addString("$prefix Hello")
@Composable fun StringListScope.World(suffix: String) = addString("World $suffix")
@Composable
fun StringListOutput(count: Int) = StringListBuilder {
Hello("$count")
World("$count")
}
val flow =
moleculeFlow(RecompositionClock.Immediate) {
var count by remember { mutableStateOf(0) }
LaunchedEffect(Unit) {
while (true) {
delay(500)
count++
}
}
StringListOutput(count)
}
jw
01/28/2023, 2:34 AMTrevor Stone
01/28/2023, 2:34 AMjw
01/28/2023, 2:34 AMcount
is it included in every emission? Or only the first?Trevor Stone
01/28/2023, 2:38 AMfun interface StringListScope {
fun addString(string: String)
}
@Composable
fun StringListBuilder(content: @Composable StringListScope.() -> Unit): List<String> = run {
(mutableListOf<String>()).also {
StringListScope { string: String -> it.add(string) }.content()
}
}
@Composable fun StringListScope.Hello(prefix: String) = addString("$prefix Hello")
@Composable fun StringListScope.World() = addString("World")
@Composable
fun StringListOutput(count: Int) = StringListBuilder {
Hello("$count")
World()
}
val flow: Flow<List<String>> =
moleculeFlow(RecompositionClock.Immediate) {
var count by remember { mutableStateOf(0) }
LaunchedEffect(Unit) {
while (true) {
delay(500)
count++
}
}
StringListOutput(count)
}
@Test
fun sanityCheck() = runTest {
flow.test {
println("first" + awaitItem())
cancelAndIgnoreRemainingEvents()
}
flow.test {
println("second" + awaitItem())
println("second" + awaitItem())
println("second" + awaitItem())
println("second" + awaitItem())
cancelAndIgnoreRemainingEvents()
}
}
Trevor Stone
01/28/2023, 2:38 AMfirst[0 Hello, World]
second[0 Hello, World]
second[1 Hello, World]
second[2 Hello, World]
second[3 Hello, World]
Trevor Stone
01/28/2023, 2:38 AMTrevor Stone
01/28/2023, 2:41 AMTrevor Stone
01/28/2023, 2:42 AMjw
01/28/2023, 2:42 AMjw
01/28/2023, 2:43 AMStringListScope.something()
function would call ComposeNode
to emit something into the treejw
01/28/2023, 2:44 AMComposeNode
use the children
parameter, the result will be a root node and a single set of children (basically, a list)Trevor Stone
01/28/2023, 2:45 AMTrevor Stone
01/28/2023, 2:45 AMjw
01/28/2023, 2:45 AMjw
01/28/2023, 2:47 AMjw
01/28/2023, 2:48 AMjw
01/28/2023, 2:48 AMjw
01/28/2023, 2:48 AMjw
01/28/2023, 2:48 AMTrevor Stone
01/28/2023, 2:48 AMjw
01/28/2023, 2:52 AMjw
01/28/2023, 2:52 AMjw
01/28/2023, 2:52 AMTrevor Stone
01/28/2023, 2:52 AMTrevor Stone
01/28/2023, 2:58 AMjw
01/28/2023, 3:00 AMTrevor Stone
01/29/2023, 11:52 PMsetContent
. When the body
recomposes the new return value gets returned and emitted. Based upon mosaic, I have a root node that I want to emit instead.
mosaic seems to update the output on a 50ms interval, but I was hoping to have a solution that emits nothing unless a change has occurred. Is there a place in the recomposition lifecycle I could be emitting from?jw
01/30/2023, 12:18 AMTrevor Stone
01/30/2023, 12:58 AMoverride fun setContent(content: @Composable () -> Unit) {
composition.setContent(content)
hasFrameWaiters = true
}
Trevor Stone
01/30/2023, 2:30 AM