Skip to content

Commit e24b3cd

Browse files
committed
[Turbo] add options to EventSource Mercure
1 parent a3de399 commit e24b3cd

6 files changed

+17
-10
lines changed

src/Turbo/assets/dist/turbo_stream_controller.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ export default class extends Controller {
33
static values: {
44
topic: StringConstructor;
55
hub: StringConstructor;
6+
eventSourceOptions: ObjectConstructor;
67
};
78
es: EventSource | undefined;
89
url: string | undefined;
910
readonly topicValue: string;
1011
readonly hubValue: string;
12+
readonly eventSourceOptionsValue: object;
1113
readonly hasHubValue: boolean;
1214
readonly hasTopicValue: boolean;
1315
initialize(): void;

src/Turbo/assets/dist/turbo_stream_controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class default_1 extends Controller {
1616
}
1717
connect() {
1818
if (this.url) {
19-
this.es = new EventSource(this.url);
19+
this.es = new EventSource(this.url, this.eventSourceOptionsValue);
2020
connectStreamSource(this.es);
2121
}
2222
}
@@ -30,6 +30,7 @@ class default_1 extends Controller {
3030
default_1.values = {
3131
topic: String,
3232
hub: String,
33+
eventSourceOptions: Object,
3334
};
3435

3536
export { default_1 as default };

src/Turbo/assets/src/turbo_stream_controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ export default class extends Controller {
1717
static values = {
1818
topic: String,
1919
hub: String,
20+
eventSourceOptions: Object,
2021
};
2122
es: EventSource | undefined;
2223
url: string | undefined;
2324

2425
declare readonly topicValue: string;
2526
declare readonly hubValue: string;
27+
declare readonly eventSourceOptionsValue: object;
2628
declare readonly hasHubValue: boolean;
2729
declare readonly hasTopicValue: boolean;
2830

@@ -40,7 +42,7 @@ export default class extends Controller {
4042

4143
connect() {
4244
if (this.url) {
43-
this.es = new EventSource(this.url);
45+
this.es = new EventSource(this.url, this.eventSourceOptionsValue);
4446
connectStreamSource(this.es);
4547
}
4648
}

src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtens
4646
$this->stimulusHelper = $stimulus;
4747
}
4848

49-
public function renderTurboStreamListen(Environment $env, $topic): string
49+
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string
5050
{
5151
if (\is_object($topic)) {
5252
$class = $topic::class;
@@ -61,11 +61,13 @@ public function renderTurboStreamListen(Environment $env, $topic): string
6161
$topic = sprintf(Broadcaster::TOPIC_PATTERN, rawurlencode($topic), '{id}');
6262
}
6363

64+
$params = ['topic' => $topic, 'hub' => $this->hub->getPublicUrl()];
65+
if ($eventSourceOptions) {
66+
$params['eventSourceOptions'] = $eventSourceOptions;
67+
}
68+
6469
$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes();
65-
$stimulusAttributes->addController(
66-
'symfony/ux-turbo/mercure-turbo-stream',
67-
['topic' => $topic, 'hub' => $this->hub->getPublicUrl()]
68-
);
70+
$stimulusAttributes->addController('symfony/ux-turbo/mercure-turbo-stream', $params);
6971

7072
return (string) $stimulusAttributes;
7173
}

src/Turbo/src/Twig/TurboStreamListenRendererInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ interface TurboStreamListenRendererInterface
2323
/**
2424
* @param string|object $topic
2525
*/
26-
public function renderTurboStreamListen(Environment $env, $topic): string;
26+
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions): string;
2727
}

src/Turbo/src/Twig/TwigExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public function getFunctions(): iterable
3838
/**
3939
* @param object|string $topic
4040
*/
41-
public function turboStreamListen(Environment $env, $topic, string $transport = null): string
41+
public function turboStreamListen(Environment $env, $topic, string $transport = null, array $eventSourceOptions = []): string
4242
{
4343
$transport = $transport ?? $this->default;
4444

4545
if (!$this->turboStreamListenRenderers->has($transport)) {
4646
throw new \InvalidArgumentException(sprintf('The Turbo stream transport "%s" doesn\'t exist.', $transport));
4747
}
4848

49-
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic);
49+
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic, $eventSourceOptions);
5050
}
5151
}

0 commit comments

Comments
 (0)