| 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
| |||||||||||||
| 29 | 2 | await $sse->keepalive($keepalive); | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | # Register disconnect callback | ||||||||||||
| 33 | 3 | ▶ | if ($self->can('on_disconnect')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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"} // ""
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
| |||||||||||||
| 59 | 3 | ▶ | croak "Expected sse scope, got '$type'" unless $type eq 'sse'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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__ | ||||||||||||