examples/streaming-response/app.pl

filter: Statementstmt 95.8% 23 / 24 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 91.1% 31 / 34 CC 3 SCAR 4.8
CC
3
Cov
92%
__ANON__
3.0
__ANON__
1.1
__ANON__
1.0
CRAP 1.6
« examples/full-demo/app.pl ↑ Summary examples/connection-introspection/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 PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Ports PAGI's 02-streaming-response to PAGI::Nano.
7 # Stream a chunked response body. $c->response->stream takes an async writer;
8 # returning the response lets Nano send it, nothing buffered. Register an
9 # on_disconnect callback to learn if the client goes away mid-stream (on a real
10 # server; in-process test clients have no live connection).
11 #
12 # pagi-server app.pl
13 # curl -N http://127.0.0.1:5000/stream
14  
15 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1 ▶ get '/stream' => sub ($c) {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 my $gone = 0;
18 0 ▶ $c->on_disconnect(sub { $gone = 1 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
19  
20 1 ▶ $c->response->stream(async sub ($w) {
Subroutine __ANON__: called CC=3 SCAR=11.0
21 1 for my $i (1 .. 5) {
22 5 ▶ last if $gone; # stop early if the client left
Branch: 1 branch
BranchTrueFalseTotal
if $gone 0 5 5
23 5 await $w->write("chunk $i\n");
24 }
25 1 await $w->close;
26 1 });
27 1 };
28 1 };
29  
30 1 $app;
« examples/full-demo/app.pl ↑ Summary examples/connection-introspection/app.pl »