blib/lib/PAGI/Endpoint/SSE.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 75.0% 6 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 11 / 11 Podpod 60.0% 3 / 5 totaltotal 90.0% 63 / 70 CC 6 SCAR 7.6
CC
6
Cov
92%
handle
4.0
__ANON__
3.0
to_app
1.0
CRAP 2.1
« blib/lib/PAGI/Middleware/Session/State/Bearer.pm ↑ Summary examples/endpoint-router-demo/lib/MyApp/API.pm »
1 package PAGI::Endpoint::SSE;
2 4 $PAGI::Endpoint::SSE::VERSION = '0.002000';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Factory class method - override in subclass for customization
10 7 ▶sub context_class { 'PAGI::Context' }
Subroutine context_class: called CC=1 SCAR=0.0
Pod documented
11  
12 # Keepalive interval in seconds (0 = disabled)
13 2 ▶sub keepalive_interval { 0 }
Subroutine keepalive_interval: called CC=1 SCAR=0.0
Pod documented
14  
15 sub new {
16 4 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
17 4 return bless \%args, $class;
18 }
19  
20 3 ▶async sub handle {
Subroutine handle: called CC=4 SCAR=13.9
Pod undocumented
21 3 my ($self, $ctx) = @_;
22 3 my $sse = $ctx->sse;
23  
24 # Configure keepalive if specified
25 3 my $keepalive = $self->keepalive_interval;
26 3 ▶ if ($keepalive > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepalive > 0) 2 1 3
27 2 $sse->keepalive($keepalive);
28 }
29  
30 # Register disconnect callback
31 3 ▶ if ($self->can('on_disconnect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("on_disconnect")) 2 1 3
32 $sse->on_close(sub {
33 2 ▶ $self->on_disconnect($ctx);
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 });
35 }
36  
37 # Call on_connect if defined
38 3 ▶ if ($self->can('on_connect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('on_connect')) { } 3 0 3
39 3 await $self->on_connect($ctx);
40 } else {
41 # Default: just start the stream
42 0 await $sse->start;
43 }
44  
45 # Wait for disconnect
46 3 await $sse->run;
47 4 }
48  
49 sub to_app {
50 5 ▶ my ($class) = @_;
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
51 5 my $context_class = $class->context_class;
52  
53 3 ▶ return async sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
54 3 my ($scope, $receive, $send) = @_;
55  
56 3 ▶ my $type = $scope->{type} // '';
Condition50%1/2
$scope->{"type"} // ""
l!l
30
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
57 3 ▶ croak "Expected sse scope, got '$type'" unless $type eq 'sse';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "sse" 0 3 3
58  
59 3 require PAGI::Context;
60 3 my $endpoint = $class->new;
61 3 my $ctx = $context_class->new($scope, $receive, $send);
62  
63 3 await $endpoint->handle($ctx);
64 5 };
65 }
66  
67 4 1;
68  
69 __END__
« blib/lib/PAGI/Middleware/Session/State/Bearer.pm ↑ Summary examples/endpoint-router-demo/lib/MyApp/API.pm »