Skip to content

Commit

Permalink
Add Monix Task integration (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbylicki authored and dimafeng committed Dec 26, 2018
1 parent f5694d4 commit c9b6727
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
14 changes: 13 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ val mockitoVersion = "1.10.19"
val scalaTestVersion = "3.0.5"
val slf4jVersion = "1.7.21"
val catsEffectsVersion = "1.1.0"
val monixVersion = "3.0.0-RC2"

//lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")

Expand Down Expand Up @@ -45,7 +46,8 @@ lazy val noPublishSettings = Seq(
lazy val root = (project in file("."))
.aggregate(
core,
catsEffect
catsEffect,
monix
)
.settings(noPublishSettings)
.settings(
Expand Down Expand Up @@ -94,6 +96,16 @@ lazy val catsEffect = (project in file("cats-effect"))
)
)

lazy val monix = (project in file("monix"))
.dependsOn(core % "compile->compile;test->test")
.settings(commonSettings: _*)
.settings(
name := "neotypes-monix",
libraryDependencies ++= PROVIDED(
"io.monix" %% "monix" % monixVersion
)
)

lazy val microsite = (project in file("docs"))
.settings(moduleName := "docs")
.enablePlugins(MicrositesPlugin)
Expand Down
23 changes: 23 additions & 0 deletions monix/src/main/scala/neotypes/monix/MonixAsync.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package neotypes.monix

import monix.eval.Task
import monix.execution.Scheduler
import neotypes.Async

class MonixAsync(implicit s: Scheduler) extends Async[Task] {

override def async[T](cb: (Either[Throwable, T] => Unit) => Unit): Task[T] = Task.async(cb)

override def flatMap[T, U](m: Task[T])(f: T => Task[U]): Task[U] = m.flatMap(f)

override def map[T, U](m: Task[T])(f: T => U): Task[U] = m.map(f)

override def recoverWith[T, U >: T](m: Task[T])(f: PartialFunction[Throwable, Task[U]]): Task[U] =
m.onErrorRecoverWith(f)

override def failed[T](e: Throwable): Task[T] = Task.raiseError(e)
}

object implicits {
implicit def monixAsync(implicit s: Scheduler): Async[Task] = new MonixAsync
}
27 changes: 27 additions & 0 deletions monix/src/test/scala/neotypes/monix/MonixAsyncSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package neotypes.monix

import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import neotypes.monix.implicits._
import neotypes.implicits._
import neotypes.{BaseIntegrationSpec, BasicSessionSpec}
import org.neo4j.driver.v1.exceptions.ClientException
import org.scalatest.FlatSpec

import scala.concurrent.duration._

class MonixAsyncSpec extends FlatSpec with BaseIntegrationSpec {
it should "work with Task" in {
val s = driver.session().asScala[Task]

val string = "match (p:Person {name: 'Charlize Theron'}) return p.name".query[String].single(s)
.runSyncUnsafe(5 seconds)
assert(string == "Charlize Theron")

assertThrows[ClientException] {
"match test return p.name".query[String].single(s).runSyncUnsafe(5 seconds)
}
}

override val initQuery: String = BasicSessionSpec.INIT_QUERY
}

0 comments on commit c9b6727

Please sign in to comment.