Skip to content

Instantly share code, notes, and snippets.

@alexandru
Created August 16, 2016 07:46
Show Gist options
  • Save alexandru/e8c853e9113d25e3191311b2a5b608f4 to your computer and use it in GitHub Desktop.
Save alexandru/e8c853e9113d25e3191311b2a5b608f4 to your computer and use it in GitHub Desktop.
import monix.eval.Task
import monix.execution.Scheduler
import scala.util.control.NonFatal
/** Evaluates the given non-strict value on a given
* `Scheduler`, meant for expensive I/O.
*
* NOTE: the returned task is not cancelable. In this
* instance you wouldn't gain anything by it.
*/
def executeIO[A](ioScheduler: Scheduler)(f: => A): Task[A] =
Task.unsafeCreate { (scheduler, conn, callback) =>
// Schedule for execution on the given thread!
ioScheduler.execute(new Runnable {
def run(): Unit = {
var signalErrors = true
try {
val result = f
signalErrors = false
callback.onSuccess(result)
} catch {
case NonFatal(ex) =>
if (signalErrors) callback.onError(ex)
else scheduler.reportFailure(ex)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment