implement bracket for resource usage
Closed this issue · 7 comments
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:
acquire
has to be uncancelablerelease
has to be uncancelable- I don't remember if
doOnCancel
does the right thing, but we need to ensure that eitherdoOnCancel
ordoOnFinish
execute 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..