Description
Hello ! I believe this issue is the same as #1038, which was never truly resolved. When a component mounts, and you listen to pretty much any event like connect
or render:finished
, the event is never fired. However, it is registered, as events that are triggered later on are indeed fired correctly. The hooks
property of the component also contains the callbacks I gave it.
This is a dead-simple reproduction of my case (I can't share the real code), using the example from the documentation :
import { Controller } from '@hotwired/stimulus';
import { getComponent, type Component } from '@symfony/ux-live-component';
export default class MyController extends Controller<HTMLElement> {
component: Component | null = null;
async initialize() {
this.component = await getComponent(this.element);
this.component.on('render:finished', (component: Component) => {
console.log('render:finished', component);
});
}
}
<ul
class="{{ ('flex ' ~ attributes.render('class')|default)|tailwind_merge }}"
style="gap: {{ gap }}px; {{ attributes.render('style')|default }}"
{{ attributes.defaults({
'data-controller': 'my-controller',
}) }}
>
{# ... #}
</ul>
Nothing gets logged on the first initial render, but updating the model later on (with a simple button for example) works just fine. What I'm suspecting is that there's a race condition happening, where my controller loads after the events are dispatched. I tried removing the lazy load on the controller, or even add the defer
attribute on the Live Component, but no success. I'd really like to see this work and possibly control the first render myself (I need to get the screen width of the user and update the component accordingly, ideally showing a loader in the meantime).
Side notes :
- Would it be possible to improve the TypeScript definitions for
on
so that it would automatically type the function arguments based on the event ? - The TypeScript definitions for
component.set
andcomponent.render
always return aPromise<default>
, however in the documentation, noawait
or.then
is present between the calls, is this intended ?