Browse Source

Оптимизация (прекеширование) при скроллинге

Book Pauk 6 years ago
parent
commit
e797cedf99
1 changed files with 24 additions and 4 deletions
  1. 24 4
      client/components/Reader/TextPage/TextPage.vue

+ 24 - 4
client/components/Reader/TextPage/TextPage.vue

@@ -434,6 +434,7 @@ class TextPage extends Vue {
         await this.$nextTick();
         await this.$nextTick();
         await sleep(50);
         await sleep(50);
 
 
+        this.cachedPos = -1;
         const page = this.$refs.scrollingPage;
         const page = this.$refs.scrollingPage;
         let i = 0;
         let i = 0;
         while (!this.stopScrolling) {
         while (!this.stopScrolling) {
@@ -470,10 +471,29 @@ class TextPage extends Vue {
 
 
     draw() {
     draw() {
         if (this.doingScrolling) {
         if (this.doingScrolling) {
-            const lines = this.getLines(this.bookPos);
-            this.linesDown = lines.linesDown;
-            this.linesUp = lines.linesUp;
-            this.page1 = this.drawPage(lines.linesDown);
+            if (this.cachedPos == this.bookPos) {
+                this.linesDown = this.linesCached.linesDown;
+                this.linesUp = this.linesCached.linesUp;
+                this.page1 = this.pageCached;
+            } else {
+                const lines = this.getLines(this.bookPos);
+                this.linesDown = lines.linesDown;
+                this.linesUp = lines.linesUp;
+                this.page1 = this.drawPage(lines.linesDown);
+            }
+
+            //caching next
+            if (this.cachedPageTimer)
+                clearTimeout(this.cachedPageTimer);
+            this.cachedPageTimer = setTimeout(() => {
+                if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
+                    this.cachedPos = this.linesDown[1].begin;
+                    this.linesCached = this.getLines(this.cachedPos);
+                    this.pageCached = this.drawPage(this.linesCached.linesDown);
+                }
+                this.cachedPageTimer = null;
+            }, 20);
+
             this.debouncedDrawStatusBar();
             this.debouncedDrawStatusBar();
             return;
             return;
         }
         }