funfix
funfix copied to clipboard
implement bracket for resource usage
I put that quickly in shape. I've done some (Unit) tests that are passing.. (including cancellation)
I'm wondering if I'm on the right path of if there's a fundamental flaw in it?
const bracket = <A>(acquire: IO<A>) => (release: (a: A) => IO<void>) => <B>(
utilize: (a: A) => IO<B>
): IO<B> =>
acquire.chain(resource => {
const doRelease = release(resource)
return utilize(resource)
.doOnCancel(doRelease)
.doOnFinish(_ => doRelease)
})
We indeed need to update Funfix's IO with the latest developments, which includes bracket.
Your proposal might work for now, but note that:
acquirehas to be uncancelablereleasehas to be uncancelable- I don't remember if
doOnCanceldoes the right thing, but we need to ensure that eitherdoOnCancelordoOnFinishexecute and not both
We would also need a bracketCase that discriminates between the exit cases, like we pushed in Cats-Effect.
I'm caught up with work on Monix and Cats-Effect at the moment, unfortunately. Plus work and life, I did not have any time left for Funfix.
Once Cats-Effect 1.0 and Monix 3.0 will be finally out, I hope to have some time for giving Funfix some needed updates.
Until then PRs are welcome in case you'd like to give it a try.
About 1 and 2, is there a way to enforce this? About 3, I've made the tests and it works. I Maybe do a PR, but I'm not versed into Flow..
I've seen cancelable IO are IOAsync which have the cancellation logic in their Context.
On may use a constraint using the '_tag' value to enforce statically correct usage of the API but I find that sloppy.
SO I guess, I just need to make them become uncancellable in the implementation of bracket.
Don't we have an uncancelable operation already?
@alexandru Not found one actually..
Here's the issue: https://github.com/funfix/funfix/issues/138
Ah, OK, well, adding such an operation is not very hard to do actually, but requires some internals juggling.
The interruption capability is basically handled by an internal StackedCancelable.
What we need to do in an uncancelable operation is to pass a
StackedCancelable that can never be cancelled, as in a fake instance that
does not keep any cancel tokens and that ignores all "cancel" requests.
On Fri, Aug 3, 2018 at 6:27 PM Stéphane Le Dorze [email protected] wrote:
@alexandru https://github.com/alexandru Not found one actually..
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/funfix/funfix/issues/137#issuecomment-410289002, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAt6RFONwdSCThNH84ia2obqgRRsXyoks5uNGvggaJpZM4Vs6pW .