Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jhegedus42/c6ef9257d828e99292778f3338f93e7d to your computer and use it in GitHub Desktop.
Save jhegedus42/c6ef9257d828e99292778f3338f93e7d to your computer and use it in GitHub Desktop.
entity
object ClientRestAJAX {
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
def getEntity[E <: Entity: ClassTag: Decoder](
ref: Ref[E]): Future[HttpGetRequestResult[E]] = {
val url: GetURL = ref
val url_str = url.toURLWithHostAsString
val f: Future[XMLHttpRequest] = Ajax.get(url_str)
println("i am running")
val fr: Future[HttpGetRequestResult[E]] = f
.map(_.responseText)
.map((x: String) => {
println(" i am in future")
println(x)
def isRefValsTypeValid[E <: Entity](rv: RefVal[E]): Boolean = {
rv.r.entityType == EntityType.make
}
// todolater check if returned RefVal's type (RefVal's validity), that it matches the type of E
// use isRefValsTypeValid
import io.circe._
import io.circe.generic.auto._
import io.circe.parser._
val r: Either[Error, HttpGetRequestResult[E]] =
decode[HttpGetRequestResult[E]](x)
println(r)
r
})
.map(x => x.right.get)
// todolater, get rid of this unsafe .right.get
// flatten these nested Eithers - if the ajax call gets fucked up, merge that into the
// returned Either Type...
fr
}
def getEntityDyn(refDyn: RefDyn): Future[HttpGetRequestResultDyn] = {
def g[E <: Entity: ClassTag]
: HttpGetRequestResult[E] => HttpGetRequestResultDyn = (hgr:HttpGetRequestResult[E]) => hgr.map(RefValDyn.fromRefVal(_))
def getCase[E <: Entity: ClassTag](): EntityType = {
EntityType.make[E]
}
def res[E <: Entity: ClassTag: Decoder](
refDyn: RefDyn): Future[HttpGetRequestResultDyn] = {
val refV: \/[TypeIsIncorrectError, Ref[E]] = refDyn.toRef[E]()
val ref: Ref[E] = refV.toEither.right.get
(getEntity[E](ref)).map(x => g[E](implicitly[ClassTag[E]])(x))
}
import io.circe.generic.auto._
refDyn.et match {
case s if s == getCase[Line] =>
res[Line](refDyn) // todolater abstract this more
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment