diff --git a/templates/1-basic/py/shinyreact.py b/templates/1-basic/py/shinyreact.py index c7c9769..fe82475 100644 --- a/templates/1-basic/py/shinyreact.py +++ b/templates/1-basic/py/shinyreact.py @@ -4,6 +4,7 @@ from shiny.html_dependencies import shiny_deps from shiny.types import Jsonifiable from shiny.render.renderer import Renderer, ValueFn +from shiny.module import resolve_id from typing import Any, Mapping, Optional, Sequence, Union @@ -22,7 +23,6 @@ def page_react( css_file: str | None = "main.css", lang: str = "en", ) -> ui.Tag: - head_items: list[ui.TagChild] = [] if js_file: @@ -89,6 +89,10 @@ async def post_message(session: Session, type: str, data: JsonifiableIn): React components using useShinyMessageHandler() hook. This wraps messages in a standard format and sends them via the "shinyReactMessage" channel. + When used within a Shiny module (@module.server), the type is automatically + namespaced using resolve_id(). Outside of modules, the type is passed through + unchanged. + Parameters ---------- session @@ -99,4 +103,7 @@ async def post_message(session: Session, type: str, data: JsonifiableIn): data The data to send to the client """ - await session.send_custom_message("shinyReactMessage", {"type": type, "data": data}) + namespaced_type = resolve_id(type) + await session.send_custom_message( + "shinyReactMessage", {"type": namespaced_type, "data": data} + ) diff --git a/templates/1-basic/r/shinyreact.R b/templates/1-basic/r/shinyreact.R index 6c5b402..6adae4f 100644 --- a/templates/1-basic/r/shinyreact.R +++ b/templates/1-basic/r/shinyreact.R @@ -66,14 +66,19 @@ render_json <- function( #' React components using useShinyMessageHandler() hook. This wraps messages in a #' standard format and sends them via the "shinyReactMessage" channel. #' +#' When used within a Shiny module (moduleServer), the type is automatically +#' namespaced using session$ns(). Outside of modules, the type is passed through +#' unchanged. +#' #' @param session The Shiny session object #' @param type The message type (should match messageType in useShinyMessageHandler) #' @param data The data to send to the client post_message <- function(session, type, data) { + namespaced_type <- session$ns(type) session$sendCustomMessage( "shinyReactMessage", list( - type = type, + type = namespaced_type, data = data ) ) diff --git a/templates/2-scaffold/py/shinyreact.py b/templates/2-scaffold/py/shinyreact.py index c7c9769..fe82475 100644 --- a/templates/2-scaffold/py/shinyreact.py +++ b/templates/2-scaffold/py/shinyreact.py @@ -4,6 +4,7 @@ from shiny.html_dependencies import shiny_deps from shiny.types import Jsonifiable from shiny.render.renderer import Renderer, ValueFn +from shiny.module import resolve_id from typing import Any, Mapping, Optional, Sequence, Union @@ -22,7 +23,6 @@ def page_react( css_file: str | None = "main.css", lang: str = "en", ) -> ui.Tag: - head_items: list[ui.TagChild] = [] if js_file: @@ -89,6 +89,10 @@ async def post_message(session: Session, type: str, data: JsonifiableIn): React components using useShinyMessageHandler() hook. This wraps messages in a standard format and sends them via the "shinyReactMessage" channel. + When used within a Shiny module (@module.server), the type is automatically + namespaced using resolve_id(). Outside of modules, the type is passed through + unchanged. + Parameters ---------- session @@ -99,4 +103,7 @@ async def post_message(session: Session, type: str, data: JsonifiableIn): data The data to send to the client """ - await session.send_custom_message("shinyReactMessage", {"type": type, "data": data}) + namespaced_type = resolve_id(type) + await session.send_custom_message( + "shinyReactMessage", {"type": namespaced_type, "data": data} + ) diff --git a/templates/2-scaffold/r/shinyreact.R b/templates/2-scaffold/r/shinyreact.R index 6c5b402..6adae4f 100644 --- a/templates/2-scaffold/r/shinyreact.R +++ b/templates/2-scaffold/r/shinyreact.R @@ -66,14 +66,19 @@ render_json <- function( #' React components using useShinyMessageHandler() hook. This wraps messages in a #' standard format and sends them via the "shinyReactMessage" channel. #' +#' When used within a Shiny module (moduleServer), the type is automatically +#' namespaced using session$ns(). Outside of modules, the type is passed through +#' unchanged. +#' #' @param session The Shiny session object #' @param type The message type (should match messageType in useShinyMessageHandler) #' @param data The data to send to the client post_message <- function(session, type, data) { + namespaced_type <- session$ns(type) session$sendCustomMessage( "shinyReactMessage", list( - type = type, + type = namespaced_type, data = data ) )