-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add HTMLTracer to debug rendering (#86)
Creates a simple HTML page that shows how budgets were used in the rendering process Closes #82
- Loading branch information
1 parent
c607486
commit 49b59bf
Showing
5 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation and GitHub. All rights reserved. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { ITracer } from './tracer'; | ||
|
||
/** | ||
* Handler that can trace rendering internals into an HTML summary. | ||
*/ | ||
export class HTMLTracer implements ITracer { | ||
private readonly entities: string[] = []; | ||
private value = ''; | ||
|
||
private elementStack: { hadChildren: boolean }[] = []; | ||
|
||
public startRenderPass(): void { | ||
const stackElem = this.elementStack[this.elementStack.length - 1]; | ||
if (stackElem && !stackElem.hadChildren) { | ||
stackElem.hadChildren = true; | ||
this.value += `<details><summary>Children</summary>`; | ||
} | ||
|
||
this.value += `<div class="render-pass">`; | ||
} | ||
public startRenderFlex(group: number, reserved: number, remainingTokenBudget: number): void { | ||
this.value += `<h2>flexGrow=${group}</h2><div class="render-flex"><p>${reserved} tokens reserved, ${remainingTokenBudget} tokens to split between children</p>`; | ||
} | ||
public didRenderElement(name: string, literals: string[]): void { | ||
this.value += `<h3>${this.entity(`<${name} />`)}</h3><div class="render-element">`; | ||
if (literals.length) { | ||
this.value += `<ul class="literals">${literals.map(l => this.entity(l.replace(/\n/g, '\\n'), 'li')).join('')}</ul>`; | ||
} | ||
this.elementStack.push({ hadChildren: false }); | ||
} | ||
public didRenderChildren(tokensConsumed: number): void { | ||
if (this.elementStack.pop()!.hadChildren) { | ||
this.value += `</details>`; | ||
} | ||
if (tokensConsumed) { | ||
this.value += `<p>${tokensConsumed} tokens consumed by children</p></div>`; | ||
} | ||
} | ||
public endRenderFlex(): void { | ||
this.value += '</div>'; | ||
} | ||
public endRenderPass(): void { | ||
this.value += '</div>'; | ||
} | ||
|
||
public toHTML() { | ||
return this.value + | ||
`<script>const ents = ${JSON.stringify(this.entities)}; for (let i = 0; i < ents.length; i++) document.querySelector('.entity-' + i).innerText = ents[i]; </script>` + | ||
`<style>${style}</style>`; | ||
} | ||
|
||
private entity(s: string, tag = 'span') { | ||
this.entities.push(s); | ||
return `<${tag} class="entity-${this.entities.length - 1}"></${tag}>`; | ||
} | ||
} | ||
|
||
const style = `body { | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', system-ui, 'Ubuntu', 'Droid Sans', sans-serif; | ||
} | ||
.render-pass { | ||
padding: 4px; | ||
border-left: 2px solid #ccc; | ||
&:hover { | ||
border-left-color: #000; | ||
} | ||
} | ||
.literals li { | ||
white-space: pre; | ||
font-family: monospace; | ||
} | ||
.render-flex, .render-element { | ||
padding-left: 10px; | ||
}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation and GitHub. All rights reserved. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
/** | ||
* Handler that can trace rendering internals. | ||
*/ | ||
export interface ITracer { | ||
/** starts a pass of rendering multiple elements */ | ||
startRenderPass(): void; | ||
/** starts rendering a flex group */ | ||
startRenderFlex(group: number, reserved: number, remainingTokenBudget: number): void; | ||
/** Marks that an element was rendered. May be followed by `startRenderPass` for children */ | ||
didRenderElement(name: string, literals: string[]): void; | ||
/** Marks that an element's children were rendered and consumed that many tokens */ | ||
didRenderChildren(tokensConsumed: number): void; | ||
/** ends rendering a flex group */ | ||
endRenderFlex(): void; | ||
/** ends a previously started render pass */ | ||
endRenderPass(): void; | ||
} |