Thief icon indicating copy to clipboard operation
Thief copied to clipboard

小说模式下翻页会卡

Open chenlongming opened this issue 4 years ago • 7 comments

打开一段时间后, 小说翻页的时候会有明显的延迟

chenlongming avatar Jun 05 '20 08:06 chenlongming

+1macOs 10.15.5 老板键不卡翻页卡

hidohuang avatar Jun 23 '20 01:06 hidohuang

+1

SYSUcarey avatar Sep 17 '20 06:09 SYSUcarey

+1

fangyuanma avatar Oct 28 '20 03:10 fangyuanma

+1

zalmon-sources avatar Oct 30 '20 08:10 zalmon-sources

不知道你们用的是不是免费版本,我看了一下源代码,翻页每次都要对TXT进行读取和分割,这就很蛋疼了。

我修改了一下小说的逻辑,小说内容读取后,把这些数据都保存起来,只要文件路径不发生改变,就不会重新读取数据。这样翻页的速度像是飞起来一样。


'use strict';

import fs from "fs"
import db from "./db"
import iconv from "iconv-lite"

export default {
    data() {
        return {
            curr_page_number: 1,
            page_size: 50,
            page: 0,
            start: 0,
            end: this.page_size,
            filePath: "",
            oldFilePath: "",
            errCode: false,
            fileCache: "",
        };
    },
    getSize(text) {
        let size = text.length;
        this.page = Math.ceil(size / this.page_size);
    },
    getFileName() {
        var file_name = this.filePath.split("/").pop();
    },
    getPage(type) {
        let curr_page = db.get("current_page");
        var page = 0;

        if (type === "previous") {
            if (curr_page <= 1) {
                page = 1;
            } else {
                page = curr_page - 1;
            }
        } else if (type === "next") {
            if (curr_page >= this.page) {
                page = this.page;
            } else {
                page = curr_page + 1;
            }
        } else if (type === "curr") {
            page = curr_page;
        }

        this.curr_page_number = page;
    },
    updatePage() {
        db.set("current_page", this.curr_page_number)
    },
    getStartEnd() {
        this.start = this.curr_page_number * this.page_size;
        this.end = this.curr_page_number * this.page_size - this.page_size;
    },
    readFile() {
        if (this.filePath === "" || typeof (this.filePath) === "undefined") {
            return "请选择TXT小说路径"
        }

        if (this.filePath !== this.oldFilePath) {
            try {
                var data = fs.readFileSync(this.filePath);

                if (this.errCode) {
                    data = iconv.decode(data, 'gb2312');
                } else {
                    data = iconv.decode(data, 'utf-8');
                }
                this.oldFilePath = this.filePath
                var line_break = db.get("line_break");
                data = data.toString().replace(/\n/g, line_break).replace(/\r/g, " ").replace(/  /g, " ").replace(/ /g, " ");
                this.fileCache = data
            } catch (error) {
                return "TXT小说路径不存在或路径不正确"
            }
        }

        return this.fileCache
    },
    init() {
        this.filePath = db.get("current_file_path");
        this.errCode = db.get("errCodeChecked");
        var is_english = db.get("is_english");
        var curr_model = db.get("curr_model");

        if (is_english === true) {
            if (curr_model === "1") {
                this.page_size = db.get("page_size");
            } else {
                this.page_size = db.get("page_size");
            }
        } else {
            if (curr_model === "1") {
                this.page_size = db.get("page_size");
            } else {
                this.page_size = db.get("page_size");
            }
        }
    },
    soText(so) {
        this.init();
        // 小说搜索
        let text = this.readFile();
        this.getSize(text);

        // 存储搜索结果
        var soResult = [];

        // 正则
        var re = new RegExp(so, "g");
        var result = "";

        do {
            try {
                result = re.exec(text);

                // 分页位置
                var page = Math.ceil(result.index / this.page_size);

                // 附近内容
                var textx = text.substring(result.index - 30, result.index + 31)

                // 加入结果 数组
                soResult.push({
                    index: result.index,
                    page: page,
                    text: textx
                })
            } catch (error) { }
        }
        while (result != null)

        return soResult;
    },
    makePage(text) {
        this.getStartEnd();
        this.updatePage();
        if (db.get("is_display_page")) {
            var page_info = this.curr_page_number.toString() + "/" + this.page.toString();
            return text.substring(this.start, this.end) + "    " + page_info;
        } else {
            return text.substring(this.start, this.end)
        }
    },
    getPreviousPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("previous");
        return this.makePage(text);
    },
    getNextPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("next");
        return this.makePage(text);
    },
    getJumpingPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("curr");
        return this.makePage(text);
    }
};

