diff --git a/assets/controllers/comments_mercure_controller.js b/assets/controllers/comments_mercure_controller.js index ac444dc..715d102 100644 --- a/assets/controllers/comments_mercure_controller.js +++ b/assets/controllers/comments_mercure_controller.js @@ -34,12 +34,12 @@ export default class extends Controller { this.eventSource = new EventSource(url.toString()); this.eventSource.onmessage = (event) => { - // buffer if live not ready yet - if (!this._liveReady) { - this._queue.push(event.data); - return; + const data = JSON.parse(event.data); // { comments, profiles, ... } + const live = this._getLiveController(); + if (live) { + live.set('payload', data); // <- updates the writable LiveProp + live.render(); // <- asks server to re-render } - this._ingest(event.data); }; } @@ -55,13 +55,15 @@ export default class extends Controller { _findLiveRoot() { return this.element.closest( - '[data-controller~="live"]' + '[data-controller~="live"],' + + '[data-controller~="symfony--ux-live-component--live"]' ); } _getLiveController() { if (!this._liveRoot) return null; - return this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live'); + return this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live') + || this.application.getControllerForElementAndIdentifier(this._liveRoot, 'symfony--ux-live-component--live'); } _renderWhenReady() { diff --git a/src/Twig/Components/Organisms/Comments.php b/src/Twig/Components/Organisms/Comments.php index 853ab31..087cdb2 100644 --- a/src/Twig/Components/Organisms/Comments.php +++ b/src/Twig/Components/Organisms/Comments.php @@ -18,6 +18,10 @@ final class Comments { use DefaultActionTrait; + // Writable prop the browser can set + #[LiveProp(writable: true)] + public array $payload = []; // { comments, profiles, ... } + // Live input #[LiveProp(writable: false)] public string $current; @@ -53,34 +57,19 @@ final class Comments } } - #[LiveAction] - public function ingest(#[LiveArg('payload')] string $json): void - { - $data = json_decode($json, true) ?: []; - - // Validate/normalize as needed - $this->list = $data['comments'] ?? []; - $this->authorsMetadata = $data['profiles'] ?? []; - - // If you send these in the event: - $this->zappers = $data['zappers'] ?? []; - $this->zapAmounts = $data['zapAmounts'] ?? []; - $this->commentLinks = $data['commentLinks'] ?? []; - - $this->loading = false; - } - /** Expose a view model to the template; keeps all parsing server-side */ public function getPayload(): array { - // Uses the helper we added earlier: getCommentsPayload($coordinate) - $payload = $this->redisCacheService->getCommentsPayload($this->current) ?? [ - 'comments' => [], - 'profiles' => [], - 'zappers' => [], - 'zapAmounts' => [], - 'commentLinks' => [], - ]; + $payload = $this->payload; + if (!$payload) { + $payload = $this->redisCacheService->getCommentsPayload($this->current) ?? [ + 'comments' => [], + 'profiles' => [], + 'zappers' => [], + 'zapAmounts' => [], + 'commentLinks' => [], + ]; + } // If your handler doesn’t compute zaps/links yet, reuse your helpers here: $this->list = $payload['comments'];