| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # Try to load Future::IO for loop-agnostic sleep, fall back to immediate if not available | ||||||||||||||||||||||||||||
| 6 | 1 | my $HAS_FUTURE_IO = eval { require Future::IO; 1 }; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | sub maybe_sleep { | ||||||||||||||||||||||||||
|
Subroutine
maybe_sleep: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 9 | 3 | my ($seconds) = @_; | |||||||||||||||||||||||||||
| 10 | 3 | ▶ | return $HAS_FUTURE_IO ? Future::IO->sleep($seconds) : Future->done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | async sub watch_sse_disconnect { | ||||||||||||||||||||||||||
|
Subroutine
watch_sse_disconnect: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 14 | 1 | my ($receive) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | while (1) { | |||||||||||||||||||||||||||
| 17 | 2 | my $event = await $receive->(); | |||||||||||||||||||||||||||
| 18 | 1 | ▶ | return $event if $event->{type} eq 'sse.disconnect'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | 1 | } | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | async sub app { | ||||||||||||||||||||||||||
|
Subroutine
app: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 23 | 2 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'sse'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 28 | type => 'sse.start', | ||||||||||||||||||||||||||||
| 29 | status => 200, | ||||||||||||||||||||||||||||
| 30 | headers => [ [ 'content-type', 'text/event-stream' ] ], | ||||||||||||||||||||||||||||
| 31 | }); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | my $disconnect = watch_sse_disconnect($receive); | |||||||||||||||||||||||||||
| 34 | 1 | my @events = ( | |||||||||||||||||||||||||||
| 35 | { event => 'tick', data => '1' }, | ||||||||||||||||||||||||||||
| 36 | { event => 'tick', data => '2' }, | ||||||||||||||||||||||||||||
| 37 | { event => 'done', data => 'finished' }, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | for my $msg (@events) { | |||||||||||||||||||||||||||
| 41 | 3 | ▶ | last if $disconnect->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 3 | await maybe_sleep(1); | |||||||||||||||||||||||||||
| 43 | 3 | await $send->({ type => 'sse.send', %$msg }); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | $disconnect->cancel if $disconnect->can('cancel') && !$disconnect->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$disconnect->can("cancel") and not $disconnect->is_ready
MC/DC0%0/2
$disconnect->can("cancel") and not $disconnect->is_ready
$disconnect->can("cancel")
not $disconnect->is_ready
Truth table33%1/3
A and not(B)
A= $disconnect->can("cancel")B= not $disconnect->is_ready
| |||||||||||||||||||||||||||||
| 47 | 1 | } | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | \&app; # Return coderef when loaded via do | |||||||||||||||||||||||||||