Binding.scala: Strategy to avoid too many dom-tree updates

[亡魂溺海] 提交于 2019-12-01 11:17:10

问题


In my project scala-adapters I display log entries that are sent over a websocket.

As I have no control on how many entries are sent, I am looking for a strategy to avoid that the screen freezes.

I created a ScalaFiddle to simulate that: https://scalafiddle.io/sf/kzr28tq

This function with these parameters works perfectly:

setInterval(1000) { // note the absence of () =>
  entries.value += (0 to 100).map(_.toString).mkString("")
}

If the interval gets smaller and the String longer - the screen freezes, e.g. with:

setInterval(100) { // note the absence of () =>
  entries.value += (0 to 10000).map(_.toString).mkString("")
}

Is there a solution to solve that on the client side - or do I have to solve that on the server side?


回答1:


You can try:

@dom
def render = {
  <div>
  {
    for (entry <- entries) yield {
      entryDiv(entry).bind
    }
  }
  </div>
}

The problem is that you bind entries too early. Binding.scala does its magic by CPS transform, every .bind triggers re-evaluation of all code after, so you should bind a variable as late as possible.

And for Vars, use for comprehension instead of bind directly, to avoid updating the whole list. When you use += to modify the content of Vars, Binding.scala "patches" the list internally, but if you do .bind on the Vars instance directly to get the whole list, the framework cannot do any optimization for you.

Here is the updated ScalaFiddle: https://scalafiddle.io/sf/kzr28tq/3



来源:https://stackoverflow.com/questions/50364461/binding-scala-strategy-to-avoid-too-many-dom-tree-updates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!