Skip to content

Instantly share code, notes, and snippets.

@tomjadams
Last active March 6, 2017 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjadams/bf3b6291822685c960d9c1b7fd0550fd to your computer and use it in GitHub Desktop.
Save tomjadams/bf3b6291822685c960d9c1b7fd0550fd to your computer and use it in GitHub Desktop.
package com.redbubble.util.fetch
import cats.data.NonEmptyList
import cats.free.Free
import fetch._
object FetchConcurrent {
def multipleConcurrently[I, A](i: I, is: I*)(implicit ds: DataSource[I, A]): Fetch[List[A]] = {
val idsAsNel = NonEmptyList(i, is.toList)
val queries = NonEmptyList.of(FetchMany(idsAsNel, ds).asInstanceOf[FetchQuery[Any, Any]])
concurrently(queries).map { imc =>
idsAsNel.foldLeft(List[A]()) { (acc, id) =>
val key = ds.identity(id)
imc.get(key).fold(acc)((v: A) => acc.:+(v))
}
}
}
private def concurrently(queries: NonEmptyList[FetchQuery[Any, Any]]): Fetch[InMemoryCache] =
Free.liftF(Concurrent(queries))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment