Skip to content

Commit 039472d

Browse files
authored
Merge pull request #13 from ThoughtWorksInc/thenable-and-future
Add Bindable type class for Future and Thenable
2 parents 6395639 + 01bc9d9 commit 039472d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

js/js.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
enablePlugins(Example)
22

3+
libraryDependencies += "com.thoughtworks.binding" %%% "jspromisebinding" % "11.7.0+144-dfef7165"
4+
35
libraryDependencies += "com.thoughtworks.binding" %%% "dom" % "11.7.0" % Test
46

57
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.10")

shared/build.sbt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@ libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.7" % Test
22

33
libraryDependencies += "com.thoughtworks.binding" %%% "binding" % "11.7.0"
44

5+
libraryDependencies += "com.thoughtworks.binding" %%% "futurebinding" % "11.7.0"
6+
57
libraryDependencies += "com.github.mpilquist" %%% "simulacrum" % "0.15.0"
68

79
libraryDependencies += "com.thoughtworks.enableIf" %% "enableif" % "1.1.6"
810

9-
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)
11+
// Enable macro annotation by scalac flags for Scala 2.13
12+
scalacOptions ++= {
13+
import Ordering.Implicits._
14+
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
15+
Some("-Ymacro-annotations")
16+
} else {
17+
None
18+
}
19+
}
20+
21+
// Enable macro annotation by compiler plugins for Scala 2.12
22+
libraryDependencies ++= {
23+
import Ordering.Implicits._
24+
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
25+
None
26+
} else {
27+
Some(compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full))
28+
}
29+
}

shared/src/main/scala/com/thoughtworks/binding/bindable/package.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.thoughtworks.binding
22

3+
import com.thoughtworks.binding._
34
import com.thoughtworks.binding.Binding._
5+
import com.thoughtworks.enableIf
46
import simulacrum._
57
import scala.language.implicitConversions
68

@@ -47,6 +49,9 @@ package bindable {
4749
import Jvm._
4850
import Js._
4951

52+
import scala.concurrent.{ExecutionContext, Future}
53+
import scala.util.Try
54+
5055
private[bindable] trait LowPriorityBindable0 {
5156

5257
implicit def constantBindable[Value0]: Bindable.Aux[Value0, Value0] = new Bindable[Value0] {
@@ -69,6 +74,21 @@ package bindable {
6974
def toBinding(from: Binding[Value0]): Binding[Value] = from
7075
}
7176

77+
implicit def futureBindable[Value0](
78+
implicit executionContext: ExecutionContext): Bindable.Aux[Future[Value0], Option[Try[Value0]]] =
79+
new Bindable[Future[Value0]] {
80+
type Value = Option[Try[Value0]]
81+
def toBinding(from: Future[Value0]): Binding[Value] = FutureBinding(from)
82+
}
83+
84+
@enableIf(c => c.compilerSettings.exists(_.matches("""^-Xplugin:.*scalajs-compiler_[0-9\.\-]*\.jar$""")))
85+
implicit def thenableBindable[Value0]
86+
: Bindable.Aux[scala.scalajs.js.Thenable[Value0], Option[Either[Any, Value0]]] =
87+
new Bindable[scala.scalajs.js.Thenable[Value0]] {
88+
type Value = Option[Either[Any, Value0]]
89+
def toBinding(from: scala.scalajs.js.Thenable[Value0]): Binding[Value] = JsPromiseBinding(from)
90+
}
91+
7292
}
7393

7494
/** A dependent type class that witnesses a type that can be converted to a `Binding[Value]`.

0 commit comments

Comments
 (0)