Skip to content

Commit

Permalink
feat: De-duplicate client console messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Jan 14, 2025
1 parent 79f42f5 commit 5ddbe65
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion srcts/src/components/errorConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ class ShinyErrorConsole extends LitElement {
});
}

private dedupeConsoleMessages(e: Event): void {
const slot = e.target as HTMLSlotElement;
const nodes = slot.assignedNodes();

const uniqueMessages = new Set();

const nodeKey = (node: Element) => {
const headline = node.getAttribute("headline") || "";
const message = node.getAttribute("message") || "";
return `${headline}::${message}`;
};

// Keep only one copy of each unique message
nodes.forEach((node) => {
if (
node instanceof HTMLElement &&
node.tagName.toLowerCase() === "shiny-error-message"
) {
const key = nodeKey(node);
uniqueMessages.has(key) ? node.remove() : uniqueMessages.add(key);
}
});
}

render() {
return html` <div class="header">
<span class="title"> Shiny Client Errors </span>
Expand Down Expand Up @@ -246,7 +270,7 @@ class ShinyErrorConsole extends LitElement {
</svg>
</button>
</div>
<slot class="content"></slot>`;
<slot class="content" @slotchange=${this.dedupeConsoleMessages}></slot>`;
}
}

Expand Down

0 comments on commit 5ddbe65

Please sign in to comment.