PR 在这里 https://github.com/cteamx/Thief/pull/108/commits/7845d2489c7162fd64bc99ea2f1c0a4826a5766a

leocxy avatar Mar 12 '21 21:03 leocxy

不知道你们用的是不是免费版本,我看了一下源代码,翻页每次都要对TXT进行读取和分割,这就很蛋疼了。

我修改了一下小说的逻辑,小说内容读取后,把这些数据都保存起来,只要文件路径不发生改变,就不会重新读取数据。这样翻页的速度像是飞起来一样。

'use strict';

import fs from "fs"
import db from "./db"
import iconv from "iconv-lite"

export default {
    data() {
        return {
            curr_page_number: 1,
            page_size: 50,
            page: 0,
            start: 0,
            end: this.page_size,
            filePath: "",
            oldFilePath: "",
            errCode: false,
            fileCache: "",
        };
    },
    getSize(text) {
        let size = text.length;
        this.page = Math.ceil(size / this.page_size);
    },
    getFileName() {
        var file_name = this.filePath.split("/").pop();
    },
    getPage(type) {
        let curr_page = db.get("current_page");
        var page = 0;

        if (type === "previous") {
            if (curr_page <= 1) {
                page = 1;
            } else {
                page = curr_page - 1;
            }
        } else if (type === "next") {
            if (curr_page >= this.page) {
                page = this.page;
            } else {
                page = curr_page + 1;
            }
        } else if (type === "curr") {
            page = curr_page;
        }

        this.curr_page_number = page;
    },
    updatePage() {
        db.set("current_page", this.curr_page_number)
    },
    getStartEnd() {
        this.start = this.curr_page_number * this.page_size;
        this.end = this.curr_page_number * this.page_size - this.page_size;
    },
    readFile() {
        if (this.filePath === "" || typeof (this.filePath) === "undefined") {
            return "请选择TXT小说路径"
        }

        if (this.filePath !== this.oldFilePath) {
            try {
                var data = fs.readFileSync(this.filePath);

                if (this.errCode) {
                    data = iconv.decode(data, 'gb2312');
                } else {
                    data = iconv.decode(data, 'utf-8');
                }
                this.oldFilePath = this.filePath
                var line_break = db.get("line_break");
                data = data.toString().replace(/\n/g, line_break).replace(/\r/g, " ").replace(/  /g, " ").replace(/ /g, " ");
                this.fileCache = data
            } catch (error) {
                return "TXT小说路径不存在或路径不正确"
            }
        }

        return this.fileCache
    },
    init() {
        this.filePath = db.get("current_file_path");
        this.errCode = db.get("errCodeChecked");
        var is_english = db.get("is_english");
        var curr_model = db.get("curr_model");

        if (is_english === true) {
            if (curr_model === "1") {
                this.page_size = db.get("page_size");
            } else {
                this.page_size = db.get("page_size");
            }
        } else {
            if (curr_model === "1") {
                this.page_size = db.get("page_size");
            } else {
                this.page_size = db.get("page_size");
            }
        }
    },
    soText(so) {
        this.init();
        // 小说搜索
        let text = this.readFile();
        this.getSize(text);

        // 存储搜索结果
        var soResult = [];

        // 正则
        var re = new RegExp(so, "g");
        var result = "";

        do {
            try {
                result = re.exec(text);

                // 分页位置
                var page = Math.ceil(result.index / this.page_size);

                // 附近内容
                var textx = text.substring(result.index - 30, result.index + 31)

                // 加入结果 数组
                soResult.push({
                    index: result.index,
                    page: page,
                    text: textx
                })
            } catch (error) { }
        }
        while (result != null)

        return soResult;
    },
    makePage(text) {
        this.getStartEnd();
        this.updatePage();
        if (db.get("is_display_page")) {
            var page_info = this.curr_page_number.toString() + "/" + this.page.toString();
            return text.substring(this.start, this.end) + "    " + page_info;
        } else {
            return text.substring(this.start, this.end)
        }
    },
    getPreviousPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("previous");
        return this.makePage(text);
    },
    getNextPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("next");
        return this.makePage(text);
    },
    getJumpingPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("curr");
        return this.makePage(text);
    }
};

PR 在这里 7845d24

源码怎么跑起来呢,现在用的都是 4.0 Tag 的产物,作者短期内估计不会处理PR

