examples/custom-send-events/app.pl

filter: Statementstmt 96.3% 53 / 55 Branchbran 83.3% 10 / 12 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 90.4% 76 / 84 CC 10 SCAR 11.6
CC
10
Cov
92%
__ANON__
5.2
__ANON__
5.1
__ANON__
2.0
CRAP 3.2
« examples/tasks-modulino/lib/MyApp.pm ↑ Summary examples/flow-control/app.pl »
1 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON::MaybeXS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Custom SEND events: the mirror of sse-custom-events. There, a middleware folds
8 # custom events INTO $receive. Here, the handler emits high-level, semantic events
9 # and a middleware translates them OUT of $send into the wire protocol — exactly
10 # the mechanism PAGI's own SSE/WebSocket are built on (sse.send and websocket.send
11 # are custom send events a server-side layer renders).
12 #
13 # The payoff: the handler speaks pure domain — app.start, app.event{name,data},
14 # app.end — and never names a wire format. ONE handler is served at /feed in TWO
15 # formats, chosen by content negotiation:
16 #
17 # curl -N -H 'Accept: text/event-stream' http://127.0.0.1:5000/feed # SSE
18 # curl -N http://127.0.0.1:5000/feed # NDJSON
19 #
20 # The Accept header makes the server route the request to the sse scope (and the
21 # SSE renderer); without it, it is a plain HTTP request handled by the `raw`
22 # escape-hatch route (and the NDJSON renderer). Same handler, two route-scoped
23 # middlewares, two wire formats — the handler is untouched. `raw` is what lets the
24 # imperative "emit events, let a middleware render" handler run over plain HTTP
25 # (an ordinary coerced handler must return a response).
26  
27 # The handler: pure domain events on the raw send channel. $c->raw_send gives the
28 # raw $send on any context — needed on the SSE route, whose $c->send is the SSE
29 # message convenience; on the HTTP (raw) route $c->send would already do.
30 2 ▶my $emit_events = async sub ($c) {
Subroutine __ANON__: called CC=2 SCAR=6.9
31 2 my $emit = $c->raw_send;
32 2 await $emit->({ type => 'app.start' });
33 2 for my $event (
34 { name => 'status', data => 'online' },
35 { name => 'tick', data => 1 },
36 { name => 'tick', data => 2 },
37 { name => 'status', data => 'offline' },
38 ) {
39 8 await $emit->({ type => 'app.event', %$event });
40 }
41 2 await $emit->({ type => 'app.end' });
42 1 };
43  
44 # Renderer A: domain events -> Server-Sent Events, enriched with a sequence number.
45 1 ▶my $render_sse = async sub ($scope, $receive, $send, $next) {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 1 my $seq = 0;
47 6 ▶ my $wrapped = async sub ($event) {
Subroutine __ANON__: called CC=5 SCAR=16.4
48 6 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
60
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
49 6 ▶ if ($type eq 'app.start') { await $send->({ type => 'sse.start', status => 200 }) }
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'app.start') { } 1 5 6
elsif ($type eq 'app.event') { } 4 1 5
elsif ($type eq 'app.end') { } 1 0 1
50 elsif ($type eq 'app.event') {
51 4 $seq++;
52 await $send->({
53 type => 'sse.send',
54 event => $event->{name},
55 4 data => encode_json({ value => $event->{data}, seq => $seq }),
56 });
57 }
58 elsif ($type eq 'app.end') { } # the server closes the SSE stream when the handler returns
59 0 else { await $send->($event) }
60 1 };
61 1 await $next->($scope, $receive, $wrapped);
62 1 };
63  
64 # Renderer B: the SAME domain events -> NDJSON over a plain HTTP stream.
65 1 ▶my $render_ndjson = async sub ($scope, $receive, $send, $next) {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 my $seq = 0;
67 6 ▶ my $wrapped = async sub ($event) {
Subroutine __ANON__: called CC=5 SCAR=16.4
68 6 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
60
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
69 6 ▶ if ($type eq 'app.start') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'app.start') { } 1 5 6
elsif ($type eq 'app.event') { } 4 1 5
elsif ($type eq 'app.end') { } 1 0 1
70 1 await $send->({
71 type => 'http.response.start',
72 status => 200,
73 headers => [['content-type', 'application/x-ndjson']],
74 });
75 }
76 elsif ($type eq 'app.event') {
77 4 $seq++;
78 await $send->({
79 type => 'http.response.body',
80 4 body => encode_json({ event => $event->{name}, value => $event->{data}, seq => $seq }) . "\n",
81 more => 1,
82 });
83 }
84 elsif ($type eq 'app.end') {
85 1 await $send->({ type => 'http.response.body', body => '', more => 0 });
86 }
87 0 else { await $send->($event) }
88 1 };
89 1 await $next->($scope, $receive, $wrapped);
90 1 };
91  
92 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 # Same path, same handler — the renderer is the only thing that differs.
94 1 sse '/feed' => middleware($render_sse) => $emit_events; # Accept: text/event-stream
95 1 raw '/feed' => middleware($render_ndjson) => $emit_events; # plain HTTP
96 1 };
97  
98 1 $app;
« examples/tasks-modulino/lib/MyApp.pm ↑ Summary examples/flow-control/app.pl »