Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions shared/src/main/scala/fetch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ object `package` {
))
)

def liftIO[F[_] : ConcurrentEffect, A](io: IO[A]): Fetch[F, A] =
Unfetch[F, A](for {
either <- ConcurrentEffect[F].liftIO(io).attempt
result = either match {
case Left(err) => Throw[F, A](log => UnhandledException(err, log))
case Right(r) => Done[F, A](r)
}
} yield result)

// Running a Fetch

/**
Expand Down
33 changes: 33 additions & 0 deletions shared/src/test/scala/FetchTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -851,4 +851,37 @@ class FetchTests extends FetchSpec {

Fetch.run[IO](fetch).map(_ shouldEqual 1).unsafeToFuture
}

// IO in Fetch

"We can lift IO actions into Fetch" in {
def fetch[F[_] : ConcurrentEffect]: Fetch[F, Int] =
Fetch.liftIO(IO(42))

Fetch.run[IO](fetch).map(_ shouldEqual 42).unsafeToFuture
}

"A failed IO action lifted into Fetch will cause a Fetch to fail" in {
def fetch[F[_] : ConcurrentEffect] =
Fetch.liftIO(IO.raiseError(AnException()))

val io = Fetch.run[IO](fetch)

io.attempt
.map(_ should matchPattern {
case Left(UnhandledException(AnException(), _)) =>
}).unsafeToFuture
}

"A IO action can be combined with data fetches" in {
def fetch[F[_] : ConcurrentEffect]: Fetch[F, List[Int]] = for {
x <- Fetch.liftIO(IO(3))
manies <- many(x)
(ones, y) <- (manies.traverse(one[F]), Fetch.liftIO(IO(42))).tupled
} yield ones :+ y

val io = Fetch.run[IO](fetch)

io.map(_ shouldEqual List(0, 1, 2, 42)).unsafeToFuture
}
}