3

This is related to binding-scala-strategy-to-avoid-too-many-dom-tree-updates

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

I have no control on how many entries are sent. So if there are a lot of entries the screen freezes.

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

What is the way to restrict the length of the entries (Vars) or what is the best strategy to drop the first entry of a Vars if the maximum length is reached?

pme
  • 14,156
  • 3
  • 52
  • 95

1 Answers1

3

A couple options:

  • If you don't need to keep all the data around, simply replace it in the ListBuffer: https://scalafiddle.io/sf/fsaJbkc/2

    if (entries.value.length > display)
      entries.value.remove(0, entries.value.length - display)
    entries.value += (0 to 1000).map(_=>Random.nextInt(9)).mkString("")
    
  • If you do need to keep all the data around but want to display a subset, use another Binding based off the first: https://scalafiddle.io/sf/i75YiYN/2

    val displayedEntries = Binding {
      val allEntries = entries.bind
      if (allEntries.length > display)
        allEntries.drop(allEntries.length - display).toList
      else
        allEntries.toList
    }
    

No change here: entries.value += (0 to 1000).map(_=>Random.nextInt(9)).mkString("")

pme
  • 14,156
  • 3
  • 52
  • 95
kahliburke
  • 66
  • 2
  • `entries.bind` is deprecated. It can be replaced to `entries.all.bind`. – Yang Bo Dec 08 '18 at 15:26
  • Unfortunately, `entries.all.bind` is not perfect because it suppress the ability of partial rendering. – Yang Bo Dec 08 '18 at 15:28
  • Ideally, there should be method to create bindable slices: `def slice[A](bindingSeq: BindingSeq[A], from: Binding[Int], until: Binding[Int]): BindingSeq[A]` – Yang Bo Dec 08 '18 at 15:29
  • Feel free to create a PR for the feature of bindable slices. It should be similar to [`BindingSeq.length`](https://github.com/ThoughtWorksInc/Binding.scala/blob/2513682d44de2deb0087e0ee536c4e47801263cc/Binding/src/main/scala/com/thoughtworks/binding/Binding.scala#L999) – Yang Bo Dec 08 '18 at 15:31