From 4e42d52e166dcc3c62775cb3bf86c209d098c158 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Mon, 21 Aug 2023 03:47:07 +0000
Subject: [PATCH] fix: ctrl + k breaking after page nav

---
 quartz/bootstrap-cli.mjs |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/quartz/bootstrap-cli.mjs b/quartz/bootstrap-cli.mjs
index ab63286..616f2ef 100755
--- a/quartz/bootstrap-cli.mjs
+++ b/quartz/bootstrap-cli.mjs
@@ -16,6 +16,7 @@
 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"
@@ -391,8 +392,10 @@
       ],
     })
 
+    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)}`)
@@ -415,17 +418,17 @@
       const { default: buildQuartz } = await import(cacheFile + `?update=${randomUUID()}`)
       await buildQuartz(argv, clientRefresh)
       clientRefresh()
+      buildMutex.release()
     }
 
     const rebuild = (clientRefresh) => {
       timeoutIds.forEach((id) => clearTimeout(id))
+      timeoutIds.clear()
       timeoutIds.add(setTimeout(() => build(clientRefresh), 250))
     }
 
     if (argv.serve) {
-      const wss = new WebSocketServer({ port: 3001 })
       const connections = []
-      wss.on("connection", (ws) => connections.push(ws))
       const clientRefresh = () => connections.forEach((conn) => conn.send("rebuild"))
 
       if (argv.baseDir !== "" && !argv.baseDir.startsWith("/")) {
@@ -510,6 +513,8 @@
         return serve()
       })
       server.listen(argv.port)
+      const wss = new WebSocketServer({ port: 3001 })
+      wss.on("connection", (ws) => connections.push(ws))
       console.log(
         chalk.cyan(
           `Started a Quartz server listening at http://localhost:${argv.port}${argv.baseDir}`,

--
Gitblit v1.10.0