Skip to content

Commit f9c99a9

Browse files
committed
Add debug logging
1 parent 0e93d46 commit f9c99a9

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import render from "../src/render.js";
1+
import render, { setNodeSanitizer, setEventSanitizer } from "../src/render.js";
22
import html from "../src/html.js";
33
import { state, stream } from "../src/state.js";
44
import { setDebug } from "../src/logger.js";
55

66
setDebug(true);
77

8-
const text = state("text", "Hello, world!");
9-
10-
setInterval(() => {
11-
text.send(`Hello, world! ${new Date().toLocaleTimeString()}`);
12-
}, 1000);
8+
// setNodeSanitizer(...);
9+
// setEventSanitizer(...);
1310

11+
const text = state("text", "Hello, world!");
1412
const clicks = stream("clicks");
1513

16-
clicks.sink((value) => {
17-
console.log("clicks", value);
18-
});
19-
14+
// Build template
2015
const renderable = html`
2116
<div class="container">
2217
<h1 class="title">${text}</h1>
2318
<button onclick=${clicks}>Click me</button>
2419
</div>
2520
`;
2621

27-
console.log("renderable", renderable);
28-
22+
// Render
2923
const dom = render(renderable);
3024

31-
console.log("dom", dom);
25+
clicks.sink((value) => {
26+
console.log("clicks", value);
27+
});
28+
29+
setInterval(() => {
30+
text.send(`Hello, world! ${new Date().toLocaleTimeString()}`);
31+
}, 1000);
3232

3333
document.body.appendChild(dom);

typescript/packages/common-html/src/html.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import parse from "./parser.js";
22
import { Node, isNode } from "./node.js";
33
import * as hole from "./hole.js";
44
import { NamedReactive } from "./reactive.js";
5+
import * as logger from "./logger.js";
56

67
export const html = (
78
strings: TemplateStringsArray,
@@ -21,13 +22,17 @@ export const html = (
2122
throw TypeError("Template root must be an element");
2223
}
2324

24-
const context = Object.freeze(indexContext(values));
25+
const context = indexContext(values);
2526

26-
return Object.freeze({
27+
const renderable: Renderable = {
2728
type: "renderable",
2829
template,
2930
context,
30-
})
31+
};
32+
33+
logger.debug("Renderable", renderable);
34+
35+
return renderable;
3136
};
3237

3338
export default html;
@@ -46,14 +51,16 @@ export type Context = { [key: string]: NamedReactive<unknown> };
4651

4752
const indexContext = (items: Array<NamedReactive<unknown>>): Context => {
4853
return Object.fromEntries(items.map((item) => [item.name, item]));
49-
}
54+
};
5055

5156
const flattenTemplateStrings = (
5257
strings: TemplateStringsArray,
53-
values: Array<NamedReactive<unknown>>
58+
values: Array<NamedReactive<unknown>>,
5459
): string => {
55-
return strings.reduce((result, string, i) => {
60+
const templateString = strings.reduce((result, string, i) => {
5661
const value = values[i];
5762
return result + string + (value ? hole.markup(value.name) : "");
5863
}, "");
59-
}
64+
logger.debug("Flattened", templateString);
65+
return templateString;
66+
};

typescript/packages/common-html/src/logger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const setDebug = (value: boolean) => {
1212
isDebug = value;
1313
};
1414

15-
1615
/** Log warning */
1716
export const warn = (...args: unknown[]) => {
1817
console.warn(...args);
@@ -23,4 +22,4 @@ export const debug = (...args: unknown[]) => {
2322
if (isDebug) {
2423
console.debug(...args);
2524
}
26-
};
25+
};

typescript/packages/common-html/src/parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import sax from "sax";
22
import parseMustaches from "./stache.js";
33
import { isHole } from "./hole.js";
44
import { create as createNode, Node, Attrs } from "./node.js";
5+
import * as logger from "./logger.js";
56

67
/** Parse a template into a simple JSON markup representation */
78
export const parse = (markup: string): Node => {
@@ -51,6 +52,8 @@ export const parse = (markup: string): Node => {
5152
throw new ParseError(`Unexpected root node ${root.tag}`);
5253
}
5354

55+
logger.debug("Parsed", root);
56+
5457
return root;
5558
};
5659

typescript/packages/common-html/src/render.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { isNode, Node } from "./node.js";
22
import { Renderable, Context, isRenderable } from "./html.js";
3-
import { Hole, isHole } from "./hole.js";
3+
import { isHole } from "./hole.js";
44
import { effect } from "./reactive.js";
55
import { isSendable } from "./sendable.js";
66
import { useCancelGroup, Cancel } from "./cancel.js";
7+
import * as logger from "./logger.js";
78

89
export type CancellableHTMLElement = HTMLElement & { cancel?: Cancel };
910

@@ -16,6 +17,7 @@ export const render = (renderable: Renderable): HTMLElement => {
1617
addCancel,
1718
) as CancellableHTMLElement;
1819
root.cancel = cancel;
20+
logger.debug("Rendered", root);
1921
return root;
2022
};
2123

0 commit comments

Comments
 (0)