In one of my projects, I have to use a callback-based asynchronous API. I wanted to introduce cats IO, yet I have some troubles with it:
IO.cancelable { cb =>
val subscription = createSubscription(
...,
(msg: Message) => cb { Right("SomeResult") }
)
IO {subscription.unsubscribe()}
}
It looks quite nice, however I also need to unsubscribe after receiving a message. Unfortunately IO {subscription.unsubscribe()} is evaluated only in the case of cancellation.
I can make a quick workaround, but it seems very counter-intuitive :
IO.cancelable { cb =>
lazy val subscription = createSubscription(
...,
(msg: Message) => cb {
subscription.unsubscribe() //I can't be really sure if subscription is already initialized
Right("SomeResult")
}
)
subscribe //I have to ensure evalutaion of lazy val
IO {subscription.unsubscribe()}
}
I have checked cats-effects doc, sadly I didn't find anything that could help me with this problem. I can think of some bullet-proof solutions for this issue, yet it seems so typical that I can't believe that it wasn't already solved.
Is there any simple way to achieve this resource release behavior using cats?