« get me outta code hell

write: live-dev-server: factor out startTiming - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-27 16:27:45 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-09-12 14:14:50 -0300
commit7924ab5deeaca905192c11a82a937d7615328b48 (patch)
tree94b9d5fdb3c0ab45bfc45c7e07025c13e1f0c5b4
parent62f1dba7653ff19ee1d38f0b79592afb052c58b4 (diff)
write: live-dev-server: factor out startTiming
-rw-r--r--src/write/build-modes/live-dev-server.js37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index 0f8f7607..fe627f6f 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -332,6 +332,25 @@ export async function go({
 
     // Other routes determined by page and URL specs
 
+    const startTiming = () => {
+      if (!showTimings) {
+        return () => '';
+      }
+
+      const timeStart = Date.now();
+
+      return () => {
+        const timeEnd = Date.now();
+        const timeDelta = timeEnd - timeStart;
+
+        if (timeDelta > 100) {
+          return `${(timeDelta / 1000).toFixed(2)}s`;
+        } else {
+          return `${timeDelta}ms`;
+        }
+      };
+    };
+
     // URL to page map expects trailing slash but no leading slash.
     const pathnameKey = pathname.replace(/^\//, '') + (pathname.endsWith('/') ? '' : '/');
 
@@ -395,7 +414,7 @@ export async function go({
         return;
       }
 
-      const timeStart = Date.now();
+      const timing = startTiming();
 
       const bound = bindUtilities({
         absoluteTo,
@@ -424,18 +443,10 @@ export async function go({
 
       const {pageHTML} = html.resolve(topLevelResult);
 
-      const timeEnd = Date.now();
-      const timeDelta = timeEnd - timeStart;
-
-      if (showTimings) {
-        const timeString =
-          (timeDelta > 100
-            ? `${(timeDelta / 1000).toFixed(2)}s`
-            : `${timeDelta}ms`);
-
-        console.log(`${requestHead} [200, ${timeString}] ${pathname} (${colors.blue(`page`)})`);
-      } else if (loudResponses) {
-        console.log(`${requestHead} [200] ${pathname} (${colors.blue(`page`)})`);
+      const timeString = timing();
+      const status = (timeString ? `200 ${timeString}` : `200`);
+      if (showTimings || loudResponses) {
+        console.log(`${requestHead} [${status}] ${pathname} (${colors.blue(`page`)})`);
       }
 
       response.writeHead(200, contentTypeHTML);