diff --git a/src/Turbo/assets/dist/turbo_stream_controller.d.ts b/src/Turbo/assets/dist/turbo_stream_controller.d.ts index a86c6796863..2cf16fd0c92 100644 --- a/src/Turbo/assets/dist/turbo_stream_controller.d.ts +++ b/src/Turbo/assets/dist/turbo_stream_controller.d.ts @@ -3,11 +3,13 @@ export default class extends Controller { static values: { topic: StringConstructor; hub: StringConstructor; + eventSourceOptions: ObjectConstructor; }; es: EventSource | undefined; url: string | undefined; readonly topicValue: string; readonly hubValue: string; + readonly eventSourceOptionsValue: object; readonly hasHubValue: boolean; readonly hasTopicValue: boolean; initialize(): void; diff --git a/src/Turbo/assets/dist/turbo_stream_controller.js b/src/Turbo/assets/dist/turbo_stream_controller.js index 287dbbf7719..b7ca77e72ee 100644 --- a/src/Turbo/assets/dist/turbo_stream_controller.js +++ b/src/Turbo/assets/dist/turbo_stream_controller.js @@ -16,7 +16,7 @@ class default_1 extends Controller { } connect() { if (this.url) { - this.es = new EventSource(this.url); + this.es = new EventSource(this.url, this.eventSourceOptionsValue); connectStreamSource(this.es); } } @@ -30,6 +30,7 @@ class default_1 extends Controller { default_1.values = { topic: String, hub: String, + eventSourceOptions: Object, }; export { default_1 as default }; diff --git a/src/Turbo/assets/src/turbo_stream_controller.ts b/src/Turbo/assets/src/turbo_stream_controller.ts index c408dcfb099..65dbe697734 100644 --- a/src/Turbo/assets/src/turbo_stream_controller.ts +++ b/src/Turbo/assets/src/turbo_stream_controller.ts @@ -17,12 +17,14 @@ export default class extends Controller { static values = { topic: String, hub: String, + eventSourceOptions: Object, }; es: EventSource | undefined; url: string | undefined; declare readonly topicValue: string; declare readonly hubValue: string; + declare readonly eventSourceOptionsValue: object; declare readonly hasHubValue: boolean; declare readonly hasTopicValue: boolean; @@ -40,7 +42,7 @@ export default class extends Controller { connect() { if (this.url) { - this.es = new EventSource(this.url); + this.es = new EventSource(this.url, this.eventSourceOptionsValue); connectStreamSource(this.es); } } diff --git a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php index 97dd22b76e7..11616a0b1be 100644 --- a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php +++ b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php @@ -46,7 +46,11 @@ public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtens $this->stimulusHelper = $stimulus; } - public function renderTurboStreamListen(Environment $env, $topic): string + /** + * @param string|object $topic + * @param array $eventSourceOptions + */ + public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string { if (\is_object($topic)) { $class = $topic::class; @@ -61,11 +65,13 @@ public function renderTurboStreamListen(Environment $env, $topic): string $topic = sprintf(Broadcaster::TOPIC_PATTERN, rawurlencode($topic), '{id}'); } + $params = ['topic' => $topic, 'hub' => $this->hub->getPublicUrl()]; + if ($eventSourceOptions) { + $params['eventSourceOptions'] = $eventSourceOptions; + } + $stimulusAttributes = $this->stimulusHelper->createStimulusAttributes(); - $stimulusAttributes->addController( - 'symfony/ux-turbo/mercure-turbo-stream', - ['topic' => $topic, 'hub' => $this->hub->getPublicUrl()] - ); + $stimulusAttributes->addController('symfony/ux-turbo/mercure-turbo-stream', $params); return (string) $stimulusAttributes; } diff --git a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php index 3670e40bd28..0ef71a13eae 100644 --- a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php +++ b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php @@ -21,7 +21,8 @@ interface TurboStreamListenRendererInterface { /** - * @param string|object $topic + * @param string|object $topic + * @param array $eventSourceOptions */ - public function renderTurboStreamListen(Environment $env, $topic): string; + public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string; } diff --git a/src/Turbo/src/Twig/TwigExtension.php b/src/Turbo/src/Twig/TwigExtension.php index e23024edd17..0ceda0d1579 100644 --- a/src/Turbo/src/Twig/TwigExtension.php +++ b/src/Turbo/src/Twig/TwigExtension.php @@ -36,9 +36,10 @@ public function getFunctions(): iterable } /** - * @param object|string $topic + * @param string|object $topic + * @param array $eventSourceOptions */ - public function turboStreamListen(Environment $env, $topic, ?string $transport = null): string + public function turboStreamListen(Environment $env, $topic, ?string $transport = null, array $eventSourceOptions = []): string { $transport = $transport ?? $this->default; @@ -46,6 +47,6 @@ public function turboStreamListen(Environment $env, $topic, ?string $transport = throw new \InvalidArgumentException(sprintf('The Turbo stream transport "%s" doesn\'t exist.', $transport)); } - return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic); + return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic, $eventSourceOptions); } }