-
-
Notifications
You must be signed in to change notification settings - Fork 374
[LiveComponent] Advanced Polling Features #2965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
📊 Packages dist files size differenceThanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
|
(won't have time to review attentively before next week, but this looks great!) |
src/LiveComponent/assets/src/Component/plugins/PollingPlugin.ts
Outdated
Show resolved
Hide resolved
reminds me #2448 |
// controllers/poll_controller.js | ||
import { Controller } from '@hotwired/stimulus'; | ||
import { getComponent } from '@symfony/ux-live-component'; | ||
|
||
export default class extends Controller { | ||
async connect() { | ||
this.component = await getComponent(this.element); | ||
|
||
// Disable default error window (optional) | ||
this.component.on('response:error', (backendResponse, controls) => { | ||
controls.displayError = false; | ||
}); | ||
|
||
this.component.on('poll:started', ({ actionName, limit }) => { | ||
console.log(`Polling started: ${actionName}, limit: ${limit}`); | ||
}); | ||
|
||
this.component.on('poll:running', ({ actionName, count, limit }) => { | ||
console.log(`Polling running: ${actionName} (${count}/${limit})`); | ||
}); | ||
|
||
this.component.on('poll:paused', ({ actionName, count, limit }) => { | ||
console.log(`Polling paused: ${actionName}`); | ||
}); | ||
|
||
this.component.on('poll:stopped', ({ actionName, finalCount, limit }) => { | ||
console.log(`Polling stopped: ${actionName}, total runs: ${finalCount}`); | ||
}); | ||
|
||
this.component.on('poll:error', ({ actionName, finalCount, limit, errorMessage }) => { | ||
console.error(`Polling error on ${actionName}: ${errorMessage}`); | ||
}); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the whole block here ? Maybe one block and then comments would be enough ?
Something like that ?
this.component.on('poll:started', ({ actionName, limit }) => {
console.log(`Polling started: ${actionName}, limit: ${limit}`);
});
// this.component.on('poll:started', ({ actionName, limit }) => { ...
// this.component.on('poll:running', ({ actionName, count, limit }) => { ...
// this.component.on('poll:paused', ({ actionName, count, limit }) => { ...
// this.component.on('poll:stopped', ({ actionName, finalCount, limit }) => { ...
// this.component.on('poll:error', ({ actionName, finalCount, limit, errorMessage }) => { ...
|
||
.. note:: | ||
|
||
These events are dispatched on the component and can be handled using Stimulus. You must retrieve the component instance with ``getComponent(this.element)`` before accessing event listeners. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These events are dispatched on the component and can be handled using Stimulus. You must retrieve the component instance with ``getComponent(this.element)`` before accessing event listeners. | |
These events are dispatched on the component element and can be handled using Stimulus. You must retrieve the component instance with ``getComponent(this.element)`` before accessing event listeners. |
Starting from version 2.29, polling can be programmatically managed using the ``pollingDirector`` API. | ||
|
||
This allows you to start, pause, resume, or stop polling dynamically for a given action. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A real-life example could help readers understand when / why this feature is the most helpful
// controllers/poll_controller.js | ||
import { Controller } from '@hotwired/stimulus'; | ||
import { getComponent } from '@symfony/ux-live-component'; | ||
|
||
export default class extends Controller { | ||
static values = { | ||
action: String | ||
} | ||
|
||
async start(event) { | ||
const actionName = event.params.action; | ||
(await getComponent(this.element)).pollingDirector.start(actionName); | ||
} | ||
|
||
async stop(event) { | ||
const actionName = event.params.action; | ||
(await getComponent(this.element)).pollingDirector.stop(actionName); | ||
} | ||
|
||
async pause(event) { | ||
const actionName = event.params.action; | ||
(await getComponent(this.element)).pollingDirector.pause(actionName); | ||
} | ||
|
||
async resume(event) { | ||
const actionName = event.params.action; | ||
(await getComponent(this.element)).pollingDirector.resume(actionName); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we need as many full examples in the documentation.
Could we just list the methods/hooks for both, and use one example to illustrate how to use a Stimulus controller to listen to these events/actions ?
@@ -1,8 +1,14 @@ | |||
# CHANGELOG | |||
|
|||
## 2.29.0 | |||
|
|||
- Added new modifier `limit` and new Hooks for Poll System. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Added new modifier `limit` and new Hooks for Poll System. | |
- Add new modifier `limit` and new Hooks for Poll System: |
## 2.28.0 | ||
|
||
- Add new modifiers for input validations, useful to prevent unnecessary HTTP requests: | ||
- Add new modifiers for input validations, useful to prevent uneccessary HTTP requests: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add new modifiers for input validations, useful to prevent uneccessary HTTP requests: | |
- Add new modifiers for input validations, useful to prevent unneccessary HTTP requests: |
It seems unneccessary
contains two N: https://en.wiktionary.org/wiki/unnecessary
- Fixed bug where sometimes a live component was broken after hitting "Back: | ||
in your browser - #436. | ||
in your browser" - #436. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be more like this no ?
- Fixed bug where sometimes a live component was broken after hitting "Back: | |
in your browser - #436. | |
in your browser" - #436. | |
- Fixed bug where sometimes a live component was broken after hitting "Back" | |
in your browser - #436. |
But i'm not 100% sure we should change items that old 😅
Added support for poll lifecycle hooks and pollingDirector control in UX LiveComponent
This PR introduces the following updates to the LiveComponent polling system:
limit(n)
modifier to limit the number of poll iterations.poll:started
,poll:running
,poll:paused
,poll:stopped
, andpoll:error
.pollingDirector
API to start, stop, pause, or resume polling via JavaScript.This enhancement introduces support for limiting polling cycles using the
limit
modifier. Once the limit is reached, the polling will automatically stop.Additionally, new lifecycle hooks (
poll:started
,poll:running
,poll:paused
,poll:stopped
,poll:error
) provide full control over the polling process via JavaScript (e.g. Stimulus controllers).Developers can now track the polling
count
value to build dynamic UI components such as counters, countdown timers, or progress bars.Preview:
aaa.mp4
Basic usage
By default, polling runs the
$render
action every 2000 milliseconds (2 seconds).Available Modifiers
delay(ms)
— The delay between polls in milliseconds (default:2000
)limit(n)
— Maximum number of times to run the poll (default: unlimited)actionName
— The component action to call (default:$render
)Poll Hooks
The component emits lifecycle hooks during polling. You can listen to these using JavaScript, for example:
Handling Poll Actions (Start, Stop, Pause, Resume)
Polling can be programmatically managed using the
pollingDirector
API.This allows you to start, pause, resume, or stop polling dynamically for a given action.
Available Methods
The
pollingDirector
API exposes the following methods:component.pollingDirector.start(actionName)
— Starts polling for the given action (if previously stopped).component.pollingDirector.pause(actionName)
— Temporarily pauses polling. Can be resumed later.component.pollingDirector.resume(actionName)
— Resumes a previously paused poll.component.pollingDirector.stop(actionName)
— Stops polling entirely. Usestart()
to restart.