Search code examples
arrow-kt

How to cancel arrow-kt IO?


Is is possible to cancel an (Arrow-Kt) IO?

In RxJava whenever I do observable.subscribe({ // handle success },{ // some errohandling }), I am given a Disposable which I could call Disposable.dispose() on.

Similar with coroutines, doing coroutineScope.launch { // Some suspending function } returns a Job to which I could call, Job.cancel() and even call coroutineScope.cancel() to cancel all coroutines in the scope.

I am looking for the same thing with IO. How could I achieve the same thing in IO? I am only calling IO.unsafeRunAsyncCancellable { // Some Operations } but I can't seem to find a way to cancel it if I have to. Although it returns a Disposable, I don't exactly know what to do with it as it is just a type alias for () -> Unit.

Can someone guide me here?


Solution

  • As you mentioned yourself unsafeRunAsyncCancellable returns Disposable. It's indeed a typealias for () -> Unit, which is the same as Disposable#dispose froom RxJava. It's a function that takes no arguments, and returns Unit. Upon invocation it cancels the IO. Since it's simply a function something like CompositeDisposable from RxJava becomes simple function composition.

    Further more, we also have integrations for KotlinX Coroutines structured concurrency in the case you'd like to interopt with that library. In that case you can use the KotlinX integration module which is currently in SNAPSHOT but is expected to be released this month.

    I hope that answers your questions :)