lib/PAGI/Middleware/SSE/Retry.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 70.0% 7 / 10 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 82.5% 66 / 80 CC 11 SCAR 15.4
CC
11
Cov
86%
__ANON__
8.4
_init
4.0
__ANON__
2.0
CRAP 4.7
« lib/PAGI/Test/WebSocket.pm ↑ Summary blib/lib/PAGI/WebSocket.pm »
1 package PAGI::Middleware::SSE::Retry;
2 2 $PAGI::Middleware::SSE::Retry::VERSION = '0.002002';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::Middleware::SSE::Retry - Add retry hints to SSE events
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'SSE::Retry',
18 retry => 5000; # 5 seconds
19 $my_app;
20 };
21  
22 =head1 DESCRIPTION
23  
24 PAGI::Middleware::SSE::Retry adds the C<retry> field to SSE events,
25 telling clients how long to wait before reconnecting after a disconnect.
26  
27 =head1 CONFIGURATION
28  
29 =over 4
30  
31 =item * retry (default: 3000)
32  
33 Default retry interval in milliseconds.
34  
35 =item * include_on_start (default: 1)
36  
37 If true, sends a retry hint immediately after sse.start.
38  
39 =item * include_on_events (default: 0)
40  
41 If true, includes retry in every sse.send event.
42  
43 =back
44  
45 =cut
46  
47 3 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
48 3 my ($self, $config) = @_;
49  
50 3 ▶ $self->{retry} = $config->{retry} // 3000;
Condition100%2/2
$config->{"retry"} // 3000
l!l
21
MC/DC100%1/1
$config->{"retry"} // 3000
$config->{"retry"}
Truth table100%2/2
A
A= $config->{"retry"}
Aresult
11
00
51 3 ▶ $self->{include_on_start} = $config->{include_on_start} // 1;
Condition100%2/2
$config->{"include_on_start"} // 1
l!l
12
MC/DC100%1/1
$config->{"include_on_start"} // 1
$config->{"include_on_start"}
Truth table100%2/2
A
A= $config->{"include_on_start"}
Aresult
11
00
52 3 ▶ $self->{include_on_events} = $config->{include_on_events} // 0;
Condition50%1/2
$config->{"include_on_events"} // 0
l!l
03
MC/DC0%0/1
$config->{"include_on_events"} // 0
$config->{"include_on_events"}
Truth table50%1/2
A
A= $config->{"include_on_events"}
Aresult
11
00
53 }
54  
55 3 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.1
Pod documented
56 3 my ($self, $app) = @_;
57  
58 3 ▶ return async sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
59 3 my ($scope, $receive, $send) = @_;
60 # Only apply to SSE connections
61 3 ▶ if ($scope->{type} ne 'sse') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "sse") 1 2 3
62 1 await $app->($scope, $receive, $send);
63 1 return;
64 }
65  
66 2 my $retry = $self->{retry};
67 2 my $sent_initial = 0;
68  
69 # Wrap send to add retry field
70 3 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=7 SCAR=21.3
71 3 my ($event) = @_;
72 3 ▶ if ($event->{type} eq 'sse.start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "sse.start") 2 1 3
73 2 await $send->($event);
74  
75 # Send initial retry hint
76 2 ▶ if ($self->{include_on_start} && !$sent_initial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_on_start"} and not $sent_initial) 2 0 2
Condition33%1/3
$self->{"include_on_start"} and not $sent_initial
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"include_on_start"} and not $sent_initial
$self->{"include_on_start"} not $sent_initial
Truth table33%1/3
A and not(B)
A= $self->{"include_on_start"}B= not $sent_initial
ABresult
0X0
100
111
77 2 $sent_initial = 1;
78 2 await $send->({
79 type => 'sse.send',
80 retry => $retry,
81 });
82 }
83 2 return;
84 }
85  
86 1 ▶ if ($event->{type} eq 'sse.send') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "sse.send") 1 0 1
87 # Add retry to event if configured and not already present
88 1 ▶ if ($self->{include_on_events} && !defined $event->{retry}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_on_events"} and not defined $event->{"retry"}) 0 1 1
Condition33%1/3
$self->{"include_on_events"} and not defined $event->{"retry"}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"include_on_events"} and not defined $event->{"retry"}
$self->{"include_on_events"} not defined $event->{"retry"}
Truth table33%1/3
A and not(B)
A= $self->{"include_on_events"}B= not defined $event->{"retry"}
ABresult
0X0
100
111
89 0 $event = { %$event, retry => $retry };
90 }
91 }
92  
93 1 await $send->($event);
94 2 };
95  
96 # Add retry info to scope
97 2 my $new_scope = {
98 %$scope,
99 'pagi.sse.retry' => $retry,
100 };
101  
102 2 await $app->($new_scope, $receive, $wrapped_send);
103 3 };
104 }
105  
106 2 1;
107  
108 __END__
« lib/PAGI/Test/WebSocket.pm ↑ Summary blib/lib/PAGI/WebSocket.pm »