perf: memoize filetree computation (#490)

* perf: memoize filetree computation

* format

* var -> let
This commit is contained in:
Jacky Zhao 2023-09-20 16:09:18 -07:00 committed by GitHub
parent 16d33fb771
commit 48452231d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ import explorerStyle from "./styles/explorer.scss"
// @ts-ignore
import script from "./scripts/explorer.inline"
import { ExplorerNode, FileNode, Options } from "./ExplorerNode"
import { QuartzPluginData } from "../plugins/vfile"
// Options interface defined in `ExplorerNode` to avoid circular dependency
const defaultOptions = {
@ -27,12 +28,17 @@ const defaultOptions = {
} satisfies Options
export default ((userOpts?: Partial<Options>) => {
function Explorer({ allFiles, displayClass, fileData }: QuartzComponentProps) {
// Parse config
const opts: Options = { ...defaultOptions, ...userOpts }
// memoized
let fileTree: FileNode
let jsonTree: string
function constructFileTree(allFiles: QuartzPluginData[]) {
if (!fileTree) {
// Construct tree from allFiles
const fileTree = new FileNode("")
fileTree = new FileNode("")
allFiles.forEach((file) => fileTree.add(file, 1))
/**
@ -68,8 +74,12 @@ export default ((userOpts?: Partial<Options>) => {
const folders = fileTree.getFolderPaths(opts.folderDefaultState === "collapsed")
// Stringify to pass json tree as data attribute ([data-tree])
const jsonTree = JSON.stringify(folders)
jsonTree = JSON.stringify(folders)
}
}
function Explorer({ allFiles, displayClass, fileData }: QuartzComponentProps) {
constructFileTree(allFiles)
return (
<div class={`explorer ${displayClass}`}>
<button