-
-
Save tomjadams/bf3b6291822685c960d9c1b7fd0550fd to your computer and use it in GitHub Desktop.
This file contains 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
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