| 1 | package PAGI::Middleware::SSE::Retry; | ||
| 2 | $PAGI::Middleware::SSE::Retry::VERSION = '0.002000'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent 'PAGI::Middleware'; | ||
| 6 | use Future::AsyncAwait; | ||
| 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 | sub _init { | ||
| 48 | my ($self, $config) = @_; | ||
| 49 | |||
| 50 | $self->{retry} = $config->{retry} // 3000; | ||
| 51 | $self->{include_on_start} = $config->{include_on_start} // 1; | ||
| 52 | $self->{include_on_events} = $config->{include_on_events} // 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub wrap { | ||
| 56 | my ($self, $app) = @_; | ||
| 57 | |||
| 58 | return async sub { | ||
| 59 | my ($scope, $receive, $send) = @_; | ||
| 60 | # Only apply to SSE connections | ||
| 61 | if ($scope->{type} ne 'sse') { | ||
| 62 | await $app->($scope, $receive, $send); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | my $retry = $self->{retry}; | ||
| 67 | my $sent_initial = 0; | ||
| 68 | |||
| 69 | # Wrap send to add retry field | ||
| 70 | my $wrapped_send = async sub { | ||
| 71 | my ($event) = @_; | ||
| 72 | if ($event->{type} eq 'sse.start') { | ||
| 73 | await $send->($event); | ||
| 74 | |||
| 75 | # Send initial retry hint | ||
| 76 | if ($self->{include_on_start} && !$sent_initial) { | ||
| 77 | $sent_initial = 1; | ||
| 78 | await $send->({ | ||
| 79 | type => 'sse.send', | ||
| 80 | retry => $retry, | ||
| 81 | }); | ||
| 82 | } | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | if ($event->{type} eq 'sse.send') { | ||
| 87 | # Add retry to event if configured and not already present | ||
| 88 | if ($self->{include_on_events} && !defined $event->{retry}) { | ||
| 89 | $event = { %$event, retry => $retry }; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | await $send->($event); | ||
| 94 | }; | ||
| 95 | |||
| 96 | # Add retry info to scope | ||
| 97 | my $new_scope = { | ||
| 98 | %$scope, | ||
| 99 | 'pagi.sse.retry' => $retry, | ||
| 100 | }; | ||
| 101 | |||
| 102 | await $app->($new_scope, $receive, $wrapped_send); | ||
| 103 | }; | ||
| 104 | } | ||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | __END__ |