From 71b55b9ea80b0b379e0d40e6ad353e4d9df76c17 Mon Sep 17 00:00:00 2001 From: Yohann Tilotti Date: Wed, 24 Apr 2024 14:11:12 +0200 Subject: [PATCH 1/3] [Turbo] add options to EventSource Mercure --- src/Turbo/assets/dist/turbo_stream_controller.d.ts | 2 ++ src/Turbo/assets/dist/turbo_stream_controller.js | 3 ++- src/Turbo/assets/src/turbo_stream_controller.ts | 4 +++- .../src/Bridge/Mercure/TurboStreamListenRenderer.php | 12 +++++++----- .../src/Twig/TurboStreamListenRendererInterface.php | 2 +- src/Turbo/src/Twig/TwigExtension.php | 4 ++-- 6 files changed, 17 insertions(+), 10 deletions(-) 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..9b13a28c843 100644 --- a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php +++ b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php @@ -46,7 +46,7 @@ public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtens $this->stimulusHelper = $stimulus; } - public function renderTurboStreamListen(Environment $env, $topic): string + public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string { if (\is_object($topic)) { $class = $topic::class; @@ -61,11 +61,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..7e5e64c2bda 100644 --- a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php +++ b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php @@ -23,5 +23,5 @@ interface TurboStreamListenRendererInterface /** * @param string|object $topic */ - 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..7e02b7f3047 100644 --- a/src/Turbo/src/Twig/TwigExtension.php +++ b/src/Turbo/src/Twig/TwigExtension.php @@ -38,7 +38,7 @@ public function getFunctions(): iterable /** * @param object|string $topic */ - 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 +46,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); } } From f4c918e59820fc7f8da57f9f5120c91b1bac4db6 Mon Sep 17 00:00:00 2001 From: Yohann Tilotti Date: Thu, 25 Apr 2024 10:34:28 +0200 Subject: [PATCH 2/3] [Turbo] add options to EventSource Mercure --- src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php | 4 ++++ src/Turbo/src/Twig/TurboStreamListenRendererInterface.php | 3 ++- src/Turbo/src/Twig/TwigExtension.php | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php index 9b13a28c843..f661fca24f6 100644 --- a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php +++ b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php @@ -46,6 +46,10 @@ public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtens $this->stimulusHelper = $stimulus; } + /** + * @param string|object $topic + * @param array $eventSourceOptions + */ public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string { if (\is_object($topic)) { diff --git a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php index 7e5e64c2bda..1edd490519e 100644 --- a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php +++ b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php @@ -22,6 +22,7 @@ interface TurboStreamListenRendererInterface { /** * @param string|object $topic + * @param array $eventSourceOptions */ - public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions): 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 7e02b7f3047..29aa78f3876 100644 --- a/src/Turbo/src/Twig/TwigExtension.php +++ b/src/Turbo/src/Twig/TwigExtension.php @@ -37,6 +37,7 @@ public function getFunctions(): iterable /** * @param object|string $topic + * @param array $eventSourceOptions */ public function turboStreamListen(Environment $env, $topic, ?string $transport = null, array $eventSourceOptions = []): string { From 3b1de3777ec9964ae5a706485a6eafd07f52067e Mon Sep 17 00:00:00 2001 From: Yohann Tilotti Date: Thu, 25 Apr 2024 11:01:31 +0200 Subject: [PATCH 3/3] [Turbo] add options to EventSource Mercure Fix style --- src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php | 2 +- src/Turbo/src/Twig/TurboStreamListenRendererInterface.php | 2 +- src/Turbo/src/Twig/TwigExtension.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php index f661fca24f6..11616a0b1be 100644 --- a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php +++ b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php @@ -47,7 +47,7 @@ public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtens } /** - * @param string|object $topic + * @param string|object $topic * @param array $eventSourceOptions */ public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string diff --git a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php index 1edd490519e..0ef71a13eae 100644 --- a/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php +++ b/src/Turbo/src/Twig/TurboStreamListenRendererInterface.php @@ -21,7 +21,7 @@ interface TurboStreamListenRendererInterface { /** - * @param string|object $topic + * @param string|object $topic * @param array $eventSourceOptions */ 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 29aa78f3876..0ceda0d1579 100644 --- a/src/Turbo/src/Twig/TwigExtension.php +++ b/src/Turbo/src/Twig/TwigExtension.php @@ -36,7 +36,7 @@ public function getFunctions(): iterable } /** - * @param object|string $topic + * @param string|object $topic * @param array $eventSourceOptions */ public function turboStreamListen(Environment $env, $topic, ?string $transport = null, array $eventSourceOptions = []): string