Scala.Rx与ScalaTags示例编译错误

时间:2016-03-02 16:17:39

标签: scala scala.js scala.rx scalatags

我尝试了scala.js示例https://github.com/lihaoyi/hands-on-scala-js及其scala.rx与scalatags部分在高级部分。 示例编译运行正常,但是当我尝试使用最新的scala.rx v 0.3.1时,我得到以下编译错误:

[info] Compiling 19 Scala sources to /home/code/workspace/hands-on-scala-js-master/examples/demos/target/scala-2.11/classes...
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:14: 
[error] This Rx might leak! Either explicitly mark it unsafe (Rx.unsafe) or make an implicit RxCtx available
[error] in the enclosing scope, for example, by adding (implicit ctx: Ctx.Owner) to line 12: method main
[error]     val numChars = Rx{txt().length}
[error]                      ^
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:15: 
[error] This Rx might leak! Either explicitly mark it unsafe (Rx.unsafe) or make an implicit RxCtx available
[error] in the enclosing scope, for example, by adding (implicit ctx: Ctx.Owner) to line 12: method main
[error]     val numWords = Rx{
[error]                      ^
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:55: 
[error] This Rx might leak! Either explicitly mark it unsafe (Rx.unsafe) or make an implicit RxCtx available
[error] in the enclosing scope, for example, by adding (implicit ctx: Ctx.Owner) to line 42: method main2
[error]       for(fruit <- fruits) yield Rx {
[error]                                     ^
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:65: diverging implicit expansion for type Nothing => scalatags.JsDom.Frag
[error] starting with method rxFrag in object BasicRx
[error]         ul(fragments)
[error]            ^
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:72: not found: value Obs
[error]     Obs(r, skipInitial = true){
[error]     ^
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:72: not found: value skipInitial
[error]     Obs(r, skipInitial = true){
[error]            ^
[warn] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/webpage/WeatherSearch.scala:53: non-variable type argument scala.scalajs.js.Dynamic in type pattern scala.scalajs.js.Array[scala.scalajs.js.Dynamic] is unchecked since it is eliminated by erasure
[warn]         case jsonlist: js.Array[js.Dynamic] =>
[warn]                           ^
[warn] one warning found
[error] 6 errors found
[error] (demos/compile:compile) Compilation failed

似乎所有权概念已经有所改变,而且没有Obs伴侣对象。

我试图修复BasicRx.scala中的这些错误: 旧代码:

@JSExport
def main(container: html.Div) = {

新代码(泄漏修复):

@JSExport
def main(container: html.Div) = {
  implicit val ctx: Ctx.Owner = Ctx.Owner.safe() 

旧代码:

  implicit def rxFrag[T <% Frag](r: Rx[T]): Frag = {
    def rSafe: dom.Node = span(r()).render
    var last = rSafe

    Obs(r, skipInitial = true){
      val newLast = rSafe
      js.Dynamic.global.last = last
      last.parentNode.replaceChild(newLast, last)
      last = newLast
    }
    last
  }

新代码(转换修正):

  implicit def rxFrag[T <% Frag](r: Rx[T]): Frag = {
    def rSafe: dom.Node = span(r()).render
    var last = rSafe

    val thunk = () => {
      val newLast = rSafe
      js.Dynamic.global.last = last
      last.parentNode.replaceChild(newLast, last)
      last = newLast
    }
    new Obs(thunk, r)
    last
  }

但仍然会出现以下错误:

[info] Compiling 19 Scala sources to /home/code/workspace/hands-on-scala-js-master/examples/demos/target/scala-2.11/classes...
[warn] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/webpage/WeatherSearch.scala:53: non-variable type argument   scala.scalajs.js.Dynamic in type pattern scala.scalajs.js.Array[scala.scalajs.js.Dynamic] is unchecked since it is eliminated by erasure
[warn]         case jsonlist: js.Array[js.Dynamic] =>
[warn]                           ^
[error] /home/code/workspace/hands-on-scala-js-master/examples/demos/src/main/scala/advanced/BasicRx.scala:74: No implicit Ctx.Data is available here!
[error]     def rSafe: dom.Node = span(r()).render
[error]                                 ^

如何解决这个问题? 泄漏修复是否正确完成?

将scalatags更新为0.5.4。没有任何影响。

1 个答案:

答案 0 :(得分:4)

我帮助了scala.rx的0.3.x分支 - 是的,api很大程度上从0.2.x改变了。但是,我还在这里维护一个更新版本的“框架”代码:

https://github.com/Voltir/framework.rx/blob/master/src/main/scala/framework/Framework.scala

另外,事实证明,在使用js.App及其def main()的情况下,获取Ctx.Owner的正确方法是使用import rx.Ctx.Owner.Unsafe._。只要每个页面加载对主函数进行有限次数(即一次)的计算,就可以安全地进行“泄漏”。您对Ctx.Owner.safe()的使用不会起作用,因为编译器无法证明main只被称为有限次数。

所有这一切的完整示例可以在这个库的演示项目中找到: https://github.com/Voltir/route.rx