SYSUcarey avatar Mar 22 '21 09:03 SYSUcarey

不知道你们用的是不是免费版本,我看了一下源代码,翻页每次都要对TXT进行读取和分割,这就很蛋疼了。 我修改了一下小说的逻辑,小说内容读取后,把这些数据都保存起来,只要文件路径不发生改变,就不会重新读取数据。这样翻页的速度像是飞起来一样。

'use strict';

import fs from "fs"
import db from "./db"
import iconv from "iconv-lite"

export default {
    data() {
        return {
            curr_page_number: 1,
            page_size: 50,
            page: 0,
            start: 0,
            end: this.page_size,
            filePath: "",
            oldFilePath: "",
            errCode: false,
            fileCache: "",
        };
    },
    getSize(text) {
        let size = text.length;
        this.page = Math.ceil(size / this.page_size);
    },
    getFileName() {
        var file_name = this.filePath.split("/").pop();
    },
    getPage(type) {
        let curr_page = db.get("current_page");
        var page = 0;

        if (type === "previous") {
            if (curr_page <= 1) {
                page = 1;
            } else {
                page = curr_page - 1;
            }
        } else if (type === "next") {
            if (curr_page >= this.page) {
                page = this.page;
            } else {
                page = curr_page + 1;
            }
        } else if (type === "curr") {
            page = curr_page;
        }

        this.curr_page_number = page;
    },
    updatePage() {
        db.set("current_page", this.curr_page_number)
    },
    getStartEnd() {
        this.start = this.curr_page_number * this.page_size;
        this.end = this.curr_page_number * this.page_size - this.page_size;
    },
    readFile() {
        if (this.filePath === "" || typeof (this.filePath) === "undefined") {
            return "请选择TXT小说路径"
        }

        if (this.filePath !== this.oldFilePath) {
            try {
                var data = fs.readFileSync(this.filePath);

                if (this.errCode) {
                    data = iconv.decode(data, 'gb2312');
                } else {
                    data = iconv.decode(data, 'utf-8');
                }
                this.oldFilePath = this.filePath
                var line_break = db.get("line_break");
                data = data.toString().replace(/\n/g, line_break).replace(/\r/g, " ").replace(/  /g, " ").replace(/ /g, " ");
                this.fileCache = data
            } catch (error) {
                return "TXT小说路径不存在或路径不正确"
            }
        }

        return this.fileCache
    },
    init() {
        this.filePath = db.get("current_file_path");
        this.errCode = db.get("errCodeChecked");
        var is_english = db.get("is_english");
        var curr_model = db.get("curr_model");

        if (is_english === true) {
            if (curr_model === "1") {
                this.page_size = db.get("page_size");
            } else {
                this.page_size = db.get("page_size");
            }
        } else {
            if (curr_model === "1") {
                this.page_size = db.get("page_size");
            } else {
                this.page_size = db.get("page_size");
            }
        }
    },
    soText(so) {
        this.init();
        // 小说搜索
        let text = this.readFile();
        this.getSize(text);

        // 存储搜索结果
        var soResult = [];

        // 正则
        var re = new RegExp(so, "g");
        var result = "";

        do {
            try {
                result = re.exec(text);

                // 分页位置
                var page = Math.ceil(result.index / this.page_size);

                // 附近内容
                var textx = text.substring(result.index - 30, result.index + 31)

                // 加入结果 数组
                soResult.push({
                    index: result.index,
                    page: page,
                    text: textx
                })
            } catch (error) { }
        }
        while (result != null)

        return soResult;
    },
    makePage(text) {
        this.getStartEnd();
        this.updatePage();
        if (db.get("is_display_page")) {
            var page_info = this.curr_page_number.toString() + "/" + this.page.toString();
            return text.substring(this.start, this.end) + "    " + page_info;
        } else {
            return text.substring(this.start, this.end)
        }
    },
    getPreviousPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("previous");
        return this.makePage(text);
    },
    getNextPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("next");
        return this.makePage(text);
    },
    getJumpingPage() {
        this.init();
        let text = this.readFile();
        this.getSize(text);
        this.getPage("curr");
        return this.makePage(text);
    }
};

PR 在这里 7845d24

源码怎么跑起来呢,现在用的都是 4.0 Tag 的产物,作者短期内估计不会处理PR

就直接跑起来的

OS: MacOS
Node: v10.20.1
Yarn: 1.22.10

你不介意的话可以留个邮箱,告诉我你是什么OS,我打个包给你发过去……

leocxy avatar Mar 22 '21 20:03 leocxy