I happened to check sources of `Flow.collectIndexe...
# coroutines
d
I happened to check sources of
Flow.collectIndexed
and it's implemented like this:
Copy code
collect(object : FlowCollector<T> {
    private var index = 0
    override suspend fun emit(value: T) = action(checkIndexOverflow(index++), value)
})
I'm curious, why is this thread-safe? Can't it happen so that
index
is initialized on one thread and incremented in another? (if some
withContext
will somehow be used by client) Or are there some invariants which are at play here?
l
Because flow collection is all sequential, there's no parallel calls happening, and emit can only be called in the same coroutineContext, can't call it from another place on the same FlowCollector instance.
d
oh, right. it's all "coupled" together here.