Skip to content

Instantly share code, notes, and snippets.

@elyphas
Created August 1, 2024 22:00
Show Gist options
  • Save elyphas/1673574ed1f5c4e485e9faea7dee680c to your computer and use it in GitHub Desktop.
Save elyphas/1673574ed1f5c4e485e9faea7dee680c to your computer and use it in GitHub Desktop.
trait Repository2 {
type Input
type Output
val instance: Output
private val scheme: String = dom.window.location.protocol
private val host = dom.window.location.host
protected val rootPath: Uri = Uri.unsafeFromString(s"$scheme//$host")
protected val client: Client[IO] = FetchClientBuilder[IO].create
protected val endPoint: String
val event: PublishSubject[(Input, EventsDBWillDox)] = Subject.publish[(Input, EventsDBWillDox)]
val onEvent: Observable[Either[Throwable, (Output, EventsDBResult)]]
val onSuccess: Observable[Output] = onEvent.filter(_.fold(l => false, r => true))
.map {
case Left(value) => instance
case Right((lst, event)) => lst
}
}
trait Repository3 extends Repository2 {
type Input = Recipe
type Output = List[ItemRecipe]
val instance: Output = List.empty[ItemRecipe]
protected val endPoint = "getLstItemsRecipe"
implicit val bodyPostEncode: EntityEncoder[IO, Input] = jsonEncoderOf
implicit val responseDecode: EntityDecoder[IO, (Output, EventsDBResult)] = jsonOf
override val onEvent: Observable[Either[Throwable, (Output, EventsDBResult)]] = {
event.switchMap { bodyPost =>
val (recipe, event) = bodyPost
val bodyPostRefine = recipe
Observable.fromEffect {
client.fetchAs[(Output, EventsDBResult)](POST(bodyPostRefine, rootPath / endPoint))
.redeem (
error => (new Exception(error)).asLeft[(Output, EventsDBResult)],
something => something.asRight[Throwable]
)
}
}
}
}
implicit val repoLstItemsRecipe3: Repository3 = new Repository3{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment