funfix icon indicating copy to clipboard operation
funfix copied to clipboard

implement bracket for resource usage

Open sledorze opened this issue 7 years ago • 7 comments
trafficstars

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)
  })

sledorze avatar Aug 02 '18 20:08 sledorze

We indeed need to update Funfix's IO with the latest developments, which includes bracket.

Your proposal might work for now, but note that:

  1. acquire has to be uncancelable
  2. release has to be uncancelable
  3. I don't remember if doOnCancel does the right thing, but we need to ensure that either doOnCancel or doOnFinish 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.

alexandru avatar Aug 03 '18 10:08 alexandru

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..

sledorze avatar Aug 03 '18 10:08 sledorze

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.

sledorze avatar Aug 03 '18 13:08 sledorze

Don't we have an uncancelable operation already?

alexandru avatar Aug 03 '18 15:08 alexandru

@alexandru Not found one actually..

sledorze avatar Aug 03 '18 15:08 sledorze

Here's the issue: https://github.com/funfix/funfix/issues/138

sledorze avatar Aug 03 '18 15:08 sledorze

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 .

alexandru avatar Aug 03 '18 15:08 alexandru