3

我在高级部分尝试了 scala.js 示例https://github.com/lihaoyi/hands-on-scala-js及其带有 scalatags 部分的 scala.rx。示例编译运行良好,但是当我尝试使用最新的 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]                                 ^

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

将 scalatag 更新到 0.5.4。没有任何影响。

4

1 回答 1

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()获取 a 的正确方法Ctx.Owner是使用import rx.Ctx.Owner.Unsafe._. 只要每次页面加载对该主函数进行有限次数的评估(即一次),允许“泄漏”是安全的。您的使用Ctx.Owner.safe()不会起作用,因为编译器无法证明 main 只被调用了有限次。

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

于 2016-03-02T18:35:50.257 回答