Skip to content

Commit

Permalink
feat: De-duplicate client console messages (#4177)
Browse files Browse the repository at this point in the history
* feat: De-duplicate client console messages

* refactor(ShinyErrorConsole): Add `appendConsoleMessage()` static method

* fix: Make `appendConsoleMessage()` an instance method

* rename: `createClientMessageElement()`

* docs: add news item
  • Loading branch information
gadenbuie authored Jan 27, 2025
1 parent d764ea9 commit b8a5aef
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 55 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

* Fixed a bug with modals where calling `removeModal()` too quickly after `showModal()` would fail to remove the modal if the remove modal message was received while the modal was in the process of being revealed. (#4173)

* The Shiny Client Console (enabled with `shiny::devmode()`) no longer displays duplicate warning or error message. (#4177)

# shiny 1.10.0

## New features and improvements
Expand Down
182 changes: 143 additions & 39 deletions inst/www/shared/shiny.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions inst/www/shared/shiny.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/www/shared/shiny.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions inst/www/shared/shiny.min.js.map

Large diffs are not rendered by default.

50 changes: 41 additions & 9 deletions srcts/src/components/errorConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,42 @@ class ShinyErrorConsole extends LitElement {
});
}

static createClientMessageElement({ headline, message }: ShinyClientMessage) {
const msg = document.createElement("shiny-error-message");
msg.setAttribute("headline", headline || "");
msg.setAttribute("message", message);
return msg;
}

appendConsoleMessage({ headline, message }: ShinyClientMessage) {
const content =
this.shadowRoot?.querySelector<HTMLSlotElement>("slot.content");

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

for (const node of content.assignedElements()) {
if (node.tagName.toLowerCase() === "shiny-error-message") {
if (nodeKey(node) === newKey) {
// Do nothing, this message is already in the console
// TODO: Increase count of message here
return;
}
}
}
}

this.appendChild(
ShinyErrorConsole.createClientMessageElement({ headline, message })
);
return;
}

render() {
return html` <div class="header">
<span class="title"> Shiny Client Errors </span>
Expand Down Expand Up @@ -523,17 +559,13 @@ function showShinyClientMessage({

// Check to see if an Error Console Container element already exists. If it
// doesn't we need to add it before putting an error on the screen
let errorConsoleContainer = document.querySelector("shiny-error-console");
if (!errorConsoleContainer) {
errorConsoleContainer = document.createElement("shiny-error-console");
document.body.appendChild(errorConsoleContainer);
let sec = document.querySelector<ShinyErrorConsole>("shiny-error-console");
if (!sec) {
sec = document.createElement("shiny-error-console") as ShinyErrorConsole;
document.body.appendChild(sec);
}

const errorConsole = document.createElement("shiny-error-message");
errorConsole.setAttribute("headline", headline);
errorConsole.setAttribute("message", message);

errorConsoleContainer.appendChild(errorConsole);
sec.appendConsoleMessage({ headline, message });
}

/**
Expand Down

0 comments on commit b8a5aef

Please sign in to comment.