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
« examples/endpoint-router-demo/lib/MyApp/Main.pm ↑ Summary blib/lib/PAGI/Endpoint/Router.pm »
1 package PAGI::Endpoint::SSE;
2 4 $PAGI::Endpoint::SSE::VERSION = '0.002002';
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 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 4 my ($class, %args) = @_;
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. keepalive() is async (it sends an
25 # sse.keepalive event); await it so the send completes rather than being a
26 # fire-and-forget Future (PAGI applications must await all $send calls).
27 3 my $keepalive = $self->keepalive_interval;
28 3 ▶ if ($keepalive > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepalive > 0) 2 1 3
29 2 await $sse->keepalive($keepalive);
30 }
31  
32 # Register disconnect callback
33 3 ▶ if ($self->can('on_disconnect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("on_disconnect")) 2 1 3
34 2 ▶ $sse->on_close(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 2 $self->on_disconnect($ctx);
36 2 });
37 }
38  
39 # Call on_connect if defined
40 3 ▶ if ($self->can('on_connect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('on_connect')) { } 3 0 3
41 3 await $self->on_connect($ctx);
42 } else {
43 # Default: just start the stream
44 0 await $sse->start;
45 }
46  
47 # Wait for disconnect
48 3 await $sse->run;
49 4 }
50  
51 5 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
52 5 my ($class) = @_;
53 5 my $context_class = $class->context_class;
54  
55 3 ▶ return async sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
56 3 my ($scope, $receive, $send) = @_;
57  
58 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
59 3 ▶ croak "Expected sse scope, got '$type'" unless $type eq 'sse';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "sse" 0 3 3
60  
61 3 require PAGI::Context;
62 3 my $endpoint = $class->new;
63 3 my $ctx = $context_class->new($scope, $receive, $send);
64  
65 3 await $endpoint->handle($ctx);
66 5 };
67 }
68  
69 4 1;
70  
71 __END__
« examples/endpoint-router-demo/lib/MyApp/Main.pm ↑ Summary blib/lib/PAGI/Endpoint/Router.pm »