Skip to content

Commit

Permalink
Replace macros with wrappers on HttpPromise
Browse files Browse the repository at this point in the history
* Contributes to #23
  • Loading branch information
jokade committed Jan 30, 2015
1 parent 8fceee3 commit ee6adee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ lazy val root = project.in(file(".")).
name := "scalajs-angulate",
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"org.scala-js" %%% "scalajs-dom" % "0.7.1-SNAPSHOT"
"org.scala-js" %%% "scalajs-dom" % "0.7.0"
),
resolvers += Resolver.sonatypeRepo("releases")
)
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0-RC1")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0-RC2")

addSbtPlugin("com.lihaoyi" % "utest-js-plugin" % "0.2.5-RC1")
35 changes: 11 additions & 24 deletions src/main/scala/biz/enef/angular/core/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,48 +74,35 @@ object HttpConfig {
trait HttpPromise[T] extends js.Object {
def success(callback: js.Function): HttpPromise[T] = js.native

def success(callback: js.Function1[js.Any, Unit]): this.type = js.native
def success(callback: js.Function1[js.Any, Unit]): HttpPromise[T] = js.native

def success(callback: js.Function2[js.Any, Int, Unit]): this.type = js.native
def success(callback: js.Function2[js.Any, Int, Unit]): HttpPromise[T] = js.native

def success(callback: js.Function3[js.Any, js.Any, Int, Unit]): this.type = js.native
def success(callback: js.Function3[js.Any, js.Any, Int, Unit]): HttpPromise[T] = js.native

def success(callback: js.Function4[js.Any, Int, js.Any, js.Any, Unit]): this.type = js.native
def success(callback: js.Function4[js.Any, Int, js.Any, js.Any, Unit]): HttpPromise[T] = js.native

def success(callback: js.Function5[js.Any, Int, js.Any, js.Any, js.Any, Unit]): this.type = js.native
def success(callback: js.Function5[js.Any, Int, js.Any, js.Any, js.Any, Unit]): HttpPromise[T] = js.native

def error(callback: js.Function): HttpPromise[T] = js.native

def error(callback: js.Function1[js.Any, Unit]): this.type = js.native
def error(callback: js.Function1[js.Any, Unit]): HttpPromise[T] = js.native

def error(callback: js.Function2[js.Any, Int, Unit]): this.type = js.native
def error(callback: js.Function2[js.Any, Int, Unit]): HttpPromise[T] = js.native

def error(callback: js.Function3[js.Any, js.Any, Int, Unit]): this.type = js.native
def error(callback: js.Function3[js.Any, js.Any, Int, Unit]): HttpPromise[T] = js.native

def error(callback: js.Function4[js.Any, Int, js.Any, js.Any, Unit]): this.type = js.native
def error(callback: js.Function4[js.Any, Int, js.Any, js.Any, Unit]): HttpPromise[T] = js.native

def error(callback: js.Function5[js.Any, Int, js.Any, js.Any, UndefOr[String], Unit]): this.type = js.native
def error(callback: js.Function5[js.Any, Int, js.Any, js.Any, UndefOr[String], Unit]): HttpPromise[T] = js.native

var `then`: js.Function3[js.Function, js.Function, js.Function, HttpPromise[T]] = js.native
//var `then`: js.Function3[js.Function1[T,Unit],js.Function,js.Function,Unit] = js.native
}

object HttpPromise {
//------------------------- ANGULATE ENHANCEMENTS --------------------------
final implicit class RichHttpPromise[T](self: HttpPromise[T]) {
/**
* The provided callback will be called when a successful response is available
* (response status codes between 200 and 299 are considered a success status).
*
* @note This is a scalajs-angulate enhancement. The response data provided to
* the callback will be cast to the specified type T.
*
* @param callback Callback function that will receive the response data as its first argument
* @tparam T Type of the response data object
*/
//def success[T](callback: (T) => Unit) : this.type = macro impl.HttpPromiseMacros.onSuccessWithType[T]

//def onSuccess(pf: PartialFunction[Any,Unit]) : Unit = macro impl.HttpPromiseMacros.onSuccess
final implicit class RichHttpPromise[T](val self: HttpPromise[T]) extends AnyVal {

def onSuccess(f: T => Unit): HttpPromise[T] = macro impl.HttpPromiseMacros.onSuccess

Expand Down
14 changes: 10 additions & 4 deletions src/main/scala/biz/enef/angular/core/impl/HttpMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,40 @@ protected[angular] class HttpPromiseMacros(val c: blackbox.Context) extends Macr

def onSuccess(f: c.Tree) = {
val T = f.tpe.typeArgs.head
val self = Select(c.prefix.tree, TermName("self"))
val tree = q"""{val fun = $f
${c.prefix}.success( (x: js.UndefOr[$T]) => fun(x.getOrElse(null)) )}"""
$self.success( (x: js.UndefOr[$T]) => fun(x.getOrElse(null)) )}"""

if(logCode) printCode(tree)
tree
}

def onComplete(f: c.Tree) = {
val T = f.tpe.typeArgs.head.typeArgs.head

val self = Select(c.prefix.tree, TermName("self"))

val tree = q"""{val fun = $f
${c.prefix}.success( ((x:js.UndefOr[$T]) =>fun($tSuccess(x.getOrElse(null)))) ).
$self.success( ((x:js.UndefOr[$T]) =>fun($tSuccess(x.getOrElse(null)))) ).
error( (msg:Any,status:Int)=>fun($tFailure(new $tHttpError(msg.toString,status))) )
}"""
if(logCode) printCode(tree)
tree
}

def onFailure(f: c.Tree) = {
val tree = q"""${c.prefix}.error( (msg:Any,status:Int)=>$f(new $tHttpError(msg.toString,status)))"""
val self = Select(c.prefix.tree, TermName("self"))
val tree = q"""$self.error( (msg:Any,status:Int)=>$f(new $tHttpError(msg.toString,status)))"""

if(logCode) printCode(tree)
tree
}

def map(f: c.Tree) = {
val T = f.tpe.typeArgs(1)
val self = Select(c.prefix.tree, TermName("self"))
val tree = q"""{import biz.enef.angular.core
val mapped = new core.impl.MappedHttpPromise(${c.prefix},$f)
val mapped = new core.impl.MappedHttpPromise($self,$f)
mapped.asInstanceOf[core.HttpPromise[$T]]
}
"""
Expand Down

0 comments on commit ee6adee

Please sign in to comment.