Skip to content

[LiveComponent] Enhance polling with limit, visible modifiers and dynamic PHP control#3365

Open
xDeSwa wants to merge 1 commit intosymfony:2.xfrom
xDeSwa:poll-limit-visible-features
Open

[LiveComponent] Enhance polling with limit, visible modifiers and dynamic PHP control#3365
xDeSwa wants to merge 1 commit intosymfony:2.xfrom
xDeSwa:poll-limit-visible-features

Conversation

@xDeSwa
Copy link
Contributor

@xDeSwa xDeSwa commented Mar 7, 2026

Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? yes
Issues -
License MIT

✨ Description

This PR introduces lightweight yet powerful controls for the data-poll feature to prevent over-fetching, save server resources, and give developers fine-grained control over polling loops.

While I noticed that PR #2965 proposes a very comprehensive and detailed polling overhaul, I believe this PR covers the most immediate and highly requested polling needs in a simpler, highly stable, and backward-compatible way. (I am completely open to adapting this approach, changing modifier names, or merging efforts based on your feedback!).

✨ New Features

  • limit(n) Modifier: Stops the polling loop after exactly n successful requests. The counter is securely stored in memory across component re-renders, preventing the limit from infinitely resetting itself.
  • visible & visible(page) Modifiers: Inspired by the visibility discussions in PR [LiveComponent] Add keep-alive modifier to polling plugin #2898.
    • visible (or visible(component)): Uses IntersectionObserver to automatically pause polling when the component leaves the viewport.
    • visible(page): Pauses polling only when the browser tab is backgrounded/hidden.
    • Note: Resuming from a paused state does NOT reset the limit counter; it resumes exactly where it left off. Default behavior (without these modifiers) remains unchanged for strict BC.
  • Server-Side Control (stopPoll): Added stopPoll(string $actionName = '$render') and stopAllPolls() to ComponentToolsTrait. This allows developers to stop frontend polling dynamically from PHP. For example, stopping a progress bar poll precisely when a long-running promotional product import finishes.

🛠️ Architecture & Stability Improvements

  • Memory Leak Protection: Added a proper destroy() cleanup method to PollingDirector that removes global event listeners and disconnects observers when the LiveComponent is disconnected.
  • Over-fetching Prevention: Shifted from setInterval to a safe recursive pattern to prevent overlapping requests (DDoS effect) if the server responds slower than the defined polling delay.

Looking forward to your thoughts and suggestions!

@carsonbot carsonbot added Documentation Improvements or additions to documentation Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed labels Mar 7, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2026

📊 Packages dist files size difference

Thanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
Please review the changes and make sure they are expected.

FileBefore (Size / Gzip)After (Size / Gzip)
LiveComponent
live_controller.js 82.44 kB / 18.33 kB 84.99 kB+3% 📈 / 18.89 kB+3% 📈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants