What's the purpose of the `Flow.cancellable` opera...
# coroutines
t
What's the purpose of the
Flow.cancellable
operator ? What kind of use case does it solve ?
l
I think it's for when you have a flow that is made completely out of non cancellable functions but you want it to be cancellable on element emission. Am I right @elizarov?
👀 1
e
Right
o
l
That example is ready to paste in the editor on kotl.in or play.kotlin for you to try @tseisel
t
I see, this checks for cancellation on each
emit
and propagate it when applicable, which is not performed otherwise for performance reasons. Does this apply for thread-blocking code as well ?
Thread.sleep
show a different behavior
o
no, unless you wrap it in
runInterruptible
-- cancellation must be picked up by checking
isActive
, which is impossible for anything that isn't depending on the coroutines library (this means that suspend functions defined in stdlib also are not cancellable)
runInterruptible
registers a handler on the appropriate
Job
that calls
Thread.interrupt()
on cancellation
l
@octylFractal Where does
runInterruptible
come from?
l
Ah, I had to update kotllinx.coroutines in the project I'm working on (how ironic since it's a project to update libraries!), TIL this exists, great stuff for interop, thank you 👍