Skip to content
Closed
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
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Or, if using Scala.js (0.6.x):

## Remote data

Fetch is a library for making access to data both simple & efficient. Fetch is especially useful when querying data that
Fetch is a library for making access to data both simple and efficient. Fetch is especially useful when querying data that
has a latency cost, such as databases or web services.

## Define your data sources
Expand All @@ -61,7 +61,7 @@ trait DataSource[F[_], Identity, Result]{
}
```

Returning `Concurrent` instances from the fetch methods allows us to specify if the fetch must run synchronously or asynchronously and use all the goodies available in `cats` and `cats-effect`.
Returning `Concurrent` instances from the fetch methods allows us to specify if the fetch must run synchronously or asynchronously, and use all the goodies available in `cats` and `cats-effect`.

We'll implement a dummy data source that can convert integers to strings. For convenience, we define a `fetchString` function that lifts identities (`Int` in our dummy data source) to a `Fetch`.

Expand Down Expand Up @@ -108,7 +108,7 @@ def fetchString[F[_] : Concurrent](n: Int): Fetch[F, String] =

Since `Fetch` relies on `Concurrent` from the `cats-effect` library, we'll need a runtime for executing our effects. We'll be using `IO` from `cats-effect` to run fetches, but you can use any type that has a `Concurrent` instance.

For executing `IO` we need a `ContextShift[IO]` used for running `IO` instances and a `Timer[IO]` that is used for scheduling, let's go ahead and create them, we'll use a `java.util.concurrent.ScheduledThreadPoolExecutor` with a couple of threads to run our fetches.
For executing `IO`, we need a `ContextShift[IO]` used for running `IO` instances and a `Timer[IO]` that is used for scheduling. Let's go ahead and create them. We'll use a `java.util.concurrent.ScheduledThreadPoolExecutor` with a couple of threads to run our fetches.

```scala
import java.util.concurrent._
Expand All @@ -130,7 +130,7 @@ def fetchOne[F[_] : Concurrent]: Fetch[F, String] =
fetchString(1)
```

Let's run it and wait for the fetch to complete, we'll use `IO#unsafeRunTimed` for testing purposes, which will run an `IO[A]` to `Option[A]` and return `None` if it didn't complete in time:
Let's run it and wait for the fetch to complete. We'll use `IO#unsafeRunTimed` for testing purposes, which will run an `IO[A]` to `Option[A]` and return `None` if it didn't complete in time:

```scala
import scala.concurrent.duration._
Expand All @@ -146,14 +146,14 @@ As you can see in the previous example, the `ToStringSource` is queried once to

## Batching

Multiple fetches to the same data source are automatically batched. For illustrating it, we are going to compose three independent fetch results as a tuple.
Multiple fetches to the same data source are automatically batched. For illustrating this, we are going to compose three independent fetch results as a tuple.

```scala
def fetchThree[F[_] : Concurrent]: Fetch[F, (String, String, String)] =
(fetchString(1), fetchString(2), fetchString(3)).tupled
```

When executing the above fetch, note how the three identities get batched and the data source is only queried once.
When executing the above fetch, note how the three identities get batched, and the data source is only queried once.

```scala
Fetch.run[IO](fetchThree).unsafeRunTimed(5.seconds)
Expand All @@ -162,7 +162,7 @@ Fetch.run[IO](fetchThree).unsafeRunTimed(5.seconds)
// res1: Option[(String, String, String)] = Some((1,2,3))
```

Note that the `DataSource#batch` method is not mandatory, it will be implemented in terms of `DataSource#fetch` if you don't provide an implementation.
Note that the `DataSource#batch` method is not mandatory. It will be implemented in terms of `DataSource#fetch` if you don't provide an implementation.

```scala
object UnbatchedToString extends Data[Int, String] {
Expand Down Expand Up @@ -267,7 +267,7 @@ def fetchTwice[F[_] : Concurrent]: Fetch[F, (String, String)] = for {
} yield (one, two)
```

While running it, notice that the data source is only queried once. The next time the identity is requested it's served from the cache.
While running it, notice that the data source is only queried once. The next time the identity is requested, it's served from the cache.

```scala
Fetch.run[IO](fetchTwice).unsafeRunTimed(5.seconds)
Expand All @@ -281,11 +281,11 @@ Fetch.run[IO](fetchTwice).unsafeRunTimed(5.seconds)

---

For more in-depth information take a look at our [documentation](http://47deg.github.io/fetch/docs.html).
For more in-depth information, take a look at our [documentation](http://47deg.github.io/fetch/docs.html).

## Fetch in the wild

If you wish to add your library here please consider a PR to include it in the list below.
If you wish to add your library here, please consider a PR to include it in the list below.

[comment]: # (Start Copyright)
# Copyright
Expand All @@ -294,4 +294,4 @@ Fetch is designed and developed by 47 Degrees

Copyright (C) 2016-2019 47 Degrees. <http://47deg.com>

[comment]: # (End Copyright)
[comment]: # (End Copyright)