SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def interruptable[F[_]: Concurrent: ContextShift](thunk: => A): F[A] = {
  2.   val fa: F[A] = Concurrent[F] cancelable { cb =>
  3.     val done = new AtomicBoolean(false)
  4.     val t = new Thread { () =>
  5.       try {
  6.         val result = thunk
  7.         done.set(true)
  8.         cb(Right(result))
  9.       } catch {
  10.         case NonFatal(e) =>
  11.           done.set(true)
  12.           cb(Left(e))
  13.  
  14.         case t: Throwable =>
  15.           done.set(true)
  16.           throw t
  17.       }
  18.     }
  19.     t.setDaemon(true)
  20.     t.setName("interruptable-thingy-todo")
  21.     t.start()
  22.  
  23.     Sync[F] delay {
  24.       while (!done.get()) {
  25.         t.interrupt()
  26.       }
  27.     }
  28.   }
  29.  
  30.   fa.guarantee(ContextShift[F].shift)
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top