« get me outta code hell

infra: experimental HSMUSIC_DEBUG_CONTENT_PERF mode - 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-04-12 12:12:58 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-04-12 12:12:58 -0300
commitbcf97080272263019696296527cd8852d8d9f0ac (patch)
tree2f04418f7604549630ccdf91bd4dc94ac08e69b8
parentfd74ecf53ce92881460e1cf9b4c07aec76e97567 (diff)
infra: experimental HSMUSIC_DEBUG_CONTENT_PERF mode
-rw-r--r--src/content-function.js22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/content-function.js b/src/content-function.js
index 16b18641..44f8b842 100644
--- a/src/content-function.js
+++ b/src/content-function.js
@@ -1,7 +1,7 @@
 import {inspect as nodeInspect} from 'node:util';
 
 import {decorateError} from '#aggregate';
-import {colors, ENABLE_COLOR} from '#cli';
+import {colors, decorateTime, ENABLE_COLOR} from '#cli';
 import {Template} from '#html';
 import {annotateFunction, empty, setIntersection} from '#sugar';
 
@@ -9,6 +9,8 @@ function inspect(value, opts = {}) {
   return nodeInspect(value, {colors: ENABLE_COLOR, ...opts});
 }
 
+const DECORATE_TIME = process.env.HSMUSIC_DEBUG_CONTENT_PERF === '1';
+
 export class ContentFunctionSpecError extends Error {}
 
 export default function contentFunction({
@@ -104,6 +106,11 @@ export function expectDependencies({
 
   let wrappedGenerate;
 
+  const optionalDecorateTime = (prefix, fn) =>
+    (DECORATE_TIME
+      ? decorateTime(`${prefix}/${generate.name}`, fn)
+      : fn);
+
   if (isInvalidated) {
     wrappedGenerate = function() {
       throw new Error(`Generate invalidated because unfulfilled dependencies provided: ${[...invalidatingDependencyKeys].join(', ')}`);
@@ -119,7 +126,7 @@ export function expectDependencies({
     annotateFunction(wrappedGenerate, {name: generate, trait: 'unfulfilled'});
     wrappedGenerate.fulfilled = false;
   } else {
-    const callUnderlyingGenerate = ([arg1, arg2], ...extraArgs) => {
+    let callUnderlyingGenerate = ([arg1, arg2], ...extraArgs) => {
       if (hasDataFunction && !arg1) {
         throw new Error(`Expected data`);
       }
@@ -161,6 +168,9 @@ export function expectDependencies({
       }
     };
 
+    callUnderlyingGenerate =
+      optionalDecorateTime(`generate`, callUnderlyingGenerate);
+
     if (hasSlotsDescription) {
       const stationery = fulfilledDependencies.html.stationery({
         annotation: generate.name,
@@ -204,19 +214,19 @@ export function expectDependencies({
   wrappedGenerate[contentFunction.identifyingSymbol] = true;
 
   if (hasSprawlFunction) {
-    wrappedGenerate.sprawl = sprawl;
+    wrappedGenerate.sprawl = optionalDecorateTime(`sprawl`, sprawl);
   }
 
   if (hasQueryFunction) {
-    wrappedGenerate.query = query;
+    wrappedGenerate.query = optionalDecorateTime(`query`, query);
   }
 
   if (hasRelationsFunction) {
-    wrappedGenerate.relations = relations;
+    wrappedGenerate.relations = optionalDecorateTime(`relations`, relations);
   }
 
   if (hasDataFunction) {
-    wrappedGenerate.data = data;
+    wrappedGenerate.data = optionalDecorateTime(`data`, data);
   }
 
   wrappedGenerate.fulfill ??= function fulfill(dependencies) {