fix: debounce shiny:conditional to fire once per frame#4374
Draft
cpsievert wants to merge 1 commit into
Draft
Conversation
`$updateConditionals()` is called from `connect()`, `reconnect()`, `sendInput()`, and `dispatchMessage()`. During a typical interaction it fires at least twice per cycle (e.g. sendInput + dispatchMessage), redundantly re-evaluating all `[data-display-if]` elements each time. Debounce via `requestAnimationFrame` so multiple calls within a single frame collapse into one evaluation. None of the call sites depend on synchronous completion, and rAF runs before the browser paints, so there is no visual flicker.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3668.
$updateConditionals()— which triggers theshiny:conditionalevent and re-evaluates all[data-display-if]elements — is called from 4 places:connect(),reconnect(),sendInput(), anddispatchMessage(). During a typical user interaction (e.g. clicking a button), it fires at least twice per cycle: once fromsendInput()when the input is sent, and again fromdispatchMessage()when the server response arrives. Server-driveninputMessagescan add yet another call.This PR debounces
$updateConditionals()usingrequestAnimationFrame, so multiple calls within a single animation frame collapse into one evaluation. This is safe because:requestAnimationFramecallbacks run before the browser paints, so there's no visual flicker$inputValuesand$valuesat execution time, so deferring doesn't lose dataTest plan
shiny:conditionalis running multiple times during the flush cycle #3668 and verifyshiny:conditionalfires once per interaction instead of multiple times:conditionalPanel()still works (elements show/hide correctly based on input conditions)