NAME

PAGI::Utils - Shared utility helpers for PAGI

SYNOPSIS

use PAGI::Utils qw(handle_lifespan);

return await handle_lifespan($scope, $receive, $send,
    startup  => async sub { my ($state) = @_; ... },
    shutdown => async sub { my ($state) = @_; ... },
) if $scope->{type} eq 'lifespan';

FUNCTIONS

handle_lifespan

return await handle_lifespan($scope, $receive, $send,
    startup  => async sub { my ($state) = @_; ... },
    shutdown => async sub { my ($state) = @_; ... },
) if $scope->{type} eq 'lifespan';

Consumes lifespan events, runs registered startup/shutdown hooks, and sends the appropriate completion messages. Hooks are taken from $scope->{'pagi.lifespan.handlers'}, and optional startup and shutdown callbacks can be passed in via %opts.

Important: This function will croak if called with a non-lifespan scope. Always check $scope->{type} eq 'lifespan' before calling, as shown in the synopsis.

to_app

use PAGI::Utils qw(to_app);

my $app = to_app($thing);

Coerce $thing into a PAGI application (an async coderef). Accepts:

  • a coderef - returned unchanged

  • an object with a to_app method - compiled by calling it

  • a class name with a to_app method - auto-required if needed, then compiled by calling $class->to_app

Anything else croaks. A middleware object or class (something with wrap but no to_app) gets a croak pointing at enable() instead, since middleware belongs in middleware position, not app position.

All composition points in this distribution (builder mounts, router targets, cascades, the test client) call this for you, so user code can pass components and class names directly:

mount '/static' => PAGI::App::File->new(root => $dir);
mount '/api'    => 'MyApp::API';