diesieben07
01/20/2020, 7:08 PMfun foo(): Flow<ResultA>
and each ResultA
has a fun getElements(): Flow<SubResult>
. The getElements
is tied to the same execution context as the containing flow and has to be cancelled with it.streetsofboston
01/20/2020, 7:33 PMflatMapXXX
functions on Flow.diesieben07
01/20/2020, 7:36 PMfoo
and getElements
methods.
How can I ensure that someone calls getElements
only while consuming the outer flow and not later?streetsofboston
01/20/2020, 7:52 PMdiesieben07
01/20/2020, 7:54 PMstreetsofboston
01/20/2020, 8:04 PMclass Outer {
...
fun foo(): Flow<ResultA> = ...
...
fun ResultA.getElements(): Flow<SubResult> = ...
}
class ResultA {
...
...
fun test(outer: Outer) {
val flow<SubResult> = with (outer) {
foo().flatMapConcat { it.getElements() }
}
...
}
...
}
The above limits the scope (not the number of times) in which you can call them. In the above example, you limit it to the scope of instances of ‘Outer’.diesieben07
01/20/2020, 8:07 PMclass SubResult
class Outer {
fun foo(): Flow<ResultA> = TODO()
fun ResultA.getElements(): Flow<SubResult> = TODO()
}
class ResultA {
suspend fun test(outer: Outer) {
val results = outer.foo().toList()
with(outer) {
results.flatMap {
/* this call to getElements is invalid, it happens after the outer flow is collected */
it.getElements().toList()
}
}
}
}
streetsofboston
01/20/2020, 8:12 PMdiesieben07
01/20/2020, 8:14 PMstreetsofboston
01/20/2020, 8:17 PMfun batches() : Flow<Batch>
and fun Batch.rows(): Flow<Row>
?diesieben07
01/20/2020, 8:19 PMrows
twice on the same batch? Because thats not valid...streetsofboston
01/20/2020, 8:19 PMdiesieben07
01/20/2020, 8:20 PMstreetsofboston
01/20/2020, 8:21 PMfun Batch.rows()
would have the returned Flow<Row>
emit an error instead of a proper flow of rows when it is tried to get collected after it has completed.class Batch {
...
var canBeOpenend: Boolean = true
...
fun rows(): Flow<Row> = flow {
if (!canBeOpenend) throw SomeException()
canBeOpened = false
... find rows in the batch to emit ...
}
...
}
diesieben07
01/20/2020, 8:40 PMbdawg.io
01/20/2020, 11:54 PM