| 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
| |||||||||||
| 23 | 5 | await $w->write("chunk $i\n"); | |||||||||
| 24 | } | ||||||||||
| 25 | 1 | await $w->close; | |||||||||
| 26 | 1 | }); | |||||||||
| 27 | 1 | }; | |||||||||
| 28 | 1 | }; | |||||||||
| 29 | |||||||||||
| 30 | 1 | $app; | |||||||||