blib/lib/PAGI/SSE.pm

filter: Statementstmt 86.1% 342 / 397 Branchbran 68.0% 128 / 188 Conditioncond 53.0% 52 / 98 MC/DCmcdc 30.9% 17 / 55 Subroutinesub 90.9% 60 / 66 Podpod 91.6% 44 / 48 totaltotal 75.4% 643 / 852 CC 142 SCAR 26.4
CC
142
Cov
76%
every
88.4
__ANON__
72.0
query_params
13.5
CRAP 14.0
« blib/lib/PAGI/App/Router.pm ↑ Summary blib/lib/PAGI/Middleware/RequestId.pm »
1 package PAGI::SSE;
2 24 $PAGI::SSE::VERSION = '0.002001';
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Encode qw(decode FB_CROAK FB_DEFAULT LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 sub new {
15 108 ▶ my ($class, $scope, $receive, $send) = @_;
Subroutine new: called CC=10 SCAR=23.2
Pod documented
16  
17 108 ▶ croak "PAGI::SSE requires scope hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless $scope and ref $scope eq "HASH" 1 107 108
Condition66%2/3
$scope and ref $scope eq "HASH"
!ll&&!rl&&r
10107
MC/DC50%1/2
$scope and ref $scope eq "HASH"
$scope ref $scope eq "HASH"
Truth table66%2/3
A and B
A= $scopeB= ref $scope eq "HASH"
ABresult
0X0
100
111
18 unless $scope && ref($scope) eq 'HASH';
19 107 ▶ croak "PAGI::SSE requires receive coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $receive and ref $receive eq "CODE" 1 106 107
Condition66%2/3
$receive and ref $receive eq "CODE"
!ll&&!rl&&r
10106
MC/DC50%1/2
$receive and ref $receive eq "CODE"
$receive ref $receive eq "CODE"
Truth table66%2/3
A and B
A= $receiveB= ref $receive eq "CODE"
ABresult
0X0
100
111
20 unless $receive && ref($receive) eq 'CODE';
21 106 ▶ croak "PAGI::SSE requires send coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $send and ref $send eq "CODE" 1 105 106
Condition66%2/3
$send and ref $send eq "CODE"
!ll&&!rl&&r
10105
MC/DC50%1/2
$send and ref $send eq "CODE"
$send ref $send eq "CODE"
Truth table66%2/3
A and B
A= $sendB= ref $send eq "CODE"
ABresult
0X0
100
111
22 unless $send && ref($send) eq 'CODE';
23 croak "PAGI::SSE requires scope type 'sse', got '$scope->{type}'"
24 105 ▶ unless ($scope->{type} // '') eq 'sse';
Branch: 1 branch
BranchTrueFalseTotal
unless ($scope->{"type"} // "") eq "sse" 1 104 105
Condition50%1/2
$scope->{"type"} // ""
l!l
1050
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
25  
26 # Return existing SSE object if one was already created for this scope
27 # This ensures consistent state (is_started, is_closed, callbacks) if
28 # multiple code paths create SSE objects from the same scope.
29 104 ▶ return $scope->{'pagi.sse'} if $scope->{'pagi.sse'};
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"pagi.sse"} 1 103 104
30  
31 103 my $self = bless {
32 scope => $scope,
33 receive => $receive,
34 send => $send,
35 _state => 'pending', # pending -> started -> closed
36 _on_close => [],
37 _on_error => [],
38 _disconnect_reason => undef, # Set when disconnect received
39 }, $class;
40  
41 # Cache in scope for reuse (weakened to avoid circular reference leak)
42 103 $scope->{'pagi.sse'} = $self;
43 103 Scalar::Util::weaken($scope->{'pagi.sse'});
44  
45 103 return $self;
46 }
47  
48 # Scope property accessors
49 5 ▶sub scope { shift->{scope} }
Subroutine scope: called CC=1 SCAR=0.0
Pod documented
50 2 ▶sub path { shift->{scope}{path} // '/' }
Subroutine path: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"path"} // "/"
l!l
20
MC/DC0%0/1
+(shift())->{"scope"}{"path"} // "/"
+(shift())->{"scope"}{"path"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"path"}
Aresult
11
00
51 0 ▶sub raw_path { my $s = shift; $s->{scope}{raw_path} // $s->{scope}{path} // '/' }
Subroutine raw_path: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/5
($s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}) // "/"
l!l
00
$s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($s->{"scope"}{"raw_path"} // $s->{"scope"}{"path"}) // "/"
$s->{"scope"}{"raw_path"} $s->{"scope"}{"path"}
Truth table0%0/3
A // B
A= $s->{"scope"}{"raw_path"}B= $s->{"scope"}{"path"}
ABresult
1X1
011
000
52 5 ▶sub query_string { shift->{scope}{query_string} // '' }
Subroutine query_string: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"query_string"} // ""
l!l
50
MC/DC0%0/1
+(shift())->{"scope"}{"query_string"} // ""
+(shift())->{"scope"}{"query_string"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"query_string"}
Aresult
11
00
53  
54 # URL decode helper (handles + as space per application/x-www-form-urlencoded)
55 sub _url_decode {
56 12 ▶ my ($str) = @_;
Subroutine _url_decode: called CC=2 SCAR=7.0
57 12 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 12 12
58 12 $str =~ s/\+/ /g;
59 12 $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
60 12 return $str;
61 }
62  
63 # UTF-8 decode helper with strict/lenient mode
64 sub _decode_utf8 {
65 8 ▶ my ($str, $strict) = @_;
Subroutine _decode_utf8: called CC=3 SCAR=11.3
66 8 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 8 8
67 8 ▶ my $flag = $strict ? FB_CROAK : FB_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
$strict ? : 0 8 8
68 8 $flag |= LEAVE_SRC;
69 8 return decode('UTF-8', $str, $flag);
70 }
71  
72 # Query params as Hash::MultiValue (cached in scope)
73 # Options: strict => 1 (croak on invalid UTF-8), raw => 1 (skip UTF-8 decoding)
74 sub query_params {
75 6 ▶ my ($self, %opts) = @_;
Subroutine query_params: called CC=13 SCAR=26.0
Pod documented
76 6 ▶ my $strict = delete $opts{strict} // 0;
Condition50%1/2
delete $opts{"strict"} // 0
l!l
06
MC/DC0%0/1
delete $opts{"strict"} // 0
delete $opts{"strict"}
Truth table50%1/2
A
A= delete $opts{"strict"}
Aresult
11
00
77 6 ▶ my $raw = delete $opts{raw} // 0;
Condition100%2/2
delete $opts{"raw"} // 0
l!l
24
MC/DC100%1/1
delete $opts{"raw"} // 0
delete $opts{"raw"}
Truth table100%2/2
A
A= delete $opts{"raw"}
Aresult
11
00
78 6 ▶ croak("Unknown options to query_params: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 6 6
79  
80 6 ▶ my $cache_key = $raw ? 'pagi.sse.query.raw' : ($strict ? 'pagi.sse.query.strict' : 'pagi.sse.query');
Branch: 2 branches
BranchTrueFalseTotal
$raw ? : 2 4 6
$strict ? : 0 4 4
81 6 ▶ return $self->{scope}{$cache_key} if $self->{scope}{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"scope"}{$cache_key} 2 4 6
82  
83 4 my $qs = $self->query_string;
84 4 my @pairs;
85  
86 4 for my $part (split /[&;]/, $qs) {
87 6 ▶ next unless length $part;
Branch: 1 branch
BranchTrueFalseTotal
unless length $part 0 6 6
88 6 my ($key, $val) = split /=/, $part, 2;
89 6 ▶ $key //= '';
Condition50%1/2
$key //= ""
l!l
60
MC/DC0%0/1
$key //= ""
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
90 6 ▶ $val //= '';
Condition50%1/2
$val //= ""
l!l
60
MC/DC0%0/1
$val //= ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
91  
92 # URL decode (handles + as space)
93 6 my $key_decoded = _url_decode($key);
94 6 my $val_decoded = _url_decode($val);
95  
96 # UTF-8 decode unless raw mode
97 6 ▶ my $key_final = $raw ? $key_decoded : _decode_utf8($key_decoded, $strict);
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 2 4 6
98 6 ▶ my $val_final = $raw ? $val_decoded : _decode_utf8($val_decoded, $strict);
Branch: 1 branch
BranchTrueFalseTotal
$raw ? : 2 4 6
99  
100 6 push @pairs, $key_final, $val_final;
101 }
102  
103 4 $self->{scope}{$cache_key} = Hash::MultiValue->new(@pairs);
104 4 return $self->{scope}{$cache_key};
105 }
106  
107 # Raw query params (no UTF-8 decoding)
108 sub raw_query_params {
109 1 ▶ my $self = shift;
Subroutine raw_query_params: called CC=1 SCAR=0.0
Pod documented
110 1 return $self->query_params(raw => 1);
111 }
112  
113 # Single query param accessor
114 sub query_param {
115 4 ▶ my ($self, $name, %opts) = @_;
Subroutine query_param: called CC=1 SCAR=0.0
Pod documented
116 4 return $self->query_params(%opts)->get($name);
117 }
118  
119 # Raw single query param
120 sub raw_query_param {
121 1 ▶ my ($self, $name) = @_;
Subroutine raw_query_param: called CC=1 SCAR=0.0
Pod documented
122 1 return $self->query_param($name, raw => 1);
123 }
124  
125 1 ▶sub scheme { shift->{scope}{scheme} // 'http' }
Subroutine scheme: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"scheme"} // "http"
l!l
10
MC/DC0%0/1
+(shift())->{"scope"}{"scheme"} // "http"
+(shift())->{"scope"}{"scheme"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"scheme"}
Aresult
11
00
126 1 ▶sub http_version { shift->{scope}{http_version} // '1.1' }
Subroutine http_version: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"scope"}{"http_version"} // "1.1"
l!l
10
MC/DC0%0/1
+(shift())->{"scope"}{"http_version"} // "1.1"
+(shift())->{"scope"}{"http_version"}
Truth table50%1/2
A
A= +(shift())->{"scope"}{"http_version"}
Aresult
11
00
127 0 ▶sub client { shift->{scope}{client} }
Subroutine client: not called CC=1 SCAR=6.9
Pod undocumented
128 0 ▶sub server { shift->{scope}{server} }
Subroutine server: not called CC=1 SCAR=6.9
Pod undocumented
129  
130  
131 # Application state (injected by PAGI::Lifespan, read-only)
132 sub state {
133 4 ▶ my $self = shift;
Subroutine state: called CC=2 SCAR=6.9
Pod documented
134 4 ▶ return $self->{scope}{state} // {};
Condition100%2/2
$self->{"scope"}{"state"} // {}
l!l
22
MC/DC100%1/1
$self->{"scope"}{"state"} // {}
$self->{"scope"}{"state"}
Truth table100%2/2
A
A= $self->{"scope"}{"state"}
Aresult
11
00
135 }
136  
137 # Path parameter accessors - captured from URL path by router
138 # Stored in scope->{path_params} for router-agnostic access
139 sub path_params {
140 2 ▶ my ($self) = @_;
Subroutine path_params: called CC=2 SCAR=6.9
Pod documented
141 2 ▶ return $self->{scope}{path_params} // {};
Condition100%2/2
$self->{"scope"}{"path_params"} // {}
l!l
11
MC/DC100%1/1
$self->{"scope"}{"path_params"} // {}
$self->{"scope"}{"path_params"}
Truth table100%2/2
A
A= $self->{"scope"}{"path_params"}
Aresult
11
00
142 }
143  
144 sub path_param {
145 6 ▶ my ($self, $name) = @_;
Subroutine path_param: called CC=2 SCAR=6.9
Pod documented
146 6 ▶ my $params = $self->{scope}{path_params} // {};
Condition100%2/2
$self->{"scope"}{"path_params"} // {}
l!l
51
MC/DC100%1/1
$self->{"scope"}{"path_params"} // {}
$self->{"scope"}{"path_params"}
Truth table100%2/2
A
A= $self->{"scope"}{"path_params"}
Aresult
11
00
147 6 return $params->{$name};
148 }
149  
150 # Connection state accessors
151 4 ▶sub connection_state { shift->{_state} }
Subroutine connection_state: called CC=1 SCAR=0.0
Pod documented
152  
153 sub is_started {
154 126 ▶ my $self = shift;
Subroutine is_started: called CC=1 SCAR=0.0
Pod documented
155 126 return $self->{_state} eq 'started';
156 }
157  
158 sub is_closed {
159 158 ▶ my $self = shift;
Subroutine is_closed: called CC=1 SCAR=0.0
Pod documented
160 158 return $self->{_state} eq 'closed';
161 }
162  
163 # True while the stream is live (started and not yet closed/disconnected).
164 # Mirrors PAGI::WebSocket->is_connected (state eq 'connected'): an SSE scope
165 # carries no pagi.connection (spec: N/A for sse), so connection liveness is
166 # derived from this object's own state machine, not the base Context method.
167 sub is_connected {
168 9 ▶ my $self = shift;
Subroutine is_connected: called CC=1 SCAR=0.0
Pod documented
169 9 return $self->{_state} eq 'started';
170 }
171  
172 # Disconnect reason - why the connection closed
173 # Common values: 'client_closed', 'write_error', 'send_timeout', 'idle_timeout'
174 sub disconnect_reason {
175 0 ▶ my $self = shift;
Subroutine disconnect_reason: not called CC=1 SCAR=6.9
Pod documented
176 0 return $self->{_disconnect_reason};
177 }
178  
179 # Outbound flow-control introspection (delegates to the pagi.transport handle)
180 sub buffered_amount {
181 2 ▶ my $self = shift;
Subroutine buffered_amount: called CC=2 SCAR=6.9
Pod documented
182 2 my $t = $self->{scope}{'pagi.transport'};
183 2 ▶ return 0 unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 1 2
184 1 return $t->buffered_amount;
185 }
186  
187 sub high_water_mark {
188 2 ▶ my $self = shift;
Subroutine high_water_mark: called CC=2 SCAR=6.9
Pod documented
189 2 my $t = $self->{scope}{'pagi.transport'};
190 2 ▶ return undef unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 1 2
191 1 return $t->high_water_mark;
192 }
193  
194 sub low_water_mark {
195 2 ▶ my $self = shift;
Subroutine low_water_mark: called CC=2 SCAR=6.9
Pod documented
196 2 my $t = $self->{scope}{'pagi.transport'};
197 2 ▶ return undef unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 1 2
198 1 return $t->low_water_mark;
199 }
200  
201 sub on_high_water {
202 2 ▶ my ($self, $cb) = @_;
Subroutine on_high_water: called CC=3 SCAR=11.0
Pod documented
203 2 my $t = $self->{scope}{'pagi.transport'};
204 2 ▶ $t->on_high_water($cb) if $t && $t->can('on_high_water');
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t->can("on_high_water") 1 1 2
Condition66%2/3
$t and $t->can("on_high_water")
!ll&&!rl&&r
011
MC/DC50%1/2
$t and $t->can("on_high_water")
$t $t->can("on_high_water")
Truth table66%2/3
A and B
A= $tB= $t->can("on_high_water")
ABresult
0X0
100
111
205 2 return $self;
206 }
207  
208 sub on_drain {
209 2 ▶ my ($self, $cb) = @_;
Subroutine on_drain: called CC=3 SCAR=11.0
Pod documented
210 2 my $t = $self->{scope}{'pagi.transport'};
211 2 ▶ $t->on_drain($cb) if $t && $t->can('on_drain');
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t->can("on_drain") 1 1 2
Condition66%2/3
$t and $t->can("on_drain")
!ll&&!rl&&r
011
MC/DC50%1/2
$t and $t->can("on_drain")
$t $t->can("on_drain")
Truth table66%2/3
A and B
A= $tB= $t->can("on_drain")
ABresult
0X0
100
111
212 2 return $self;
213 }
214  
215 sub is_writable {
216 3 ▶ my $self = shift;
Subroutine is_writable: called CC=4 SCAR=13.9
Pod documented
217 3 my $t = $self->{scope}{'pagi.transport'};
218 3 ▶ return 1 unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 1 2 3
219 2 my $high = $t->high_water_mark;
220 2 ▶ return 1 unless defined $high;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $high 0 2 2
221 2 ▶ return $t->buffered_amount < $high ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$t->buffered_amount < $high ? : 1 1 2
222 }
223  
224 # Internal state setters
225 sub _set_state {
226 63 ▶ my ($self, $state) = @_;
Subroutine _set_state: called CC=1 SCAR=0.0
227 63 $self->{_state} = $state;
228 }
229  
230 sub _set_closed {
231 32 ▶ my ($self) = @_;
Subroutine _set_closed: called CC=3 SCAR=11.0
232 32 $self->{_state} = 'closed';
233  
234 # Wake a parked run() (which races this future against $receive) so a close
235 # from deep in a helper actually ends the stream.
236 $self->{_closed_future}->done
237 32 ▶ if $self->{_closed_future} && !$self->{_closed_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_closed_future"} and not $self->{"_closed_future"}->is_ready 1 31 32
Condition100%3/3
$self->{"_closed_future"} and not $self->{"_closed_future"}->is_ready
!ll&&!rl&&r
20111
MC/DC100%2/2
$self->{"_closed_future"} and not $self->{"_closed_future"}->is_ready
$self->{"_closed_future"} not $self->{"_closed_future"}->is_ready
Truth table100%3/3
A and not(B)
A= $self->{"_closed_future"}B= not $self->{"_closed_future"}->is_ready
ABresult
0X0
100
111
238 }
239  
240 # Start the SSE stream
241 56 ▶async sub start {
Subroutine start: called CC=5 SCAR=16.1
Pod documented
242 56 my ($self, %opts) = @_;
243  
244 # Idempotent - don't start twice
245 56 ▶ return $self if $self->is_started || $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_started or $self->is_closed 2 54 56
Condition66%2/3
$self->is_started or $self->is_closed
l!l&&r!l&&!r
2054
MC/DC50%1/2
$self->is_started or $self->is_closed
$self->is_started $self->is_closed
Truth table66%2/3
A or B
A= $self->is_startedB= $self->is_closed
ABresult
1X1
011
000
246  
247 my $event = {
248 type => 'sse.start',
249 54 ▶ status => $opts{status} // 200,
Condition100%2/2
$opts{"status"} // 200
l!l
252
MC/DC100%1/1
$opts{"status"} // 200
$opts{"status"}
Truth table100%2/2
A
A= $opts{"status"}
Aresult
11
00
250 };
251 54 ▶ $event->{headers} = $opts{headers} if exists $opts{headers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"headers"} 2 52 54
252  
253 54 await $self->{send}->($event);
254 54 $self->_set_state('started');
255  
256 54 return $self;
257 24 }
258  
259 # Enable keepalive - sends sse.keepalive event to server
260 # Server handles the timer; this is loop-agnostic
261 8 ▶async sub keepalive {
Subroutine keepalive: called CC=3 SCAR=11.0
Pod documented
262 8 my ($self, $interval, $comment) = @_;
263  
264 8 ▶ await $self->{send}->({
Condition75%3/4
$interval // 0
l!l
80
$comment // ""
l!l
26
MC/DC0%0/1
$interval // 0
$interval
MC/DC100%1/1
$comment // ""
$comment
Truth table50%1/2
A
A= $interval
Aresult
11
00
Truth table100%2/2
A
A= $comment
Aresult
11
00
265 type => 'sse.keepalive',
266 interval => $interval // 0,
267 comment => $comment // '',
268 });
269  
270 8 return $self;
271 24 }
272  
273 # Single header lookup (case-insensitive, returns last value)
274 sub header {
275 9 ▶ my ($self, $name) = @_;
Subroutine header: called CC=4 SCAR=13.9
Pod documented
276 9 $name = lc($name);
277 9 my $value;
278 9 ▶ for my $pair (@{$self->{scope}{headers} // []}) {
Condition50%1/2
$self->{"scope"}{"headers"} // []
l!l
90
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
Aresult
11
00
279 15 ▶ if (lc($pair->[0]) eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $pair->[0] eq $name) 6 9 15
280 6 $value = $pair->[1];
281 }
282 }
283 9 return $value;
284 }
285  
286 # All headers as Hash::MultiValue (cached in scope)
287 sub headers {
288 2 ▶ my $self = shift;
Subroutine headers: called CC=4 SCAR=14.0
Pod documented
289 2 ▶ return $self->{scope}{'pagi.request.headers'} if $self->{scope}{'pagi.request.headers'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"scope"}{"pagi.request.headers"} 0 2 2
290  
291 2 my @pairs;
292 2 ▶ for my $pair (@{$self->{scope}{headers} // []}) {
Condition50%1/2
$self->{"scope"}{"headers"} // []
l!l
20
MC/DC0%0/1
$self->{"scope"}{"headers"} // []
$self->{"scope"}{"headers"}
Truth table50%1/2
A
A= $self->{"scope"}{"headers"}
Aresult
11
00
293 5 push @pairs, lc($pair->[0]), $pair->[1];
294 }
295  
296 2 $self->{scope}{'pagi.request.headers'} = Hash::MultiValue->new(@pairs);
297 2 return $self->{scope}{'pagi.request.headers'};
298 }
299  
300 # All values for a header
301 sub header_all {
302 2 ▶ my ($self, $name) = @_;
Subroutine header_all: called CC=1 SCAR=0.0
Pod documented
303 2 return $self->headers->get_all(lc($name));
304 }
305  
306 # Get Last-Event-ID header from client (for reconnection)
307 sub last_event_id {
308 6 ▶ my ($self) = @_;
Subroutine last_event_id: called CC=1 SCAR=0.0
Pod documented
309 6 return $self->header('last-event-id');
310 }
311  
312 # Send data-only event
313 11 ▶async sub send {
Subroutine send: called CC=3 SCAR=11.0
Pod documented
314 11 my ($self, $data) = @_;
315  
316 11 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 1 10 11
317  
318 # Auto-start if not started
319 10 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 2 8 10
320  
321 10 await $self->{send}->({
322 type => 'sse.send',
323 data => $data,
324 });
325  
326 10 return $self;
327 24 }
328  
329 # Send JSON-encoded data
330 5 ▶async sub send_json {
Subroutine send_json: called CC=3 SCAR=11.0
Pod documented
331 5 my ($self, $data) = @_;
332  
333 5 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 5 5
334  
335 5 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 1 4 5
336  
337 5 my $json = JSON::MaybeXS::encode_json($data);
338  
339 5 await $self->{send}->({
340 type => 'sse.send',
341 data => $json,
342 });
343  
344 5 return $self;
345 24 }
346  
347 # Send full SSE event with all fields
348 15 ▶async sub send_event {
Subroutine send_event: called CC=8 SCAR=20.8
Pod documented
349 15 my ($self, %opts) = @_;
350  
351 15 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 15 15
352 15 ▶ croak "send_event requires 'data' parameter" unless exists $opts{data};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"data"} 0 15 15
353  
354 15 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 7 8 15
355  
356 # Auto-encode hashref/arrayref data as JSON
357 15 my $data = $opts{data};
358 15 ▶ if (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 13 2 15
359 13 $data = JSON::MaybeXS::encode_json($data);
360 }
361  
362 15 my $event = {
363 type => 'sse.send',
364 data => $data,
365 };
366  
367 15 ▶ $event->{event} = $opts{event} if defined $opts{event};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"event"} 13 2 15
368 15 ▶ $event->{id} = "$opts{id}" if defined $opts{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"id"} 9 6 15
369 15 ▶ $event->{retry} = int($opts{retry}) if defined $opts{retry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"retry"} 3 12 15
370  
371 15 await $self->{send}->($event);
372  
373 15 return $self;
374 24 }
375  
376 # Safe send - returns bool instead of throwing
377 10 ▶async sub try_send {
Subroutine try_send: called CC=4 SCAR=13.9
Pod documented
378 10 my ($self, $data) = @_;
379 10 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 1 9 10
380  
381 9 eval {
382 9 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 9 9
383 9 await $self->{send}->({
384 type => 'sse.send',
385 data => $data,
386 });
387 };
388 9 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 7 2 9
389 7 $self->_set_closed;
390 7 await $self->_trigger_error($err);
391 7 return 0;
392 }
393 2 return 1;
394 24 }
395  
396 2 ▶async sub try_send_json {
Subroutine try_send_json: called CC=4 SCAR=15.2
Pod documented
397 2 my ($self, $data) = @_;
398 2 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 2 2
399  
400 2 eval {
401 2 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 2 2
402 2 my $json = JSON::MaybeXS::encode_json($data);
403 2 await $self->{send}->({
404 type => 'sse.send',
405 data => $json,
406 });
407 };
408 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
409 0 $self->_set_closed;
410 0 await $self->_trigger_error($err);
411 0 return 0;
412 }
413 2 return 1;
414 24 }
415  
416 # Send SSE comment (doesn't trigger onmessage in browser)
417 1 ▶async sub send_comment {
Subroutine send_comment: called CC=3 SCAR=11.2
Pod undocumented
418 1 my ($self, $comment) = @_;
419  
420 1 ▶ croak "Cannot send on closed SSE connection" if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 1 1
421  
422 1 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 1 1
423  
424 1 await $self->{send}->({
425 type => 'sse.comment',
426 comment => $comment,
427 });
428  
429 1 return $self;
430 24 }
431  
432 1 ▶async sub try_send_comment {
Subroutine try_send_comment: called CC=4 SCAR=15.5
Pod undocumented
433 1 my ($self, $comment) = @_;
434 1 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 1 1
435  
436 1 eval {
437 1 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 1 1
438 1 await $self->{send}->({
439 type => 'sse.comment',
440 comment => $comment,
441 });
442 };
443 1 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 1 1
444 0 $self->_set_closed;
445 0 await $self->_trigger_error($err);
446 0 return 0;
447 }
448 1 return 1;
449 24 }
450  
451 2 ▶async sub try_send_event {
Subroutine try_send_event: called CC=9 SCAR=25.3
Pod documented
452 2 my ($self, %opts) = @_;
453 2 ▶ return 0 if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 2 2
454  
455 2 eval {
456 2 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 2 2
457  
458 2 ▶ my $data = $opts{data} // '';
Condition50%1/2
$opts{"data"} // ""
l!l
20
MC/DC0%0/1
$opts{"data"} // ""
$opts{"data"}
Truth table50%1/2
A
A= $opts{"data"}
Aresult
11
00
459 2 ▶ if (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 0 2 2
460 0 $data = JSON::MaybeXS::encode_json($data);
461 }
462  
463 2 my $event = {
464 type => 'sse.send',
465 data => $data,
466 };
467 2 ▶ $event->{event} = $opts{event} if defined $opts{event};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"event"} 2 0 2
468 2 ▶ $event->{id} = "$opts{id}" if defined $opts{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"id"} 0 2 2
469 2 ▶ $event->{retry} = int($opts{retry}) if defined $opts{retry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"retry"} 0 2 2
470  
471 2 await $self->{send}->($event);
472 };
473 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
474 0 $self->_set_closed;
475 0 await $self->_trigger_error($err);
476 0 return 0;
477 }
478 2 return 1;
479 24 }
480  
481 # Internal: trigger error callbacks
482 7 ▶async sub _trigger_error {
Subroutine _trigger_error: called CC=6 SCAR=17.9
483 7 my ($self, $error) = @_;
484  
485 7 for my $cb (@{$self->{_on_error}}) {
486 7 eval {
487 7 my $r = $cb->($self, $error);
488 6 ▶ if (blessed($r) && $r->isa('Future')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 2 4 6
Condition66%2/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
402
MC/DC50%1/2
builtin::blessed($r) and $r->isa("Future")
builtin::blessed($r) $r->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($r)B= $r->isa("Future")
ABresult
0X0
100
111
489 2 await $r;
490 }
491 };
492 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 5 7
493 2 warn "PAGI::SSE on_error callback error: $@";
494 }
495 }
496  
497 # If no error handlers registered, warn
498 7 ▶ if (!@{$self->{_on_error}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->{"_on_error"};}) 2 5 7
499 2 warn "PAGI::SSE error: $error";
500 }
501 24 }
502  
503 # Register close callback
504 sub on_close {
505 19 ▶ my ($self, $callback) = @_;
Subroutine on_close: called CC=1 SCAR=0.0
Pod documented
506 19 push @{$self->{_on_close}}, $callback;
507 19 return $self;
508 }
509  
510 # Register error callback
511 sub on_error {
512 8 ▶ my ($self, $callback) = @_;
Subroutine on_error: called CC=1 SCAR=0.0
Pod documented
513 8 push @{$self->{_on_error}}, $callback;
514 8 return $self;
515 }
516  
517 # Generic event dispatcher - dispatches to on_close or on_error
518 sub on {
519 4 ▶ my ($self, $event, $callback) = @_;
Subroutine on: called CC=3 SCAR=11.0
Pod documented
520 4 ▶ if ($event eq 'close') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event eq 'close') { } 2 2 4
elsif ($event eq 'error') { } 1 1 2
521 2 return $self->on_close($callback);
522 }
523 elsif ($event eq 'error') {
524 1 return $self->on_error($callback);
525 }
526 else {
527 1 croak "Unknown event type: $event (expected close or error)";
528 }
529 }
530  
531 # Internal: run all on_close callbacks
532 22 ▶async sub _run_close_callbacks {
Subroutine _run_close_callbacks: called CC=7 SCAR=19.7
533 22 my ($self) = @_;
534  
535 # Only run once
536 22 ▶ return if $self->{_close_callbacks_ran};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_close_callbacks_ran"} 0 22 22
537 22 $self->{_close_callbacks_ran} = 1;
538  
539 22 ▶ my $reason = $self->{_disconnect_reason} // 'unknown';
Condition50%1/2
$self->{"_disconnect_reason"} // "unknown"
l!l
220
MC/DC0%0/1
$self->{"_disconnect_reason"} // "unknown"
$self->{"_disconnect_reason"}
Truth table50%1/2
A
A= $self->{"_disconnect_reason"}
Aresult
11
00
540  
541 22 for my $cb (@{$self->{_on_close}}) {
542 16 eval {
543 16 my $r = $cb->($self, $reason);
544 16 ▶ if (blessed($r) && $r->isa('Future')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa("Future")) 3 13 16
Condition100%3/3
builtin::blessed($r) and $r->isa("Future")
!ll&&!rl&&r
1213
MC/DC100%2/2
builtin::blessed($r) and $r->isa("Future")
builtin::blessed($r) $r->isa("Future")
Truth table100%3/3
A and B
A= builtin::blessed($r)B= $r->isa("Future")
ABresult
0X0
100
111
545 3 await $r;
546 }
547 };
548 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 16 16
549 0 warn "PAGI::SSE on_close callback error: $@";
550 }
551 }
552  
553 # Clear all callback arrays to break any closure-based cycles
554 22 $self->{_on_close} = [];
555 22 $self->{_on_error} = [];
556 24 }
557  
558 # Close the connection
559 11 ▶async sub close {
Subroutine close: called CC=5 SCAR=16.4
Pod documented
560 11 my ($self, %opts) = @_;
561  
562 11 ▶ return $self if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 11 11
563  
564 # Record the reason so on_close callbacks (and the server access log) see it.
565 11 ▶ $self->{_disconnect_reason} //= $opts{reason} // 'app_closed';
Condition60%3/5
$self->{"_disconnect_reason"} //= $opts{"reason"} // "app_closed"
l!l&&r!l&&!r
0110
$opts{"reason"} // "app_closed"
l!l
38
MC/DC0%0/2
$self->{"_disconnect_reason"} //= $opts{"reason"} // "app_closed"
$self->{"_disconnect_reason"} $opts{"reason"}
Truth table33%1/3
A //= B
A= $self->{"_disconnect_reason"}B= $opts{"reason"}
ABresult
1X1
011
000
566  
567 # Tell the server to end the stream now (sse.close). The reason is
568 # server-side only and is never written to the wire.
569 await $self->{send}->({
570 type => 'sse.close',
571 11 ▶ (defined $opts{reason} ? (reason => $opts{reason}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'reason'} ? : 3 8 11
572 });
573  
574 11 $self->_set_closed;
575 11 await $self->_run_close_callbacks;
576  
577 11 return $self;
578 24 }
579  
580 # Wait for disconnect
581 13 ▶async sub run {
Subroutine run: called CC=9 SCAR=22.4
Pod documented
582 13 my ($self) = @_;
583  
584 13 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 1 12 13
585  
586 # Race incoming events against an explicit close() (which resolves this
587 # future via _set_closed), so run() returns when the stream is closed from
588 # anywhere -- not only when the client disconnects.
589 13 ▶ $self->{_closed_future} //= Future->new;
Condition33%1/3
$self->{"_closed_future"} //= "Future"->new
l!l&&r!l&&!r
0130
MC/DC0%0/2
$self->{"_closed_future"} //= "Future"->new
$self->{"_closed_future"} "Future"->new
Truth table33%1/3
A //= B
A= $self->{"_closed_future"}B= "Future"->new
ABresult
1X1
011
000
590  
591 13 ▶ while (!$self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_closed) 13 0 13
592 13 my $event = eval { await Future->wait_any($self->{receive}->(), $self->{_closed_future}) };
593 13 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 12 13
594 1 warn "PAGI::SSE receive error: $err";
595 1 last;
596 }
597  
598 12 ▶ last if $self->is_closed; # woken by close()
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 1 11 12
599  
600 11 ▶ my $type = $event->{type} // '';
Condition50%1/2
$event->{"type"} // ""
l!l
110
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table50%1/2
A
A= $event->{"type"}
Aresult
11
00
601  
602 11 ▶ if ($type eq 'sse.disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sse.disconnect") 11 0 11
603 11 ▶ $self->{_disconnect_reason} = $event->{reason} // 'client_closed';
Condition50%1/2
$event->{"reason"} // "client_closed"
l!l
011
MC/DC0%0/1
$event->{"reason"} // "client_closed"
$event->{"reason"}
Truth table50%1/2
A
A= $event->{"reason"}
Aresult
11
00
604 11 $self->_set_closed;
605 11 await $self->_run_close_callbacks;
606 11 last;
607 }
608 }
609  
610 13 return;
611 24 }
612  
613 # Iterate over items and send events
614 4 ▶async sub each {
Subroutine each: called CC=10 SCAR=23.6
Pod documented
615 4 my ($self, $source, $callback) = @_;
616  
617 4 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 1 3 4
618  
619 4 my $index = 0;
620  
621 # Handle arrayref
622 4 ▶ if (ref $source eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source eq 'ARRAY') { } 3 1 4
elsif (ref $source eq 'CODE') { } 1 0 1
623 3 for my $item (@$source) {
624 7 ▶ last if $self->is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_closed 0 7 7
625  
626 7 my $result = await $callback->($item, $index++);
627  
628 # If callback returns a hashref, treat as event spec
629 7 ▶ if (ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH") 2 5 7
630 2 await $self->send_event(%$result);
631 }
632 }
633 }
634 # Handle coderef iterator
635 elsif (ref $source eq 'CODE') {
636 1 ▶ while (!$self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_closed) 4 0 4
637 4 my $item = $source->();
638 4 ▶ last unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 1 3 4
639  
640 3 my $result = await $callback->($item, $index++);
641  
642 3 ▶ if (ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH") 0 3 3
643 0 await $self->send_event(%$result);
644 }
645 }
646 }
647 else {
648 0 croak "each() requires arrayref or coderef, got " . ref($source);
649 }
650  
651 4 return $self;
652 24 }
653  
654 # Periodic callback execution with interval delay
655 # Requires Future::IO to be installed
656 1 ▶async sub every {
Subroutine every: called CC=14 SCAR=44.8
Pod documented
657 1 my ($self, $interval, $callback) = @_;
658  
659 1 ▶ croak "every() requires interval" unless defined $interval && $interval > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $interval and $interval > 0 0 1 1
Condition33%1/3
defined $interval and $interval > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $interval and $interval > 0
defined $interval $interval > 0
Truth table33%1/3
A and B
A= defined $intervalB= $interval > 0
ABresult
0X0
100
111
660 1 ▶ croak "every() requires callback coderef" unless ref $callback eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 1 1
661  
662 # Future::IO is required for every() - fail clearly if not available
663 1 ▶ eval { require Future::IO; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Future::IO; 1 } } or croak("every() requires Future::IO to be installed. Install it with: cpanm Future::IO") 0 1 1
664 or croak "every() requires Future::IO to be installed. "
665 . "Install it with: cpanm Future::IO";
666  
667 # Future::IO must be configured with a backend
668 24 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
669 1 ▶ croak "Future::IO backend not configured. Add to your app.pl:\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $Future::IO::IMPL 1 0 1
670 . " use Future::IO::Impl::IOAsync;\n"
671 unless $Future::IO::IMPL;
672  
673 0 ▶ await $self->start unless $self->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_started 0 0 0
674  
675 # Start background disconnect monitor
676 0 my $disconnect_future = $self->_watch_for_disconnect;
677  
678 0 ▶ while (!$self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_closed) 0 0 0
679 # Execute the callback
680 0 my $ok = eval { await $callback->(); 1 };
681 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
682 0 my $err = $@;
683 # Callback failed - connection likely closed or error occurred
684 0 $self->_set_closed;
685 0 await $self->_run_close_callbacks;
686 0 last;
687 }
688  
689 # Race between sleep and disconnect detection
690 0 my $sleep_future = Future::IO->sleep($interval);
691 0 my $winner = await Future->wait_any($sleep_future, $disconnect_future);
692  
693 # If disconnect won, exit the loop
694 0 ▶ if ($self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_closed) 0 0 0
695 0 ▶ $sleep_future->cancel if $sleep_future->can('cancel') && !$sleep_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $sleep_future->can("cancel") and not $sleep_future->is_ready 0 0 0
Condition0%0/3
$sleep_future->can("cancel") and not $sleep_future->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$sleep_future->can("cancel") and not $sleep_future->is_ready
$sleep_future->can("cancel") not $sleep_future->is_ready
Truth table0%0/3
A and not(B)
A= $sleep_future->can("cancel")B= not $sleep_future->is_ready
ABresult
0X0
100
111
696 0 last;
697 }
698 }
699  
700 # Clean up disconnect monitor if still running
701 0 ▶ $disconnect_future->cancel if $disconnect_future->can('cancel') && !$disconnect_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect_future->can("cancel") and not $disconnect_future->is_ready 0 0 0
Condition0%0/3
$disconnect_future->can("cancel") and not $disconnect_future->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$disconnect_future->can("cancel") and not $disconnect_future->is_ready
$disconnect_future->can("cancel") not $disconnect_future->is_ready
Truth table0%0/3
A and not(B)
A= $disconnect_future->can("cancel")B= not $disconnect_future->is_ready
ABresult
0X0
100
111
702  
703 0 return $self;
704 24 }
705  
706 # Background future that completes when disconnect is detected
707 sub _watch_for_disconnect {
708 0 ▶ my ($self) = @_;
Subroutine _watch_for_disconnect: not called CC=1 SCAR=6.9
709  
710 0 my $receive = $self->{receive};
711 0 require Scalar::Util;
712 0 my $weak_self = $self;
713 0 Scalar::Util::weaken($weak_self);
714  
715 0 ▶ return (async sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
716 0 ▶ while ($weak_self && !$weak_self->is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self and not $weak_self->is_closed) 0 0 0
Condition0%0/3
$weak_self and not $weak_self->is_closed
!ll&&!rl&&r
000
MC/DC0%0/2
$weak_self and not $weak_self->is_closed
$weak_self not $weak_self->is_closed
Truth table0%0/3
A and not(B)
A= $weak_selfB= not $weak_self->is_closed
ABresult
0X0
100
111
717 0 my $event = eval { await $receive->() };
718 0 ▶ last unless $event;
Branch: 1 branch
BranchTrueFalseTotal
unless $event 0 0 0
719  
720 0 ▶ my $type = $event->{type} // '';
Condition0%0/2
$event->{"type"} // ""
l!l
00
MC/DC0%0/1
$event->{"type"} // ""
$event->{"type"}
Truth table0%0/2
A
A= $event->{"type"}
Aresult
11
00
721 0 ▶ if ($type eq 'sse.disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sse.disconnect") 0 0 0
722 0 ▶ if ($weak_self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self) 0 0 0
723 0 ▶ $weak_self->{_disconnect_reason} = $event->{reason} // 'client_closed';
Condition0%0/2
$event->{"reason"} // "client_closed"
l!l
00
MC/DC0%0/1
$event->{"reason"} // "client_closed"
$event->{"reason"}
Truth table0%0/2
A
A= $event->{"reason"}
Aresult
11
00
724 0 $weak_self->_set_closed;
725 0 await $weak_self->_run_close_callbacks;
726 }
727 0 last;
728 }
729 }
730 0 })->();
731 }
732  
733 24 1;
734  
735 __END__
« blib/lib/PAGI/App/Router.pm ↑ Summary blib/lib/PAGI/Middleware/RequestId.pm »