Skip to content

Commit

Permalink
fix: add async-mutex to builds on large vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Aug 20, 2023
1 parent b99d4cd commit e65ea48
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@clack/prompts": "^0.6.3",
"@floating-ui/dom": "^1.4.0",
"@napi-rs/simple-git": "^0.1.8",
"async-mutex": "^0.4.0",
"chalk": "^4.1.2",
"chokidar": "^3.5.3",
"cli-spinner": "^0.2.10",
Expand Down
4 changes: 4 additions & 0 deletions quartz/bootstrap-cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import http from "http"
import serveHandler from "serve-handler"
import { WebSocketServer } from "ws"
import { randomUUID } from "crypto"
import { Mutex } from "async-mutex"

const ORIGIN_NAME = "origin"
const UPSTREAM_NAME = "upstream"
Expand Down Expand Up @@ -391,8 +392,10 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.
],
})

const buildMutex = new Mutex()
const timeoutIds = new Set()
const build = async (clientRefresh) => {
await buildMutex.acquire()
const result = await ctx.rebuild().catch((err) => {
console.error(`${chalk.red("Couldn't parse Quartz configuration:")} ${fp}`)
console.log(`Reason: ${chalk.grey(err)}`)
Expand All @@ -415,6 +418,7 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.
const { default: buildQuartz } = await import(cacheFile + `?update=${randomUUID()}`)
await buildQuartz(argv, clientRefresh)
clientRefresh()
buildMutex.release()
}

const rebuild = (clientRefresh) => {
Expand Down
12 changes: 8 additions & 4 deletions quartz/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Argv, BuildCtx } from "./util/ctx"
import { glob, toPosixPath } from "./util/glob"
import { trace } from "./util/trace"
import { options } from "./util/sourcemap"
import { Mutex } from "async-mutex"

async function buildQuartz(argv: Argv, clientRefresh: () => void) {
const ctx: BuildCtx = {
Expand Down Expand Up @@ -77,10 +78,11 @@ async function startServing(
}

const initialSlugs = ctx.allSlugs
let timeoutIds: Set<ReturnType<typeof setTimeout>> = new Set()
let toRebuild: Set<FilePath> = new Set()
let toRemove: Set<FilePath> = new Set()
let trackedAssets: Set<FilePath> = new Set()
const buildMutex = new Mutex()
const timeoutIds: Set<ReturnType<typeof setTimeout>> = new Set()
const toRebuild: Set<FilePath> = new Set()
const toRemove: Set<FilePath> = new Set()
const trackedAssets: Set<FilePath> = new Set()
async function rebuild(fp: string, action: "add" | "change" | "delete") {
// don't do anything for gitignored files
if (ignored(fp)) {
Expand Down Expand Up @@ -111,6 +113,7 @@ async function startServing(
// debounce rebuilds every 250ms
timeoutIds.add(
setTimeout(async () => {
await buildMutex.acquire()
const perf = new PerfTimer()
console.log(chalk.yellow("Detected change, rebuilding..."))
try {
Expand Down Expand Up @@ -143,6 +146,7 @@ async function startServing(
clientRefresh()
toRebuild.clear()
toRemove.clear()
buildMutex.release()
}, 250),
)
}
Expand Down
4 changes: 2 additions & 2 deletions quartz/components/RecentNotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export default ((userOpts?: Partial<Options>) => {
const opts = { ...defaultOptions, ...userOpts }
function RecentNotes(props: QuartzComponentProps) {
const { allFiles, fileData, displayClass } = props
const pages = allFiles.filter(opts.filter).sort(opts.sort).slice(0, opts.limit)
const pages = allFiles.filter(opts.filter).sort(opts.sort)
const remaining = Math.max(0, pages.length - opts.limit)
return (
<div class={`recent-notes ${displayClass}`}>
<h3>{opts.title}</h3>
<ul class="recent-ul">
{pages.map((page) => {
{pages.slice(0, opts.limit).map((page) => {
const title = page.frontmatter?.title
const tags = page.frontmatter?.tags ?? []

Expand Down

0 comments on commit e65ea48

Please sign in to comment.