Skip to content

Commit e1ccbd4

Browse files
committed
✨ 同じscrapbox.Page.linesの再生成コストをなくす関数を追加
1 parent 45d200f commit e1ccbd4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

browser/dom/getCachedLines.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Line, Scrapbox } from "../../deps/scrapbox.ts";
2+
declare const scrapbox: Scrapbox;
3+
4+
let isLatestData = false;
5+
let lines: typeof scrapbox.Page.lines = null;
6+
7+
scrapbox.addListener("lines:changed", () => isLatestData = false);
8+
scrapbox.addListener("layout:changed", () => isLatestData = false);
9+
10+
/** scrapbox.Page.linesをcacheして取得する
11+
*
12+
* scrapbox.Page.linesの生成には時間がかかるので、実際に必要になるまで呼び出さないようにする
13+
*
14+
* @return `scrapbox.Page.lines`と同じ。常に最新のものが返される
15+
*/
16+
export const getCachedLines = (): readonly Line[] | null => {
17+
if (!isLatestData) {
18+
lines = scrapbox.Page.lines;
19+
isLatestData = true;
20+
}
21+
return lines;
22+
};

0 commit comments

Comments
 (0)