« get me outta code hell

Optimize getScrollPositionOfElementAtEndOfView - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-15 00:36:18 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-15 00:36:18 -0400
commitae2c9e19d72ecc701d9ce64ae44cbe1f1e77413b (patch)
tree5267b3b270d1ae5266966651716dd5537e72e319 /ui
parentd712d405f76178fc67b9421c113dccc3537e2a0c (diff)
Optimize getScrollPositionOfElementAtEndOfView
This actually drastically improves the performance of mtui when opening
very, very large playlists.
Diffstat (limited to 'ui')
-rw-r--r--ui/form/ListScrollForm.js29
1 files changed, 22 insertions, 7 deletions
diff --git a/ui/form/ListScrollForm.js b/ui/form/ListScrollForm.js
index 77bebcd..d22cd35 100644
--- a/ui/form/ListScrollForm.js
+++ b/ui/form/ListScrollForm.js
@@ -158,14 +158,21 @@ module.exports = class ListScrollForm extends Form {
   getScrollPositionOfElementAtEndOfView(element) {
     // We can decide how many items to scroll past by moving forward until
     // the item's far edge is visible.
+    const pos = this.getItemPos(element);
     let edge = this.formEdge
-    let i = 0
-    for (; i < this.inputs.length; i++) {
-      if (this.getItemPos(element) <= edge) break
+    for (let i = 0; i < this.inputs.length; i++) {
+      if (pos <= edge) {
+        return i;
+      }
 
-      edge += this.inputs[i][this.sizeProp]
+      if (this.sizeProp === 'w') {
+        edge += this.inputs[i].w;
+      } else {
+        edge += this.inputs[i].h;
+      }
     }
-    return i
+    // No result? Well, it's at the end.
+    return this.inputs.length;
   }
 
   scrollElementIntoEndOfView(element) {
@@ -199,8 +206,16 @@ module.exports = class ListScrollForm extends Form {
   getItemPos(item) {
     // Gets the position of the item in an unscrolled view.
 
-    return this.inputs.slice(0, this.inputs.indexOf(item) + 1)
-      .reduce((a, b) => a + b[this.sizeProp], 0)
+    const index = this.inputs.indexOf(item);
+    let pos = 0;
+    for (let i = 0; i <= index; i++) {
+      if (this.sizeProp === 'w') {
+        pos += this.inputs[i].w;
+      } else {
+        pos += this.inputs[i].h;
+      }
+    }
+    return pos;
   }
 
   getSizeProp() {