Skip to content

Instantly share code, notes, and snippets.

@elyphas
Created August 6, 2021 22:19
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 elyphas/6baf933d035f32384b012b6ef090b0ad to your computer and use it in GitHub Desktop.
Save elyphas/6baf933d035f32384b012b6ef090b0ad to your computer and use it in GitHub Desktop.
val eventDocument = PublishSubject[(Inputs, EventsDoobieWillDo)]()
//Old, New
val onEventDocument: Observable[Either[String, (Inputs, Inputs, EventsDoobieResult)]] =
eventDocument.dump("onEventDocument:").mapEval { case (oldItem, event) =>
Task.suspend {
Task.fromFuture {
val payload = Pickle.intoBytes[(Inputs, EventsDoobieWillDo)]((oldItem, event))
val endPoint = event match {
case InsertDoobie => "saveInputs"
case UpdateDoobie => "saveInputs"
case FindDoobie => "getInputs"
case DeleteDoobie => "deleteInputs"
}
val respondWS = WSMyCelium.ws.send(endPoint :: Nil, payload, SendType.WhenConnected, 30 seconds)
respondWS.failed.foreach(println)
respondWS
}.redeem (
err => Left(err.getMessage),
{
case Right(value) =>
val unpickle = Unpickle[Either[String, (Inputs, EventsDoobieResult)]].fromBytes(value)
unpickle match {
case Right((newItem, eventReturn)) =>
println(s"Regreso de $event")
println(s"Hizo un $eventReturn")
val checarEvent = if (eventReturn == DeletedDoobie) FoundDoobie() else eventReturn
Right((oldItem, newItem, checarEvent))
//Right((oldItem, newItem, eventReturn))
case Left(errors) => Left(errors)
}
case Left(failure) => Left(failure)
}
)
}
}
val processSideEffects = onEventDocument
.dump("Changos en el processSideEffects")
.map { ver =>
println("Changos en el processSideEfects :")
println(s"Lo que va ha imprimir: $ver")
ver match {
case Right((oldItem, newItem, event)) =>
event match {
case FoundDoobie(x) => x
case SavedDoobie(x) => x
case DeletedDoobie(x) => x
case NotFoundDoobie(x) => x
case ErrorDoobie(x) => x
}
case Left(error) => error
}
}
val obsIsNew = onEventDocument.map {
case Right((oldItem, newItem, event)) =>
println("En el obsIsNew ****************")
event match {
case SavedDoobie(_) | FoundDoobie(_) => false
case DeletedDoobie(_) => true
case NotFoundDoobie(_) | ErrorDoobie(_) => true
}
case Left(error) => false
}
val obsMainItem = onEventDocument.map {
case Right((oldItem, newItem, event)) =>
println("En el obsMainItem ****************")
event match {
case NotFoundDoobie(msg) => oldItem
case _ => newItem
}
case Left(error) => Inputs()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment