| 1 | package Langertha::Knarr::Stream; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Async chunk iterator returned by streaming Knarr handlers | ||||||||||||||||||||||||||||
| 3 | 16 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||
| 4 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub from_callback { | ||||||||||||||||||||||||||||
| 9 | 0 | ▶ | my ($class, $setup) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_callback: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 0 | my @queue; | |||||||||||||||||||||||||||
| 11 | my $pending; | ||||||||||||||||||||||||||||
| 12 | 0 | my $finished = 0; | |||||||||||||||||||||||||||
| 13 | 0 | my $error; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | my $deliver = sub { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my ($v) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | if ( $pending ) { my $p = $pending; $pending = undef; $p->done($v) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 0 | else { push @queue, $v } | |||||||||||||||||||||||||||
| 19 | 0 | }; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my $emit = sub { | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my ($chunk) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | return unless defined $chunk && length $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $chunk and length $chunk
MC/DC0%0/2
defined $chunk and length $chunk
defined $chunk
length $chunk
Truth table0%0/3
A and B
A= defined $chunkB= length $chunk
| |||||||||||||||||||||||||||||
| 24 | 0 | $deliver->($chunk); | |||||||||||||||||||||||||||
| 25 | 0 | }; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $done = sub { $finished = 1; $deliver->(undef) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $fail = sub { $error = $_[0] // 'unknown error'; $finished = 1; $deliver->(undef) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0] // "unknown error"
MC/DC0%0/1
$_[0] // "unknown error"
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | $setup->($emit, $done, $fail); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | return $class->new( | ||||||||||||||||||||||||||||
| 32 | source => sub { | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( @queue ) { return Future->done( shift @queue ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( $finished ) { return $error ? Future->fail($error) : Future->done(undef) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 0 | $pending = Future->new; | |||||||||||||||||||||||||||
| 36 | 0 | return $pending; | |||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | 0 | ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Two ways to construct: | ||||||||||||||||||||||||||||
| 42 | # 1) generator => sub { ... } — sync coderef returning next string or undef | ||||||||||||||||||||||||||||
| 43 | # 2) source => sub { ... } — coderef returning a Future[string|undef] | ||||||||||||||||||||||||||||
| 44 | 16 | has generator => ( is => 'ro', isa => 'Maybe[CodeRef]' ); | |||||||||||||||||||||||||||
| 45 | 16 | has source => ( is => 'ro', isa => 'Maybe[CodeRef]' ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub next_chunk_f { | ||||||||||||||||||||||||||||
| 48 | 73 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_chunk_f: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 73 | ▶ | if ( my $g = $self->generator ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 57 | my $v = $g->(); | |||||||||||||||||||||||||||
| 51 | 57 | return Future->done($v); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 16 | ▶ | if ( my $s = $self->source ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 16 | return $s->(); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | return Future->done(undef); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Convenience: build a stream from a fixed list of chunks | ||||||||||||||||||||||||||||
| 60 | sub from_list { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ($class, @chunks) = @_; | ||||||||||||||||||||||||||
|
Subroutine
from_list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 0 | my @queue = @chunks; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | return $class->new( generator => sub { @queue ? shift @queue : undef } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 67 | 16 | 1; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =pod | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 NAME | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Langertha::Knarr::Stream - Async chunk iterator returned by streaming Knarr handlers | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 VERSION | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | version 1.100 | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | use Langertha::Knarr::Stream; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # From a fixed list of strings | ||||||||||||||||||||||||||||
| 88 | my $stream = Langertha::Knarr::Stream->from_list('hel', 'lo'); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # From a sync generator | ||||||||||||||||||||||||||||
| 91 | my @parts = ('hel', 'lo'); | ||||||||||||||||||||||||||||
| 92 | my $stream = Langertha::Knarr::Stream->new( | ||||||||||||||||||||||||||||
| 93 | generator => sub { @parts ? shift @parts : undef }, | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # From a future-yielding source (real async) | ||||||||||||||||||||||||||||
| 97 | my $stream = Langertha::Knarr::Stream->new( | ||||||||||||||||||||||||||||
| 98 | source => sub { $next_chunk_future }, | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Drain it | ||||||||||||||||||||||||||||
| 102 | while ( defined( my $chunk = $stream->next_chunk_f->get ) ) { | ||||||||||||||||||||||||||||
| 103 | print $chunk; | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | The chunk iterator that streaming Knarr handlers return. Supports two | ||||||||||||||||||||||||||||
| 109 | construction modes: a sync C<generator> coderef that returns the next | ||||||||||||||||||||||||||||
| 110 | chunk string each call (or C<undef> for end), or a C<source> coderef | ||||||||||||||||||||||||||||
| 111 | that returns a L<Future> resolving to the next chunk string. The | ||||||||||||||||||||||||||||
| 112 | Future form is the one real async backends like L<Net::Async::HTTP> | ||||||||||||||||||||||||||||
| 113 | use; the generator form is for tests and simple cases. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 generator | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Optional. CodeRef returning the next chunk synchronously. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 source | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Optional. CodeRef returning a L<Future> that resolves to the next | ||||||||||||||||||||||||||||
| 122 | chunk. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 next_chunk_f | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Returns a L<Future> resolving to the next chunk string, or C<undef> | ||||||||||||||||||||||||||||
| 127 | when the stream is exhausted. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 from_list | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | my $stream = Langertha::Knarr::Stream->from_list(@chunks); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Convenience constructor that builds a stream from a fixed list of | ||||||||||||||||||||||||||||
| 134 | chunk strings. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 from_callback | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | my $stream = Langertha::Knarr::Stream->from_callback( sub { | ||||||||||||||||||||||||||||
| 139 | my ($emit, $done, $fail) = @_; | ||||||||||||||||||||||||||||
| 140 | my $f = $engine->simple_chat_stream_realtime_f( | ||||||||||||||||||||||||||||
| 141 | sub { $emit->( $_[0]->content ) }, | ||||||||||||||||||||||||||||
| 142 | @messages, | ||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||
| 144 | $f->on_done( $done ); | ||||||||||||||||||||||||||||
| 145 | $f->on_fail( $fail ); | ||||||||||||||||||||||||||||
| 146 | $f->retain; | ||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Builds a stream backed by a callback-driven producer. The setup sub | ||||||||||||||||||||||||||||
| 150 | receives three callbacks — C<$emit-E<gt>($chunk)>, C<$done-E<gt>()>, | ||||||||||||||||||||||||||||
| 151 | C<$fail-E<gt>($err)> — and is expected to wire them to the underlying | ||||||||||||||||||||||||||||
| 152 | async source. Internally maintains a queue and pending Future so the | ||||||||||||||||||||||||||||
| 153 | consumer side can sit on C<next_chunk_f> without polling. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This is the canonical replacement for the queue/pending/finished/error | ||||||||||||||||||||||||||||
| 156 | pump that engine-backed handlers used to inline. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 Issues | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 163 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 IRC | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 182 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||