Created
August 16, 2016 07:46
-
-
Save alexandru/e8c853e9113d25e3191311b2a5b608f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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