https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

sboishtyan

05/12/2021, 4:48 PM
Hi everyone How could I write the code below with
Flow
Copy code
Observable.create(
            { emitter ->
                val process: Process = ProcessBuilder(commandAndArgs)
                    .redirectErrorStream(true)
                    .start()

                emitter.setCancellation {
                    process.destroy()
                }
        )
So idea is that I want when someone starts collecting the flow I will create a process and when a client stops collecting I will destroy the process
e

ephemient

05/12/2021, 7:39 PM
Copy code
channelFlow {
    val process = ...
    invokeOnClose { process.destroy() }
    process.inputStream.reader().useLines { lines ->
        for (line in lines) send(line)
    }
}
s

sboishtyan

05/12/2021, 8:50 PM
thank you
3 Views