Vampire
12/02/2024, 5:06 PMfor (i in 1..30) {
if (suspendingFunction().contains("what I wait for")) {
break
}
delay(1.seconds)
}
Just in nicer / fancier πSam
12/02/2024, 5:21 PMJohann Pardanaud
12/02/2024, 5:22 PMJohann Pardanaud
12/02/2024, 5:22 PMVampire
12/02/2024, 5:24 PMsequence { ... }
with takeUntil
but that doesn't work exactly due to the suspending function and the delay
too of course.Sam
12/02/2024, 5:25 PMRobert Williams
12/02/2024, 5:26 PMephemient
12/02/2024, 5:27 PMfun <T> Flow<T>.repeat(): Flow<T> = flow {
while (true) emitAll(this@repeat)
}
fun <T> Flow<T>.afterEach(block: suspend (T) -> Unit): Flow<T> = transform {
emit(it)
block(it)
}
then you could write
::suspendingFunction.asFlow()
.repeat()
.afterEach { delay(1.seconds) }
.take(30)
.firstOrNull { it.contains("what I wait for") }
Robert Williams
12/02/2024, 5:32 PMVampire
12/02/2024, 5:32 PMflow
is what I was missing, thanks, so
flow {
for (i in 1..30) {
emit(suspendingFunction())
delay(1.seconds)
}
}.takeUnless { it.contains("what I wait for") }
πVampire
12/02/2024, 5:34 PMbut Iβd suggest you first think hard about what the suspend fun is doing and consider if itβs something you could make observable by default rather than requiring pollingIt calls a utility and waits for the utility to not say anymore "upgrade in progress"
ephemient
12/02/2024, 5:34 PMVampire
12/02/2024, 5:36 PMflow {
for (i in 1 until 30) {
emit(suspendingFunction())
delay(1.seconds)
}
emit(suspendingFunction())
}.takeUnless { it.contains("what I wait for") }
πephemient
12/02/2024, 5:40 PMflow {
while (true) {
emit(suspendingFunction())
delay(1.seconds)
}
}.take(30)
doesn't have the trailing delay (and is equivalent to what I wrote earlier with helper functions)Vampire
12/02/2024, 5:40 PMasFlow()
ephemient
12/02/2024, 5:42 PMfun <T> (suspend () -> T).asFlow(): Flow<T> = flow {
emit(invoke())
}
which is kind of the only thing that could make sense. it can't call the function outside the flow since it's not itself suspend
Vampire
12/02/2024, 5:45 PMflow {
while (true) {
emit(suspendingFunction())
delay(1.seconds)
}
}
.take(30)
.takeUnless { it.contains("what I wait for") }
Vampire
12/02/2024, 5:45 PMVampire
12/02/2024, 6:17 PM.collect()
or it's not doing anything πephemient
12/02/2024, 6:18 PM.firstOrNull
Vampire
12/02/2024, 6:20 PMflow {
while (true) {
emit(wslStatus())
delay(1.seconds)
}
}
.take(30)
.firstOrNull { !it.contains("WSL is finishing an upgrade...") }
Johann Pardanaud
12/03/2024, 3:17 PMflowOf(1..30)
.transform {
emit(wslStatus())
delay(1.seconds)
}
.firstOrNull { !it.contains("WSL is finishing an upgrade...") }
Vampire
12/03/2024, 3:20 PMephemient
12/03/2024, 3:21 PMJohann Pardanaud
12/03/2024, 3:23 PMtake
I think?Vampire
12/03/2024, 3:23 PMit
in transform
is IntRange
Johann Pardanaud
12/03/2024, 3:23 PMVampire
12/03/2024, 3:24 PMVampire
12/03/2024, 3:24 PMephemient
12/03/2024, 3:25 PM(2..30).asFlow().onEach { delay(1.seconds) }.onStart { emit(1) }.map { wslStatus() }.firstOrNull { ... }
Johann Pardanaud
12/03/2024, 3:26 PMSecond line, end of lineOh right! That's not an issue, it's just the range we use on the first line, but it's overridden by the
emit
in the third lineVampire
12/03/2024, 3:26 PMOh right! That's not an issue, it's just the range we use on the first line, but it's overridden by theBut wouldn't the flow only be of size 1 then?in the third lineemit
ephemient
12/03/2024, 3:27 PM.asFlow()
, not flowOf()
Johann Pardanaud
12/03/2024, 3:27 PMJohann Pardanaud
12/03/2024, 3:27 PMVampire
12/03/2024, 4:02 PMslightly uglier but hypothetically you could swap things around a little@ephemient but that would do the delay before each value is emitted, so adds an unnecessary 1 second wait before the first invocation, wouldn't it?
ephemient
12/03/2024, 4:03 PMonStart
adds an element before any of the delaysVampire
12/03/2024, 4:07 PMephemient
12/03/2024, 4:13 PMVampire
12/03/2024, 4:14 PM(2..30)
.asFlow()
.onEach { delay(1.seconds) }
.onStart { emit(1) }
.map { wslStatus() }
.firstOrNull { !it.contains("WSL is finishing an upgrade...") }
best while not introducing extensions.
Or when not caring about the final delay that should not be reached anyway
(1..30)
.asFlow()
.transform {
emit(wslStatus())
delay(1.seconds)
}
.firstOrNull { !it.contains("WSL is finishing an upgrade...") }
Vampire
12/03/2024, 4:15 PM