Binding.scala: Strategy to have a defined length of a Vars

117 Views Asked by At

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?

1

There are 1 best solutions below

4
kahliburke On BEST ANSWER

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("")