Is there anything out there for repeatedly reading...
# coroutines
c
Is there anything out there for repeatedly reading input from stdin, something like a suspending version of
readLine()
in a while loop?
t
seems like you could create a flow that reads from stdin and yields once per line?
z
Copy code
val File.lines: Flow<String>
    get() = flow<String> {
      useLines { lines ->
        emitAll(lines.asFlow())
      }
    }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
o
that might close the source too early, perhaps
reader.lineSequence().asFlow().onCompletion { reader.close() }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
z
Why would it close the source too early?
useLines
won’t close the source until the block exits, which won’t happen until the collector finishes processing the last line. If anything it will close the source too late, which I think is what your solution fixes.
t
asFlow().onCompletion
seems suspect to leak on anything but the happy path?
o
I'm not sure what you mean by that Trevor?
onCompletion
is called upon exception as well iirc
t
copy that, rx brain leaking through. not yet a flow user myself
o
zach, yes, I apologize for that. Your version works as well.
z
@octylFractal Yours is objectively better, with mine if the last
emit
call suspends for a long time the file handle is effectively leaked. Could happen if a
buffer(RENDEZVOUS)
was chained right after the
flowOn
.