I am trying to write a typed façade for my library Paths.js, following the official guide.
What I would like to be able to translate is a call like the following:
var Polygon = require('paths-js/Polygon');
var polygon = Polygon({
points: [[1, 3], [2, 5], [3, 4], [2, 0]],
closed: true
});
into
val polygon = Polygon(points = List((1, 3), (2, 5), (5, 6)), closed = true)
but I am not sure what I need to do to get to this point.
What I have done is something like the following
type Point = (Number, Number)
trait PolygonOpt {
val points: Array[Point]
val closed: Boolean
}
@JSName("paths.Polygon")
object Polygon extends js.Object {
def apply(options: PolygonOpt): Shape = js.native
}
Then, I can invoke it like
class Opt extends PolygonOpt {
val points: Array[Point] = Array((1, 2), (3, 4), (5, 6))
val closed = true
}
val opts = new Opt
val poly = Polygon(opts)
I have a few doubts on this:
- I am at a point where everything compiles, but the resulting javascript fails at the point of invocation. I believe that this is because I am passing an instance of
PolygonOpt
, while the runtime expects a javascript object literal - is the definition of
Point
translated into a js array with two components? - I would like to be able to overload
Polygon.apply
likedef apply(points: Seq[Point], closed: Boolean): Shape
, but scala.js does not let me write method implementation insidePolygon
since it extendsjs.Object
Moreover, I have both a version of my library using common.js (which is split into several files, one for each component) and another one which can be used as a single <script>
tag, putting everything under the namespace paths
(which is the one I am using right now).
Which one works better for a Scala.js wrapper?