Ryu/Source.pm

filter: Statementstmt 63.1% 606 / 960 Branchbran 56.4% 193 / 342 Conditioncond 55.0% 99 / 180 MC/DCmcdc 37.7% 40 / 106 Subroutinesub 58.8% 153 / 260 Podpod 91.3% 74 / 81 totaltotal 60.3% 1165 / 1929 CC 265 SCAR 20.0
CC
265
Cov
61%
emit_batch
90.0
__ANON__
42.0
take_until
42.0
CRAP 7.4
« Ryu/Node.pm ↑ Summary Ryu/Buffer.pm »
1 package Ryu::Source;
2  
3 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 43 ▶use parent qw(Ryu::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 43 our $VERSION = '4.001'; # VERSION
9 43 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
10  
11 =head1 NAME
12  
13 Ryu::Source - base representation for a source of events
14  
15 =head1 SYNOPSIS
16  
17 my $src = Ryu::Source->new;
18 my $chained = $src->map(sub { $_ * $_ })->prefix('value: ')->say;
19 $src->emit($_) for 1..5;
20 $src->finish;
21  
22 =head1 DESCRIPTION
23  
24 This is probably the module you'd want to start with, if you were going to be
25 using any of this. There's a disclaimer in L<Ryu> that may be relevant at this
26 point.
27  
28 =head2 Quick start
29  
30 You'd normally want to start by creating a L<Ryu::Source> instance:
31  
32 my $src = Ryu::Source->new;
33  
34 If you're dealing with L<IO::Async> code, use L<Ryu::Async> to ensure that you
35 get properly awaitable L<Future> instances:
36  
37 $loop->add(my $ryu = Ryu::Async->new);
38 my $src = $ryu->source;
39  
40 Once you have a source, you'll need two things:
41  
42 =over 4
43  
44 =item * items to put into one end
45  
46 =item * processing to attach to the other end
47  
48 =back
49  
50 For the first, call L</emit>:
51  
52 use Future::AsyncAwait;
53 # 1s drifting periodic timer
54 while(1) {
55 await $loop->delay_future(after => 1);
56 $src->emit('');
57 }
58  
59 For the second, this would be L</each>:
60  
61 $src->each(sub { print "Had timer tick\n" });
62  
63 So far, not so useful - the power of this type of reactive programming is in the
64 ability to chain and combine disparate event sources.
65  
66 At this point, L<https://rxmarbles.com> is worth a visit - this provides a clear
67 visual demonstration of how to combine multiple event streams using the chaining
68 methods. Most of the API here is modelled after similar principles.
69  
70 First, the L</map> method: this provides a way to transform each item into
71 something else:
72  
73 $src->map(do { my $count = 0; sub { ++$count } })
74 ->each(sub { print "Count is now $_\n" })
75  
76 Next, L</filter> provides an equivalent to Perl's L<grep> functionality:
77  
78 $src->map(do { my $count = 0; sub { ++$count } })
79 ->filter(sub { $_ % 2 })
80 ->each(sub { print "Count is now at an odd number: $_\n" })
81  
82 You can stack these:
83  
84 $src->map(do { my $count = 0; sub { ++$count } })
85 ->filter(sub { $_ % 2 })
86 ->filter(sub { $_ % 5 })
87 ->each(sub { print "Count is now at an odd number which is not divisible by 5: $_\n" })
88  
89 or:
90  
91 $src->map(do { my $count = 0; sub { ++$count } })
92 ->map(sub { $_ % 3 ? 'fizz' : $_ })
93 ->map(sub { $_ % 5 ? 'buzz' : $_ })
94 ->each(sub { print "An imperfect attempt at the fizz-buzz game: $_\n" })
95  
96 =cut
97  
98 43 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 43 ▶use sort qw(stable);
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 43 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
102 43 ▶use Ref::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
103 43 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
104 43 ▶use List::UtilsBy;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 43 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
106 43 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 43 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 43 ▶use Future::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 43 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 43 ▶use Ryu::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 43 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 =head1 GLOBALS
116  
117 =head2 $FUTURE_FACTORY
118  
119 This is a coderef which should return a new L<Future>-compatible instance.
120  
121 Example overrides might include:
122  
123 $Ryu::Source::FUTURE_FACTORY = sub { Mojo::Future->new->set_label($_[1]) };
124  
125 =cut
126  
127 229 ▶our $FUTURE_FACTORY = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 229 Future->new->set_label($_[1])
129 43 };
130  
131 =head2 %ENCODER
132  
133 An encoder is a coderef which takes input and returns output.
134  
135 =cut
136  
137 our %ENCODER = (
138 0 ▶ utf8 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 Encode::encode_utf8($_)
141 }
142 0 },
143 1 ▶ json => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 1 require JSON::MaybeXS;
145 1 my $json = JSON::MaybeXS->new(@_);
146 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 $json->encode($_)
148 }
149 1 },
150 0 ▶ csv => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 require Text::CSV;
152 0 my $csv = Text::CSV->new(@_);
153 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
154 0 ▶ die $csv->error_input unless $csv->combine(@$_);
Branch: 1 branch
BranchTrueFalseTotal
unless $csv->combine(@$_) 0 0 0
155 0 $csv->string
156 }
157 0 },
158 1 ▶ base64 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 require MIME::Base64;
160 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 1 MIME::Base64::encode_base64($_, '');
162 }
163 1 },
164 43 );
165 # The naming of this one is a perennial source of confusion in Perl,
166 # let's just support both
167 43 $ENCODER{'UTF-8'} = $ENCODER{utf8};
168  
169 our %DECODER = (
170 0 ▶ utf8 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 my $data = '';
172 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 $data .= $_;
174 0 Encode::decode_utf8($data, Encode::FB_QUIET)
175 }
176 0 },
177 0 ▶ json => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 require JSON::MaybeXS;
179 0 my $json = JSON::MaybeXS->new(@_);
180 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 $json->decode($_)
182 }
183 0 },
184 0 ▶ csv => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 0 require Text::CSV;
186 0 my $csv = Text::CSV->new(@_);
187 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 ▶ die $csv->error_input unless $csv->parse($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $csv->parse($_) 0 0 0
189 0 [ $csv->fields ]
190 }
191 0 },
192 0 ▶ base64 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 require MIME::Base64;
194 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 0 MIME::Base64::decode_base64($_);
196 }
197 0 },
198 43 );
199 43 $DECODER{'UTF-8'} = $DECODER{utf8};
200  
201 =head1 METHODS
202  
203 =head2 new
204  
205 Takes named parameters, such as:
206  
207 =over 4
208  
209 =item * label - the label used in descriptions
210  
211 =back
212  
213 Note that this is rarely called directly, see L</from>, L</empty> and L</never> instead.
214  
215 =cut
216  
217 223 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
218 223 my ($self, %args) = @_;
219 223 ▶ $args{label} //= 'unknown';
Condition100%2/2
$args{"label"} //= "unknown"
l!l
119104
MC/DC100%1/1
$args{"label"} //= "unknown"
$args{"label"}
Truth table100%2/2
A
A= $args{"label"}
Aresult
11
00
220 223 ▶ $args{on_item} //= [];
Condition50%1/2
$args{"on_item"} //= []
l!l
0223
MC/DC0%0/1
$args{"on_item"} //= []
$args{"on_item"}
Truth table50%1/2
A
A= $args{"on_item"}
Aresult
11
00
221 223 ▶ $args{on_batch} //= [];
Condition50%1/2
$args{"on_batch"} //= []
l!l
0223
MC/DC0%0/1
$args{"on_batch"} //= []
$args{"on_batch"}
Truth table50%1/2
A
A= $args{"on_batch"}
Aresult
11
00
222 223 $self->SUPER::new(%args);
223 }
224  
225 =head2 from
226  
227 Creates a new source from things.
228  
229 The precise details of what this method supports may be somewhat ill-defined at this point in time.
230 It is expected that the interface and internals of this method will vary greatly in versions to come.
231  
232 At the moment, the following inputs are supported:
233  
234 =over 4
235  
236 =item * arrayref - when called as C<< ->from([1,2,3]) >> this will emit the values from the arrayref,
237 deferring until the source is started
238  
239 =item * L<Future> - given a L<Future> instance, will emit the results when that L<Future> is marked as done
240  
241 =item * file handle - if provided a filehandle, such as C<< ->from(\*STDIN) >>, this will read bytes and
242 emit those until EOF
243  
244 =back
245  
246 =cut
247  
248 1 ▶sub from {
Subroutine from: called CC=8 SCAR=33.9
Pod documented
249 1 my $class = shift;
250 1 ▶ my $src = (ref $class) ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 1 0 1
251 1 ▶ if(my $from_class = Scalar::Util::blessed($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $from_class = builtin::blessed($_[0])) { } 0 1 1
elsif (my $ref = ref $_[0]) { } 1 0 1
252 0 ▶ if($from_class->isa('Future')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_class->isa('Future')) { } 0 0 0
253 0 ▶ $_[0]->on_ready(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
254 0 my ($f) = @_;
255 0 ▶ if($f->failure) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f->failure) { } 0 0 0
elsif (not $f->is_cancelled) { } 0 0 0
256 0 $src->fail($f->from_future);
257 } elsif(!$f->is_cancelled) {
258 0 $src->finish;
259 } else {
260 0 $src->emit($f->get);
261 0 $src->finish;
262 }
263 0 })->retain;
264 0 return $src;
265 } else {
266 0 die 'Unknown class ' . $from_class . ', cannot turn it into a source';
267 }
268 } elsif(my $ref = ref($_[0])) {
269 1 ▶ if($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 1 0 1
elsif ($ref eq 'GLOB') { } 0 0 0
270 1 my $data = $_[0];
271 1 ▶ $src->{on_get} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
272 1 ▶ while($data->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data) 3 1 4
273 3 $src->emit(shift $data->@*);
274 }
275 1 $src->finish;
276 1 };
277 1 return $src;
278 } elsif($ref eq 'GLOB') {
279 0 ▶ if(my $fh = *{$_[0]}{IO}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = *{$_[0];}{'IO'}) { } 0 0 0
280 0 ▶ my $code = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
281 0 ▶ while(read $fh, my $buf, 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $buf, 4096) 0 0 0
282 0 $src->emit($buf)
283 }
284 $src->finish
285 0 };
286 0 $src->{on_get} = $code;
287 0 return $src;
288 } else {
289 0 die "have a GLOB with no IO entry, this is not supported"
290 }
291 }
292 0 die "unsupported ref type $ref";
293 } else {
294 0 die "unknown item in ->from";
295 }
296 }
297  
298 =head2 empty
299  
300 Creates an empty source, which finishes immediately.
301  
302 =cut
303  
304 0 ▶sub empty {
Subroutine empty: not called CC=1 SCAR=6.9
Pod documented
305 0 my ($class) = @_;
306  
307 0 $class->new(label => (caller 0)[3] =~ /::([^:]+)$/)->finish
308 }
309  
310 =head2 never
311  
312 An empty source that never finishes.
313  
314 =cut
315  
316 0 ▶sub never {
Subroutine never: not called CC=1 SCAR=6.9
Pod documented
317 0 my ($class) = @_;
318  
319 0 $class->new(label => (caller 0)[3] =~ /::([^:]+)$/)
320 }
321  
322 =head1 METHODS - Instance
323  
324 =cut
325  
326 =head2 encode
327  
328 Passes each item through an encoder.
329  
330 The first parameter is the encoder to use, the remainder are
331 used as options for the selected encoder.
332  
333 Examples:
334  
335 $src->encode('json')
336 $src->encode('utf8')
337 $src->encode('base64')
338  
339 =cut
340  
341 2 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.8
Pod documented
342 2 my ($self, $type) = splice @_, 0, 2;
343 2 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
344 2 ▶ my $code = ($ENCODER{$type} || $self->can('encode_' . $type) or die "unsupported encoding $type")->(@_);
Condition40%2/5
$Ryu::Source::ENCODER{$type} || $self->can('encode_' . $type) || die("unsupported encoding $type")
l!l
20
$Ryu::Source::ENCODER{$type} or $self->can('encode_' . $type)
l!l&&r!l&&!r
200
MC/DC0%0/2
$Ryu::Source::ENCODER{$type} || $self->can('encode_' . $type) || die("unsupported encoding $type")
$Ryu::Source::ENCODER{$type} $self->can('encode_' . $type)
Truth table33%1/3
A or B
A= $Ryu::Source::ENCODER{$type}B= $self->can('encode_' . $type)
ABresult
1X1
011
000
345 2 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
346 2 $src->emit($code->($_))
347 2 }, $src);
348 }
349  
350 =head2 decode
351  
352 Passes each item through a decoder.
353  
354 The first parameter is the decoder to use, the remainder are
355 used as options for the selected decoder.
356  
357 Examples:
358  
359 $src->decode('json')
360 $src->decode('utf8')
361 $src->decode('base64')
362  
363 =cut
364  
365 0 ▶sub decode {
Subroutine decode: not called CC=3 SCAR=24.8
Pod documented
366 0 my ($self, $type) = splice @_, 0, 2;
367 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
368 0 ▶ my $code = ($DECODER{$type} || $self->can('decode_' . $type) or die "unsupported encoding $type")->(@_);
Condition0%0/5
$Ryu::Source::DECODER{$type} || $self->can('decode_' . $type) || die("unsupported encoding $type")
l!l
00
$Ryu::Source::DECODER{$type} or $self->can('decode_' . $type)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Ryu::Source::DECODER{$type} || $self->can('decode_' . $type) || die("unsupported encoding $type")
$Ryu::Source::DECODER{$type} $self->can('decode_' . $type)
Truth table0%0/3
A or B
A= $Ryu::Source::DECODER{$type}B= $self->can('decode_' . $type)
ABresult
1X1
011
000
369 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
370 0 $src->emit($code->($_))
371 0 }, $src);
372 }
373  
374 =head2 print
375  
376 Shortcut for C<< ->each(sub { print }) >>, except this will
377 also save the initial state of C< $\ > and use that for each
378 call for consistency.
379  
380 =cut
381  
382 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
383 0 my ($self) = @_;
384 0 my $delim = $\;
385 0 ▶ $self->each(sub { local $\ = $delim; print });
Subroutine __ANON__: not called CC=1 SCAR=6.9
386 }
387  
388 =head2 say
389  
390 Shortcut for C<< ->each(sub { print "$_\n" }) >>.
391  
392 =cut
393  
394 0 ▶sub say {
Subroutine say: not called CC=1 SCAR=6.9
Pod documented
395 0 my ($self) = @_;
396 0 ▶ $self->each(sub { local $\; print "$_\n" });
Subroutine __ANON__: not called CC=1 SCAR=6.9
397 }
398  
399 =head2 hexdump
400  
401 Convert input bytes to a hexdump representation, for example:
402  
403 00000000 00 00 12 04 00 00 00 00 00 00 03 00 00 00 80 00 >................<
404 00000010 04 00 01 00 00 00 05 00 ff ff ff 00 00 04 08 00 >................<
405 00000020 00 00 00 00 7f ff 00 00 >........<
406  
407 One line is emitted for each 16 bytes.
408  
409 Takes the following named parameters:
410  
411 =over 4
412  
413 =item * C<continuous> - accumulates data for a continuous stream, and
414 does not reset the offset counter. Note that this may cause the last
415 output to be delayed until the source completes.
416  
417 =back
418  
419 =cut
420  
421 0 ▶sub hexdump {
Subroutine hexdump: not called CC=1 SCAR=6.9
Pod documented
422 0 my ($self, %args) = @_;
423  
424 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
425 0 my $offset = 0;
426 0 my $in = '';
427 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
428 0 my @out;
429 0 ▶ if($args{continuous}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'continuous'}) { } 0 0 0
430 0 $in .= $_;
431 0 ▶ return if length($in) < 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $in < 16 0 0 0
432 } else {
433 0 $in = $_;
434 0 $offset = 0;
435 }
436 0 ▶ while(length(my $bytes = substr $in, 0, List::Util::min(length($in), 16), '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $bytes = substr($in, 0, &List::Util::min(length $in, 16), ''))) 0 0 0
437 0 my $encoded = join '', unpack 'H*' => $bytes;
438 0 $encoded =~ s/[[:xdigit:]]{2}\K(?=[[:xdigit:]])/ /g;
439 0 my $ascii = $bytes =~ s{[^[:print:]]}{.}gr;
440 0 $src->emit(sprintf '%08x %-47.47s %-18.18s', $offset, $encoded, ">$ascii<");
441 0 $offset += length($bytes);
442 0 ▶ return if $args{continuous} and length($in) < 16;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"continuous"} and length $in < 16 0 0 0
Condition0%0/3
$args{"continuous"} and length $in < 16
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"continuous"} and length $in < 16
$args{"continuous"} length $in < 16
Truth table0%0/3
A and B
A= $args{"continuous"}B= length $in < 16
ABresult
0X0
100
111
443 }
444 0 }, $src);
445 }
446  
447 =head2 throw
448  
449 Throws something. I don't know what, maybe a chair.
450  
451 =cut
452  
453 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod documented
454 0 my $src = shift->new(@_);
455 0 $src->fail('...');
456 }
457  
458 =head2 debounce
459  
460 Not yet implemented.
461  
462 Requires timing support, see implementations such as L<Ryu::Async> instead.
463  
464 =cut
465  
466 0 ▶sub debounce {
Subroutine debounce: not called CC=1 SCAR=6.9
Pod documented
467 0 my ($self, $interval) = @_;
468 ...
469 0 }
470  
471 =head2 chomp
472  
473 Chomps all items with the given delimiter.
474  
475 Once you've instantiated this, it will stick with the delimiter which was in force at the time of instantiation.
476 Said delimiter follows the usual rules of C<< $/ >>, whatever they happen to be.
477  
478 Example:
479  
480 $ryu->stdin
481 ->chomp("\n")
482 ->say
483  
484 =cut
485  
486 0 ▶sub chomp {
Subroutine chomp: not called CC=2 SCAR=17.9
Pod documented
487 0 my ($self, $delim) = @_;
488 0 ▶ $delim //= $/;
Condition0%0/3
$delim //= $/
l!l&&r!l&&!r
000
MC/DC0%0/2
$delim //= $/
$delim $/
Truth table0%0/3
A //= B
A= $delimB= $/
ABresult
1X1
011
000
489 0 ▶ $self->map(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
490 0 local $/ = $delim;
491 0 chomp(my $line = $_);
492 0 $line
493 })
494 0 }
495  
496 =head2 map
497  
498 A bit like L<perlfunc/map>.
499  
500 Takes a single parameter - the coderef to execute for each item. This should return
501 a scalar value which will be used as the next item.
502  
503 Often useful in conjunction with a C<< do >> block to provide a closure.
504  
505 Examples:
506  
507 $src->map(do {
508 my $idx = 0;
509 sub {
510 [ @$_, ++$idx ]
511 }
512 })
513  
514 =cut
515  
516 3 ▶sub map : method {
Subroutine map: called CC=1 SCAR=0.0
Pod documented
517 3 my ($self, $code) = @_;
518  
519 3 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
520 10 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
521 $src->emit(Scalar::Util::blessed($_)
522 ? (scalar $_->$code)
523 : !ref($code)
524 10 ▶ ? $_->{$code}
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_) ? : 3 7 10
!ref($code) ? : 4 3 7
525 : scalar $_->$code
526 )
527 3 }, $src);
528 }
529  
530 =head2 flat_map
531  
532 Similar to L</map>, but will flatten out some items:
533  
534 =over 4
535  
536 =item * an arrayref will be expanded out to emit the individual elements
537  
538 =item * for a L<Ryu::Source>, passes on any emitted elements
539  
540 =back
541  
542 This also means you can "merge" items from a series of sources.
543  
544 Note that this is not recursive - an arrayref of arrayrefs will be expanded out
545 into the child arrayrefs, but no further.
546  
547 Failure on any input source will cause this source to be marked as failed as well.
548  
549 =cut
550  
551 5 ▶sub flat_map {
Subroutine flat_map: called CC=2 SCAR=7.1
Pod documented
552 5 my ($self, $code) = splice @_, 0, 2;
553  
554 # Upgrade ->flat_map(method => args...) to a coderef
555 5 ▶ if(!Ref::Util::is_plain_coderef($code)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Ref::Util::XS::is_plain_coderef($code)) 0 5 5
556 0 my $method = $code;
557 0 my @args = @_;
558 0 ▶ $code = sub { $_->$method(@args) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
559 0 }
560  
561 5 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
562  
563 5 Scalar::Util::weaken(my $weak_sauce = $src);
564 9 ▶ my $add = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
565 9 my $v = shift;
566 9 ▶ my $src = $weak_sauce or return;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $weak_sauce or return 9 0 9
567  
568 9 my $k = "$v";
569 5 ▶ $src->{waiting}{$k} = $v->on_ready(sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
570 5 my ($f) = @_;
571 5 ▶ return unless my $src = $weak_sauce;
Branch: 1 branch
BranchTrueFalseTotal
unless my $src = $weak_sauce 0 5 5
572  
573 # Any failed input source should propagate failure immediately
574 5 ▶ if($f->is_failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_failed) 2 3 5
575 # Clear out our waitlist, since we don't want to hold those references any more
576 2 delete $src->{waiting};
577 2 ▶ $src->fail($f->failure) unless $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 2 0 2
578 2 return;
579 }
580  
581 3 delete $src->{waiting}{$k};
582 3 ▶ $src->finish unless %{$src->{waiting}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$src->{"waiting"};} 2 1 3
583 9 });
584 9 $log->tracef("Added %s which will bring our count to %d", $k, 0 + keys %{$src->{waiting}});
585 5 };
586  
587 5 $add->($self->_completed);
588 7 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=8 SCAR=21.3
589 7 ▶ my $src = $weak_sauce or return;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $weak_sauce or return 7 0 7
590 7 for ($code->($_)) {
591 7 my $item = $_;
592 7 ▶ if(Ref::Util::is_plain_arrayref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($item)) { } 3 4 7
elsif (builtin::blessed($item) and $item->isa(__PACKAGE__)) { } 4 0 4
593 3 $log->tracef("Have an arrayref of %d items", 0 + @$item);
594 3 for(@$item) {
595 9 ▶ last if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 9 9
596 9 $src->emit($_);
597 }
598 ▶ } elsif(Scalar::Util::blessed($item) && $item->isa(__PACKAGE__)) {
Condition33%1/3
builtin::blessed($item) and $item->isa(__PACKAGE__)
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($item) and $item->isa(__PACKAGE__)
builtin::blessed($item) $item->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($item)B= $item->isa(__PACKAGE__)
ABresult
0X0
100
111
599 4 $log->tracef("This item is a source");
600 2 ▶ $src->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
601 2 ▶ return if $item->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $item->is_ready 1 1 2
602 1 $log->tracef("Marking %s as ready because %s was", $item->describe, $src->describe);
603 1 shift->on_ready($item->_completed);
604 4 });
605 4 $add->($item->_completed);
606 5 ▶ $item->each_while_source(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
607 5 ▶ my $src = $weak_sauce or return;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $weak_sauce or return 5 0 5
608 5 $src->emit($_)
609 2 ▶ }, $src)->on_ready(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
610 2 undef $item;
611 4 });
612 }
613 }
614 5 }, $src);
615 5 $src
616 }
617  
618 =head2 split
619  
620 Splits the input on the given delimiter.
621  
622 By default, will split into characters.
623  
624 Note that each item will be processed separately - the buffer won't be
625 retained across items, see L</by_line> for that.
626  
627 =cut
628  
629 0 ▶sub split : method {
Subroutine split: not called CC=2 SCAR=17.9
Pod documented
630 0 my ($self, $delim) = @_;
631 0 ▶ $delim //= qr//;
Condition0%0/2
$delim //= qr//
l!l
00
MC/DC0%0/1
$delim //= qr//
$delim
Truth table0%0/2
A
A= $delim
Aresult
11
00
632  
633 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
634 0 ▶ $self->each_while_source(sub { $src->emit($_) for split $delim, $_ }, $src);
Subroutine __ANON__: not called CC=2 SCAR=17.9
635 }
636  
637 =head2 chunksize
638  
639 Splits input into fixed-size chunks.
640  
641 Note that output is always guaranteed to be a full chunk - if there is partial input
642 at the time the input stream finishes, those extra bytes will be discarded.
643  
644 =cut
645  
646 0 ▶sub chunksize : method {
Subroutine chunksize: not called CC=3 SCAR=24.8
Pod documented
647 0 my ($self, $size) = @_;
648 0 ▶ die 'need positive chunk size parameter' unless $size && $size > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $size and $size > 0 0 0 0
Condition0%0/3
$size and $size > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$size and $size > 0
$size $size > 0
Truth table0%0/3
A and B
A= $sizeB= $size > 0
ABresult
0X0
100
111
649  
650 0 my $buffer = '';
651 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
652 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
653 0 $buffer .= $_;
654 0 ▶ $src->emit(substr $buffer, 0, $size, '') while length($buffer) >= $size;
Branch: 1 branch
BranchTrueFalseTotal
length $buffer >= $size and do { $src->emit(substr($buffer, 0, $size, "")) } 0 0 0
655 0 }, $src);
656 }
657  
658 =head2 batch
659  
660 Splits input into arrayref batches of a given size.
661  
662 Note that the last item emitted may have fewer elements (or none at all).
663  
664 $src->batch(10)
665 ->map(sub { "Next 10 (or fewer) items: @$_" })
666 ->say;
667  
668 =cut
669  
670 1 ▶sub batch : method {
Subroutine batch: called CC=3 SCAR=11.3
Pod documented
671 1 my ($self, $size) = @_;
672 1 ▶ die 'need positive batch parameter' unless $size && $size > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $size and $size > 0 0 1 1
Condition33%1/3
$size and $size > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$size and $size > 0
$size $size > 0
Truth table33%1/3
A and B
A= $sizeB= $size > 0
ABresult
0X0
100
111
673  
674 1 my $buffer = '';
675 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
676 1 my @batch;
677 4 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
678 4 push @batch, $_;
679 4 ▶ while(@batch >= $size and my (@items) = splice @batch, 0, $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@batch >= $size and my(@items) = splice(@batch, 0, $size)) 1 4 5
Condition66%2/3
@batch >= $size and my(@items) = splice(@batch, 0, $size)
!ll&&!rl&&r
401
MC/DC50%1/2
@batch >= $size and my(@items) = splice(@batch, 0, $size)
@batch >= $size my(@items) = splice(@batch, 0, $size)
Truth table66%2/3
A and B
A= @batch >= $sizeB= my(@items) = splice(@batch, 0, $size)
ABresult
0X0
100
111
680 1 $src->emit(\@items)
681 }
682 1 ▶ }, $src, cleanup => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
683 1 ▶ $src->emit([ splice @batch ]) if @batch;
Branch: 1 branch
BranchTrueFalseTotal
if @batch 0 1 1
684 1 });
685 }
686  
687 =head2 by_line
688  
689 Emits one item for each line in the input. Similar to L</split> with a C<< \n >> parameter,
690 except this will accumulate the buffer over successive items and only emit when a complete
691 line has been extracted.
692  
693 =cut
694  
695 0 ▶sub by_line : method {
Subroutine by_line: not called CC=2 SCAR=17.9
Pod documented
696 0 my ($self, $delim) = @_;
697 0 ▶ $delim //= $/;
Condition0%0/3
$delim //= $/
l!l&&r!l&&!r
000
MC/DC0%0/2
$delim //= $/
$delim $/
Truth table0%0/3
A //= B
A= $delimB= $/
ABresult
1X1
011
000
698  
699 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
700 0 my $buffer = '';
701 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
702 0 $buffer .= $_;
703 0 ▶ while($buffer =~ s/^(.*)\Q$delim//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ s/^(.*)\Q$delim\E//) 0 0 0
704 0 $src->emit($1)
705 }
706 0 }, $src);
707 }
708  
709 =head2 prefix
710  
711 Applies a string prefix to each item.
712  
713 =cut
714  
715 1 ▶sub prefix {
Subroutine prefix: called CC=1 SCAR=0.0
Pod documented
716 1 my ($self, $txt) = @_;
717 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
718 3 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
719 3 $src->emit($txt . $_)
720 1 }, $src);
721 }
722  
723 =head2 suffix
724  
725 Applies a string suffix to each item.
726  
727 =cut
728  
729 1 ▶sub suffix {
Subroutine suffix: called CC=1 SCAR=0.0
Pod documented
730 1 my ($self, $txt) = @_;
731 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
732 3 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
733 3 $src->emit($_ . $txt)
734 1 }, $src);
735 }
736  
737 =head2 sprintf_methods
738  
739 Convenience method for generating a string from a L</sprintf>-style format
740 string and a set of method names to call.
741  
742 Note that any C<undef> items will be mapped to an empty string.
743  
744 Example:
745  
746 $src->sprintf_methods('%d has name %s', qw(id name))
747 ->say
748 ->await;
749  
750 =cut
751  
752 0 ▶sub sprintf_methods {
Subroutine sprintf_methods: not called CC=1 SCAR=6.9
Pod documented
753 0 my ($self, $fmt, @methods) = @_;
754 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
755 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
756 0 my ($item) = @_;
757 0 ▶ $src->emit(sprintf $fmt, map $item->$_ // '', @methods)
Condition0%0/2
$item->$_ // ""
l!l
00
MC/DC0%0/1
$item->$_ // ""
$item->$_
Truth table0%0/2
A
A= $item->$_
Aresult
11
00
758 0 }, $src);
759 }
760  
761 =head2 ignore
762  
763 Receives items, but ignores them entirely.
764  
765 Emits nothing and eventually completes when the upstream L<Ryu::Source> is done.
766  
767 Might be useful for keeping a source alive.
768  
769 =cut
770  
771 0 ▶sub ignore {
Subroutine ignore: not called CC=1 SCAR=6.9
Pod documented
772 0 my ($self) = @_;
773 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
774 0 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
775 0 ▶ shift->on_ready($src->_completed) unless $src->_completed->is_ready
Branch: 1 branch
BranchTrueFalseTotal
unless $src->_completed->is_ready 0 0 0
776 0 });
777 0 return $src;
778 }
779  
780 =head2 buffer
781  
782 Accumulate items while any downstream sources are paused.
783  
784 Takes the following named parameters:
785  
786 =over 4
787  
788 =item * C<high> - once at least this many items are buffered, will L</pause>
789 the upstream L<Ryu::Source>.
790  
791 =item * C<low> - if the buffered count drops to this number, will L</resume>
792 the upstream L<Ryu::Source>.
793  
794 =back
795  
796 =cut
797  
798 30 ▶sub buffer {
Subroutine buffer: called CC=5 SCAR=16.2
Pod documented
799 30 my $self = shift;
800 30 my %args;
801 30 ▶ %args = @_ != 1
Branch: 1 branch
BranchTrueFalseTotal
@_ != 1 ? : 29 1 30
802 ? @_
803 : (
804 low => $_[0],
805 high => $_[0],
806 );
807 30 ▶ $args{low} //= $args{high};
Condition66%2/3
$args{"low"} //= $args{"high"}
l!l&&r!l&&!r
13017
MC/DC50%1/2
$args{"low"} //= $args{"high"}
$args{"low"} $args{"high"}
Truth table66%2/3
A //= B
A= $args{"low"}B= $args{"high"}
ABresult
1X1
011
000
808 30 ▶ $args{low} //= 10;
Condition100%2/2
$args{"low"} //= 10
l!l
1317
MC/DC100%1/1
$args{"low"} //= 10
$args{"low"}
Truth table100%2/2
A
A= $args{"low"}
Aresult
11
00
809 30 ▶ $args{high} //= $args{low};
Condition66%2/3
$args{"high"} //= $args{"low"}
l!l&&r!l&&!r
13170
MC/DC0%0/2
$args{"high"} //= $args{"low"}
$args{"high"} $args{"low"}
Truth table66%2/3
A //= B
A= $args{"high"}B= $args{"low"}
ABresult
1X1
011
000
810  
811 30 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
812 30 $src->{pause_propagation} = 0;
813 30 my @pending;
814 29 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
815 29 ▶ shift->on_ready($src->_completed) unless $src->_completed->is_ready or @pending;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->_completed->is_ready or @pending 27 2 29
Condition66%2/3
$src->_completed->is_ready or @pending
l!l&&r!l&&!r
0227
MC/DC50%1/2
$src->_completed->is_ready or @pending
$src->_completed->is_ready @pending
Truth table66%2/3
A or B
A= $src->_completed->is_readyB= @pending
ABresult
1X1
011
000
816 30 });
817 30 my $fc = $src->flow_control;
818 30 my $item_handler = do {
819 30 Scalar::Util::weaken(my $weak_self = $self);
820 30 Scalar::Util::weaken(my $weak_src = $src);
821 109 ▶ sub {
Subroutine __ANON__: called CC=13 SCAR=25.8
822 109 my $self = $weak_self;
823 109 ▶ my $src = $weak_src or return;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $weak_src or return 109 0 109
824 109 ▶ if(@pending >= $args{high} and $self and not $self->is_paused($src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending >= $args{"high"} and $self and not $self->is_paused($src)) 2 107 109
Condition83%5/6
@pending >= $args{"high"} and $self and not $self->is_paused($src)
!ll&&!rl&&r
10342
@pending >= $args{"high"} and $self
!ll&&!rl&&r
10306
MC/DC66%2/3
@pending >= $args{"high"} and $self and not $self->is_paused($src)
@pending >= $args{"high"} $self not $self->is_paused($src)
Truth table75%3/4
A and B and not(C)
A= @pending >= $args{"high"}B= $selfC= not $self->is_paused($src)
ABCresult
0XX0
10X0
1100
1111
825 2 $self->pause($src);
826 }
827 $src->emit(shift @pending)
828 while @pending
829 and not($src->is_paused)
830 109 ▶ and @{$self->{children}};
Branch: 1 branch
BranchTrueFalseTotal
@pending and not $src->is_paused and @{$self->{"children"};} and do { $src->emit(shift @pending) } 69 109 178
Condition83%5/6
@pending and not $src->is_paused and @{$self->{"children"};}
!ll&&!rl&&r
109069
@pending and not $src->is_paused
!ll&&!rl&&r
971269
MC/DC66%2/3
@pending and not $src->is_paused and @{$self->{"children"};}
@pending not $src->is_paused @{$self->{"children"};}
Truth table75%3/4
A and not(B) and C
A= @pendingB= not $src->is_pausedC= @{$self->{"children"};}
ABCresult
0XX0
10X0
1100
1111
831 109 ▶ $self->resume($src) if @pending <= $args{low} and $self->is_paused($src);
Branch: 1 branch
BranchTrueFalseTotal
if @pending <= $args{"low"} and $self->is_paused($src) 2 107 109
Condition100%3/3
@pending <= $args{"low"} and $self->is_paused($src)
!ll&&!rl&&r
61012
MC/DC100%2/2
@pending <= $args{"low"} and $self->is_paused($src)
@pending <= $args{"low"} $self->is_paused($src)
Truth table100%3/3
A and B
A= @pending <= $args{"low"}B= $self->is_paused($src)
ABresult
0X0
100
111
832  
833 109 ▶ return if @pending;
Branch: 1 branch
BranchTrueFalseTotal
if @pending 12 97 109
834  
835 # It's common to have a situation where the parent chain completes while we're
836 # paused waiting for the queue to drain. In this situation, we want to propagate
837 # completion only once the queue is empty.
838 97 ▶ $self->_completed->on_ready($src->_completed)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_completed->is_ready and not $src->_completed->is_ready 2 95 97
Condition66%2/3
$self->_completed->is_ready and not $src->_completed->is_ready
!ll&&!rl&&r
9502
MC/DC50%1/2
$self->_completed->is_ready and not $src->_completed->is_ready
$self->_completed->is_ready not $src->_completed->is_ready
Truth table66%2/3
A and not(B)
A= $self->_completed->is_readyB= not $src->_completed->is_ready
ABresult
0X0
100
111
839 if $self->_completed->is_ready and not $src->_completed->is_ready;
840 }
841 30 };
842 29 ▶ $src->_completed->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
843 29 ▶ $self->resume($src) if $self and $self->is_paused($src);
Branch: 1 branch
BranchTrueFalseTotal
if $self and $self->is_paused($src) 0 29 29
Condition33%1/3
$self and $self->is_paused($src)
!ll&&!rl&&r
0290
MC/DC0%0/2
$self and $self->is_paused($src)
$self $self->is_paused($src)
Truth table33%1/3
A and B
A= $selfB= $self->is_paused($src)
ABresult
0X0
100
111
844 30 });
845 30 $fc->each($item_handler)->retain;
846 69 ▶ $self->each(my $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
847 69 push @pending, $_;
848 69 $item_handler->()
849 30 });
850 29 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
851 29 my ($f) = @_;
852 29 ▶ return if @pending;
Branch: 1 branch
BranchTrueFalseTotal
if @pending 2 27 29
853 27 my $addr = Scalar::Util::refaddr($code);
854 0 ▶ my $count = List::UtilsBy::extract_by { $addr == Scalar::Util::refaddr($_) } @{$self->{on_item}};
Subroutine __ANON__: not called CC=1 SCAR=0.4
855 27 ▶ $f->on_ready($src->_completed) unless $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 0 27 27
856 27 $log->tracef("->buffer completed on %s for refaddr 0x%x, removed %d on_item handlers", $self->describe, Scalar::Util::refaddr($self), $count);
857 30 });
858 30 $src;
859 }
860  
861 0 ▶sub remove_handler {
Subroutine remove_handler: not called CC=1 SCAR=6.9
Pod undocumented
862 0 my ($self, $code) = @_;
863 0 my $addr = Scalar::Util::refaddr($code);
864 0 ▶ my $count = List::UtilsBy::extract_by {
Subroutine __ANON__: not called CC=1 SCAR=6.9
865 0 $addr == Scalar::Util::refaddr($_)
866 0 } @{$self->{on_item}};
867 0 $log->tracef(
868 "Removing handler on %s with refaddr 0x%x, matched %d total",
869 $self->describe,
870 Scalar::Util::refaddr($self),
871 $count
872 );
873 0 return $self;
874 }
875  
876 30 ▶sub retain {
Subroutine retain: called CC=1 SCAR=0.0
Pod undocumented
877 30 my ($self) = @_;
878 30 $self->{_self} = $self;
879 $self->_completed
880 30 ▶ ->on_ready(sub { delete $self->{_self} });
Subroutine __ANON__: called CC=1 SCAR=0.0
881 30 $self
882 }
883  
884 =head2 as_list
885  
886 Resolves to a list consisting of all items emitted by this source.
887  
888 =cut
889  
890 10 ▶sub as_list {
Subroutine as_list: called CC=1 SCAR=0.0
Pod documented
891 10 my ($self) = @_;
892 10 my @data;
893 52 ▶ $self->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
894 52 push @data, $_
895 10 });
896 10 ▶ $self->_completed->transform(done => sub { @data })
Subroutine __ANON__: called CC=1 SCAR=0.0
897 10 }
898  
899 =head2 as_arrayref
900  
901 Resolves to a single arrayref consisting of all items emitted by this source.
902  
903 =cut
904  
905 2 ▶sub as_arrayref {
Subroutine as_arrayref: called CC=1 SCAR=0.0
Pod documented
906 2 my ($self) = @_;
907 2 my @data;
908 6 ▶ $self->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
909 6 push @data, $_
910 2 });
911 2 ▶ $self->_completed->transform(done => sub { \@data })
Subroutine __ANON__: called CC=1 SCAR=0.0
912 2 }
913  
914 =head2 as_string
915  
916 Concatenates all items into a single string.
917  
918 Returns a L<Future> which will resolve on completion.
919  
920 =cut
921  
922 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
923 0 my ($self) = @_;
924 0 my $data = '';
925 0 ▶ $self->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
926 0 $data .= $_;
927 0 });
928 0 ▶ $self->_completed->transform(done => sub { $data })
Subroutine __ANON__: not called CC=1 SCAR=6.9
929 0 }
930  
931 =head2 as_queue
932  
933 Returns a L<Future::Queue> instance which will
934 L<Future::Queue/push> items whenever the source
935 emits them.
936  
937 The queue will be marked as finished when this source is completed.
938  
939 Parameters passed to this method will be given to the L<Future::Queue>
940 constructor:
941  
942 use Future::AsyncAwait qw(:experimental(suspend));
943 my $queue = $src->as_queue(
944 max_items => 100
945 );
946 SUSPEND { print "Waiting for more items\n" }
947 while(my @batch = await $queue->shift_atmost(10)) {
948 print "Had batch of @{[ 0 + @batch ]} items\n";
949 }
950  
951 =cut
952  
953 0 ▶sub as_queue {
Subroutine as_queue: not called CC=2 SCAR=17.9
Pod documented
954 0 my ($self, %args) = @_;
955 0 my $queue = Future::Queue->new(
956 prototype => $self->curry::weak::new_future,
957 %args
958 );
959  
960 0 ▶ if($args{max_items}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'max_items'}) { } 0 0 0
961 0 my $f;
962 0 ▶ $self->each($self->$curry::weak(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
963 0 my ($self) = @_;
964 0 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $f = $queue->push($_))->is_ready and not $self->is_paused) 0 0 0
Condition0%0/3
(my $f = $queue->push($_))->is_ready and not $self->is_paused
!ll&&!rl&&r
000
MC/DC0%0/2
(my $f = $queue->push($_))->is_ready and not $self->is_paused
(my $f = $queue->push($_))->is_ready not $self->is_paused
Truth table0%0/3
A and not(B)
A= (my $f = $queue->push($_))->is_readyB= not $self->is_paused
ABresult
0X0
100
111
965 (my $f = $queue->push($_))->is_ready
966 and not $self->is_paused
967 ) {
968 0 ▶ $f->on_ready(sub { $self->resume });
Subroutine __ANON__: not called CC=1 SCAR=6.9
969 0 $self->pause;
970 }
971 0 return;
972 0 }));
973 } else {
974 # Avoid the extra overhead when we know there isn't going to be any
975 # upper limit on accepted items.
976 0 ▶ $self->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
977 0 $queue->push($_);
978 0 return;
979 0 });
980 }
981 0 ▶ $self->completed->on_ready(sub { $queue->finish });
Subroutine __ANON__: not called CC=1 SCAR=6.9
982 0 return $queue;
983 }
984  
985 =head2 as_buffer
986  
987 Returns a L<Ryu::Buffer> instance, which will
988 L<Ryu::Buffer/write> any emitted items from this
989 source to the buffer as they arrive.
990  
991 Intended for stream protocol handling - individual
992 sized packets are perhaps better suited to the
993 L<Ryu::Source> per-item behaviour.
994  
995 Supports the following named parameters:
996  
997 =over 4
998  
999 =item * C<low> - low waterlevel for buffer, start accepting more bytes
1000 once the L<Ryu::Buffer> has less content than this
1001  
1002 =item * C<high> - high waterlevel for buffer, will pause the parent stream
1003 if this is reached
1004  
1005 =back
1006  
1007 The backpressure (low/high) values default to undefined, meaning
1008 no backpressure is applied: the buffer will continue to fill
1009 indefinitely.
1010  
1011 =cut
1012  
1013 1 ▶sub as_buffer {
Subroutine as_buffer: called CC=1 SCAR=0.0
Pod documented
1014 1 my ($self, %args) = @_;
1015 1 my $low = delete $args{low};
1016 1 my $high = delete $args{high};
1017 # We're creating a source but keeping it to ourselves here
1018 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1019  
1020 my $buffer = Ryu::Buffer->new(
1021 new_future => $self->{new_future},
1022 %args,
1023 2 ▶ on_change => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
1024 2 my ($self) = @_;
1025 2 ▶ $src->resume if $low and $self->size <= $low;
Branch: 1 branch
BranchTrueFalseTotal
if $low and $self->size <= $low 1 1 2
Condition66%2/3
$low and $self->size <= $low
!ll&&!rl&&r
011
MC/DC50%1/2
$low and $self->size <= $low
$low $self->size <= $low
Truth table66%2/3
A and B
A= $lowB= $self->size <= $low
ABresult
0X0
100
111
1026 }
1027 1 );
1028  
1029 1 Scalar::Util::weaken(my $weak_sauce = $src);
1030 1 Scalar::Util::weaken(my $weak_buffer = $buffer);
1031 6 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
1032 6 ▶ my $src = $weak_sauce or return;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $weak_sauce or return 6 0 6
1033 6 ▶ my $buf = $weak_buffer or do {
Branch: 1 branch
BranchTrueFalseTotal
my $buf = $weak_buffer or do { $src->finish; return } 5 1 6
1034 1 $src->finish;
1035 1 return;
1036 };
1037 5 $buf->write($_);
1038 5 ▶ $src->pause if $high and $buf->size >= $high;
Branch: 1 branch
BranchTrueFalseTotal
if $high and $buf->size >= $high 1 4 5
Condition66%2/3
$high and $buf->size >= $high
!ll&&!rl&&r
041
MC/DC50%1/2
$high and $buf->size >= $high
$high $buf->size >= $high
Truth table66%2/3
A and B
A= $highB= $buf->size >= $high
ABresult
0X0
100
111
1039 5 ▶ $src->resume if $low and $buf->size <= $low;
Branch: 1 branch
BranchTrueFalseTotal
if $low and $buf->size <= $low 2 3 5
Condition66%2/3
$low and $buf->size <= $low
!ll&&!rl&&r
032
MC/DC50%1/2
$low and $buf->size <= $low
$low $buf->size <= $low
Truth table66%2/3
A and B
A= $lowB= $buf->size <= $low
ABresult
0X0
100
111
1040 1 }, $src);
1041 1 return $buffer;
1042 }
1043  
1044 =head2 as_last
1045  
1046 Returns a L<Future> which resolves to the last value received.
1047  
1048 =cut
1049  
1050 10 ▶sub as_last {
Subroutine as_last: called CC=1 SCAR=0.0
Pod documented
1051 10 my ($self) = @_;
1052 10 my $v;
1053 10 ▶ $self->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1054 10 $v = $_;
1055 10 });
1056 10 ▶ $self->_completed->transform(done => sub { $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
1057 10 }
1058  
1059 =head2 as_void
1060  
1061 Returns a L<Future> which resolves to an empty list.
1062  
1063 =cut
1064  
1065 0 ▶sub as_void {
Subroutine as_void: not called CC=1 SCAR=6.9
Pod documented
1066 0 my ($self) = @_;
1067 0 ▶ $self->_completed->transform(done => sub { () })
Subroutine __ANON__: not called CC=1 SCAR=6.9
1068 0 }
1069  
1070 =head2 combine_latest
1071  
1072 Takes the most recent item from one or more L<Ryu::Source>s, and emits
1073 an arrayref containing the values in order.
1074  
1075 An item is emitted for each update as soon as all sources have provided
1076 at least one value. For example, given 2 sources, if the first emits C<1>
1077 then C<2>, then the second emits C<a>, this would emit a single C<< [2, 'a'] >>
1078 item.
1079  
1080 =cut
1081  
1082 1 ▶sub combine_latest : method {
Subroutine combine_latest: called CC=4 SCAR=14.8
Pod documented
1083 1 my ($self, @sources) = @_;
1084 0 ▶ push @sources, sub { @_ } if Scalar::Util::blessed $sources[-1];
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($sources[-1]) 0 1 1
1085 1 my $code = pop @sources;
1086  
1087 1 my $combined = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1088 1 ▶ unshift @sources, $self if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 1 0 1
1089 1 my @value;
1090 my %seen;
1091 1 for my $idx (0..$#sources) {
1092 2 my $src = $sources[$idx];
1093 5 ▶ $src->each_while_source(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1094 5 $value[$idx] = $_;
1095 5 ▶ $seen{$idx} ||= 1;
Condition100%2/2
$seen{$idx} ||= 1
l!l
32
MC/DC100%1/1
$seen{$idx} ||= 1
$seen{$idx}
Truth table100%2/2
A
A= $seen{$idx}
Aresult
11
00
1096 5 ▶ $combined->emit([ $code->(@value) ]) if @sources == keys %seen;
Branch: 1 branch
BranchTrueFalseTotal
if @sources == keys %seen 4 1 5
1097 2 }, $combined);
1098 }
1099 Future->needs_any(
1100 map $_->completed, @sources
1101 0 ▶ )->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1102 0 @value = ();
1103 0 ▶ return if $combined->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $combined->_completed->is_ready 0 0 0
1104 0 shift->on_ready($combined->_completed)
1105 1 })->retain;
1106 1 $combined
1107 }
1108  
1109 =head2 with_index
1110  
1111 Emits arrayrefs consisting of C<< [ $item, $idx ] >>.
1112  
1113 =cut
1114  
1115 1 ▶sub with_index {
Subroutine with_index: called CC=1 SCAR=0.0
Pod documented
1116 1 my ($self) = @_;
1117 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1118 1 my $idx = 0;
1119 3 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1120 3 $src->emit([ $_, $idx++ ])
1121 1 }, $src);
1122 }
1123  
1124 =head2 with_latest_from
1125  
1126 Similar to L</combine_latest>, but will start emitting as soon as
1127 we have any values. The arrayref will contain C<< undef >> for any
1128 sources which have not yet emitted any items.
1129  
1130 =cut
1131  
1132 0 ▶sub with_latest_from : method {
Subroutine with_latest_from: not called CC=3 SCAR=24.8
Pod documented
1133 0 my ($self, @sources) = @_;
1134 0 ▶ push @sources, sub { @_ } if Scalar::Util::blessed $sources[-1];
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($sources[-1]) 0 0 0
1135 0 my $code = pop @sources;
1136  
1137 0 my $combined = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1138 0 my @value;
1139 my %seen;
1140 0 for my $idx (0..$#sources) {
1141 0 my $src = $sources[$idx];
1142 0 ▶ $src->each(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1143 0 ▶ return if $combined->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $combined->_completed->is_ready 0 0 0
1144 0 $value[$idx] = $_;
1145 0 ▶ $seen{$idx} ||= 1;
Condition0%0/2
$seen{$idx} ||= 1
l!l
00
MC/DC0%0/1
$seen{$idx} ||= 1
$seen{$idx}
Truth table0%0/2
A
A= $seen{$idx}
Aresult
11
00
1146 0 });
1147 }
1148 0 ▶ $self->each(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1149 0 ▶ $combined->emit([ $code->(@value) ]) if keys %seen;
Branch: 1 branch
BranchTrueFalseTotal
if keys %seen 0 0 0
1150 0 });
1151 0 $self->_completed->on_ready($combined->_completed);
1152 0 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1153 0 @value = ();
1154 0 ▶ return if $combined->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $combined->is_ready 0 0 0
1155 0 shift->on_ready($combined->_completed);
1156 0 });
1157 0 $combined
1158 }
1159  
1160 =head2 merge
1161  
1162 Emits items as they are generated by the given sources.
1163  
1164 Example:
1165  
1166 $numbers->merge($letters)->say # 1, 'a', 2, 'b', 3, 'c'...
1167  
1168 =cut
1169  
1170 4 ▶sub merge : method {
Subroutine merge: called CC=3 SCAR=11.1
Pod documented
1171 4 my ($self, @sources) = @_;
1172  
1173 4 my $combined = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1174 4 ▶ unshift @sources, $self if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 4 0 4
1175 4 for my $src (@sources) {
1176 16 ▶ $src->each(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1177 16 ▶ return if $combined->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $combined->_completed->is_ready 0 16 16
1178 16 $combined->emit($_)
1179 5 });
1180 }
1181 Future->needs_all(
1182 map $_->completed, @sources
1183 )->on_ready($combined->_completed)
1184 3 ▶ ->on_ready(sub { @sources = () })
Subroutine __ANON__: called CC=1 SCAR=0.0
1185 4 ->retain;
1186 4 $combined
1187 }
1188  
1189 =head2 emit_from
1190  
1191 Emits items as they are generated by the given sources.
1192  
1193 Example:
1194  
1195 my $src = Ryu::Source->new;
1196 $src->say;
1197 $src->emit_from(
1198 $numbers,
1199 $letters
1200 );
1201  
1202 =cut
1203  
1204 1 ▶sub emit_from : method {
Subroutine emit_from: called CC=2 SCAR=7.0
Pod documented
1205 1 my ($self, @sources) = @_;
1206  
1207 1 for my $src (@sources) {
1208 5 ▶ $src->each_while_source(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1209 5 ▶ return if $self->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_completed->is_ready 0 5 5
1210 5 $self->emit($_)
1211 2 }, $self);
1212 }
1213 $self
1214 1 }
1215  
1216 =head2 apply
1217  
1218 Used for setting up multiple streams.
1219  
1220 Accepts a variable number of coderefs, will call each one and gather L<Ryu::Source>
1221 results.
1222  
1223 =cut
1224  
1225 0 ▶sub apply : method {
Subroutine apply: not called CC=2 SCAR=17.9
Pod documented
1226 0 my ($self, @code) = @_;
1227  
1228 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1229 0 my @pending;
1230 0 for my $code (@code) {
1231 0 push @pending, map $code->($_), $self;
1232 }
1233 Future->needs_all(
1234 0 map $_->completed, @pending
1235 )->on_ready($src->_completed)
1236 ->retain;
1237 # Pass through the original events
1238 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1239 0 $src->emit($_)
1240 0 }, $src)
1241 }
1242  
1243 =head2 switch_str
1244  
1245 Given a condition, will select one of the alternatives based on stringified result.
1246  
1247 Example:
1248  
1249 $src->switch_str(
1250 sub { $_->name }, # our condition
1251 smith => sub { $_->id }, # if this matches the condition, the code will be called with $_ set to the current item
1252 jones => sub { $_->parent->id },
1253 sub { undef } # and this is our default case
1254 );
1255  
1256 =cut
1257  
1258 1 ▶sub switch_str {
Subroutine switch_str: called CC=1 SCAR=0.1
Pod documented
1259 1 my ($self, $condition, @args) = @_;
1260  
1261 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1262 1 my @active;
1263 0 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1264 Future->needs_all(
1265 grep $_, @active
1266 0 ▶ )->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1267 0 $src->finish
1268 0 })->retain
1269 1 });
1270  
1271 3 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
1272 3 my ($item) = $_;
1273 3 my $rslt = $condition->($item);
1274 3 ▶ (Scalar::Util::blessed($rslt) && $rslt->isa('Future') ? $rslt : Future->done($rslt))->on_done(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1275 3 my ($data) = @_;
1276 3 my @copy = @args;
1277 3 ▶ while(my ($k, $v) = splice @copy, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@copy, 0, 2)) 6 0 6
1278 6 ▶ if(!defined $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $v) { } 1 5 6
elsif ($k eq $data) { } 2 3 5
1279 # Only a single value (or undef)? That's our default, just use it as-is
1280 1 return $src->emit(map $k->($_), $item)
1281 } elsif($k eq $data) {
1282 # Key matches our result? Call code with the original item
1283 2 return $src->emit(map $v->($_), $item)
1284 }
1285 }
1286 3 ▶ })->retain
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($rslt) && $rslt->isa('Future') ? : 0 3 3
Condition33%1/3
builtin::blessed($rslt) and $rslt->isa("Future")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($rslt) and $rslt->isa("Future")
builtin::blessed($rslt) $rslt->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($rslt)B= $rslt->isa("Future")
ABresult
0X0
100
111
1287 1 }, $src)
1288 }
1289  
1290 =head2 ordered_futures
1291  
1292 Given a stream of L<Future>s, will emit the results as each L<Future>
1293 is marked ready.
1294  
1295 If any L<Future> in the stream fails, that will mark this source as failed,
1296 and all remaining L<Future> instances will be cancelled. To avoid this behaviour
1297 and leave the L<Future> instances active, use:
1298  
1299 $src->map('without_cancel')
1300 ->ordered_futures
1301  
1302 See L<Future/without_cancel> for more details.
1303  
1304 Takes the following named parameters:
1305  
1306 =over 4
1307  
1308 =item * C<high> - once at least this many unresolved L<Future> instances are pending,
1309 will L</pause> the upstream L<Ryu::Source>.
1310  
1311 =item * C<low> - if the pending count drops to this number, will L</resume>
1312 the upstream L<Ryu::Source>.
1313  
1314 =back
1315  
1316 This method is also available as L</resolve>.
1317  
1318 =cut
1319  
1320 5 ▶sub ordered_futures {
Subroutine ordered_futures: called CC=3 SCAR=11.0
Pod documented
1321 5 my ($self, %args) = @_;
1322 5 my $high = delete $args{high};
1323 5 ▶ my $low = (delete $args{low}) // $high // 0;
Condition80%4/5
(delete $args{"low"} // $high) // 0
l!l
14
delete $args{"low"} // $high
l!l&&r!l&&!r
104
MC/DC50%1/2
(delete $args{"low"} // $high) // 0
delete $args{"low"} $high
Truth table66%2/3
A // B
A= delete $args{"low"}B= $high
ABresult
1X1
011
000
1324 5 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1325 5 my %pending;
1326 5 my $src_completed = $src->_completed;
1327  
1328 5 my $all_finished;
1329 5 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
1330 5 $all_finished = shift;
1331 5 ▶ $all_finished->on_ready($src_completed) unless %pending or $src_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless %pending or $src_completed->is_ready 1 4 5
Condition66%2/3
%pending or $src_completed->is_ready
l!l&&r!l&&!r
401
MC/DC50%1/2
%pending or $src_completed->is_ready
%pending $src_completed->is_ready
Truth table66%2/3
A or B
A= %pendingB= $src_completed->is_ready
ABresult
1X1
011
000
1332 5 });
1333  
1334 4 ▶ $src_completed->on_ready(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1335 4 my @pending = values %pending;
1336 4 %pending = ();
1337 4 for(@pending) {
1338 3 ▶ $_->cancel if $_ and not $_->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $_ and not $_->is_ready 2 1 3
Condition66%2/3
$_ and not $_->is_ready
!ll&&!rl&&r
012
MC/DC50%1/2
$_ and not $_->is_ready
$_ not $_->is_ready
Truth table66%2/3
A and not(B)
A= $_B= not $_->is_ready
ABresult
0X0
100
111
1339 }
1340 5 });
1341 5 my $paused = 0;
1342 13 ▶ $self->each(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1343 13 my $f = $_;
1344 13 my $k = Scalar::Util::refaddr $f;
1345 # This will keep a copy of the Future around until the
1346 # ->is_ready callback removes it
1347 13 $pending{$k} = $f;
1348 13 $log->tracef('Ordered futures has %d pending', 0 + keys %pending);
1349 13 ▶ if(!$paused and $high and keys(%pending) >= $high) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $paused and $high and keys %pending >= $high) 1 12 13
Condition83%5/6
not $paused and $high and keys %pending >= $high
!ll&&!rl&&r
841
not $paused and $high
!ll&&!rl&&r
085
MC/DC66%2/3
not $paused and $high and keys %pending >= $high
not $paused $high keys %pending >= $high
Truth table75%3/4
not(A) and B and C
A= not $pausedB= $highC= keys %pending >= $high
ABCresult
0XX0
10X0
1100
1111
1350 1 $src->pause;
1351 1 ++$paused;
1352 }
1353 9 ▶ $f->on_done(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1354 9 my @pending = @_;
1355 9 ▶ while(@pending and not $src_completed->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending and not $src_completed->is_ready) 4 9 13
Condition66%2/3
@pending and not $src_completed->is_ready
!ll&&!rl&&r
904
MC/DC50%1/2
@pending and not $src_completed->is_ready
@pending not $src_completed->is_ready
Truth table66%2/3
A and not(B)
A= @pendingB= not $src_completed->is_ready
ABresult
0X0
100
111
1356 4 $src->emit(shift @pending);
1357 }
1358 })
1359 1 ▶ ->on_fail(sub { $src->fail(@_) unless $src_completed->is_ready; })
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $src_completed->is_ready 1 0 1
1360 12 ▶ ->on_ready(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
1361 12 delete $pending{$k};
1362 12 ▶ if($paused and keys(%pending) <= $low) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paused and keys %pending <= $low) 1 11 12
Condition100%3/3
$paused and keys %pending <= $low
!ll&&!rl&&r
921
MC/DC100%2/2
$paused and keys %pending <= $low
$paused keys %pending <= $low
Truth table100%3/3
A and B
A= $pausedB= keys %pending <= $low
ABresult
0X0
100
111
1363 1 $src->resume;
1364 1 --$paused;
1365 }
1366 12 $log->tracef('Ordered futures now has %d pending after completion, upstream finish status is %d', 0 + keys(%pending), $all_finished);
1367 12 ▶ return if %pending;
Branch: 1 branch
BranchTrueFalseTotal
if %pending 6 6 12
1368 6 ▶ $all_finished->on_ready($src_completed) if $all_finished and not $src_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $all_finished and not $src_completed->is_ready 2 4 6
Condition100%3/3
$all_finished and not $src_completed->is_ready
!ll&&!rl&&r
132
MC/DC100%2/2
$all_finished and not $src_completed->is_ready
$all_finished not $src_completed->is_ready
Truth table100%3/3
A and not(B)
A= $all_finishedB= not $src_completed->is_ready
ABresult
0X0
100
111
1369 })
1370 5 });
1371 5 return $src;
1372 }
1373  
1374 =head2 resolve
1375  
1376 A synonym for L</ordered_futures>.
1377  
1378 =cut
1379  
1380 43 *resolve = *ordered_futures;
1381  
1382 =head2 concurrent
1383  
1384 =cut
1385  
1386 0 ▶sub concurrent {
Subroutine concurrent: not called CC=1 SCAR=6.9
Pod documented
1387 0 my ($self) = @_;
1388 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1389 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1390 0 $_->on_done($src->curry::weak::emit)
1391 ->on_fail($src->curry::weak::fail)
1392 ->retain
1393 0 }, $src);
1394 }
1395  
1396 =head2 distinct
1397  
1398 Emits new distinct items, using string equality with an exception for
1399 C<undef> (i.e. C<undef> is treated differently from empty string or 0).
1400  
1401 Given 1,2,3,undef,2,3,undef,'2',2,4,1,5, you'd expect to get the sequence 1,2,3,undef,4,5.
1402  
1403 =cut
1404  
1405 1 ▶sub distinct {
Subroutine distinct: called CC=1 SCAR=0.0
Pod documented
1406 1 my $self = shift;
1407  
1408 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1409 1 my %seen;
1410 my $undef;
1411 22 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1412 22 ▶ if(defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 17 5 22
1413 17 ▶ $src->emit($_) unless $seen{$_}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$_}++ 7 10 17
1414 } else {
1415 5 ▶ $src->emit($_) unless $undef++;
Branch: 1 branch
BranchTrueFalseTotal
unless $undef++ 1 4 5
1416 }
1417 1 }, $src);
1418 }
1419  
1420 =head2 distinct_until_changed
1421  
1422 Removes contiguous duplicates, defined by string equality.
1423  
1424 =cut
1425  
1426 1 ▶sub distinct_until_changed {
Subroutine distinct_until_changed: called CC=1 SCAR=0.0
Pod documented
1427 1 my $self = shift;
1428  
1429 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1430 1 my $active;
1431 my $prev;
1432 18 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1433 18 ▶ if($active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active) { } 17 1 18
1434 17 ▶ if(defined($prev) ^ defined($_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $prev ^ defined $_) { } 10 7 17
elsif (defined $_) { } 5 2 7
1435 10 $src->emit($_)
1436 } elsif(defined($_)) {
1437 5 ▶ $src->emit($_) if $prev ne $_;
Branch: 1 branch
BranchTrueFalseTotal
if $prev ne $_ 3 2 5
1438 }
1439 } else {
1440 1 $active = 1;
1441 1 $src->emit($_);
1442 }
1443 18 $prev = $_;
1444 1 }, $src);
1445 1 $src
1446 }
1447  
1448 =head2 sort_by
1449  
1450 Emits items sorted by the given key. This is a stable sort function.
1451  
1452 The algorithm is taken from L<List::UtilsBy>.
1453  
1454 =cut
1455  
1456 0 ▶sub sort_by {
Subroutine sort_by: not called CC=1 SCAR=4.8
Pod documented
1457 43 ▶ use sort qw(stable);
Subroutine BEGIN: called CC=1 SCAR=0.0
1458 0 my ($self, $code) = @_;
1459 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1460 0 my @items;
1461 my @keys;
1462 0 ▶ $self->_completed->on_done(sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
1463 0 ▶ })->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1464 0 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 0 0
1465 0 shift->on_ready($src->_completed);
1466 0 });
1467 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1468 0 push @items, $_;
1469 0 push @keys, $_->$code;
1470 0 ▶ }, $src, cleanup => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1471 0 my ($f) = @_;
1472 0 ▶ return unless $f->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_done 0 0 0
1473 0 $src->emit($_) for @items[sort { $keys[$a] cmp $keys[$b] } 0 .. $#items];
1474 0 });
1475 }
1476  
1477 =head2 nsort_by
1478  
1479 Emits items numerically sorted by the given key. This is a stable sort function.
1480  
1481 See L</sort_by>.
1482  
1483 =cut
1484  
1485 0 ▶sub nsort_by {
Subroutine nsort_by: not called CC=1 SCAR=4.0
Pod documented
1486 43 ▶ use sort qw(stable);
Subroutine BEGIN: called CC=1 SCAR=0.0
1487 0 my ($self, $code) = @_;
1488 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1489 0 my @items;
1490 my @keys;
1491 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1492 0 push @items, $_;
1493 0 push @keys, $_->$code;
1494 0 ▶ }, $src, cleanup => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1495 0 ▶ return unless shift->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless (shift())->is_done 0 0 0
1496 0 $src->emit($_) for @items[sort { $keys[$a] <=> $keys[$b] } 0 .. $#items];
1497 0 });
1498 }
1499  
1500 =head2 rev_sort_by
1501  
1502 Emits items sorted by the given key. This is a stable sort function.
1503  
1504 The algorithm is taken from L<List::UtilsBy>.
1505  
1506 =cut
1507  
1508 0 ▶sub rev_sort_by {
Subroutine rev_sort_by: not called CC=1 SCAR=4.0
Pod documented
1509 43 ▶ use sort qw(stable);
Subroutine BEGIN: called CC=1 SCAR=0.0
1510 0 my ($self, $code) = @_;
1511 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1512 0 my @items;
1513 my @keys;
1514 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1515 0 push @items, $_;
1516 0 push @keys, $_->$code;
1517 0 ▶ }, $src, cleanup => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1518 0 ▶ return unless shift->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless (shift())->is_done 0 0 0
1519 0 $src->emit($_) for @items[sort { $keys[$b] cmp $keys[$a] } 0 .. $#items];
1520 0 });
1521 }
1522  
1523 =head2 rev_nsort_by
1524  
1525 Emits items numerically sorted by the given key. This is a stable sort function.
1526  
1527 See L</sort_by>.
1528  
1529 =cut
1530  
1531 0 ▶sub rev_nsort_by {
Subroutine rev_nsort_by: not called CC=1 SCAR=6.9
Pod documented
1532 0 my ($self, $code) = @_;
1533 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1534 0 my @items;
1535 my @keys;
1536 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1537 0 push @items, $_;
1538 0 push @keys, $_->$code;
1539 0 ▶ }, $src, cleanup => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1540 0 ▶ return unless shift->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless (shift())->is_done 0 0 0
1541 0 $src->emit($_) for @items[sort { $keys[$b] <=> $keys[$a] } 0 .. $#items];
1542 0 });
1543 }
1544  
1545 =head2 extract_all
1546  
1547 Expects a regular expression and emits hashrefs containing
1548 the named capture buffers.
1549  
1550 The regular expression will be applied using the m//gc operator.
1551  
1552 Example:
1553  
1554 $src->extract_all(qr{/(?<component>[^/]+)})
1555 # emits { component => '...' }, { component => '...' }
1556  
1557 =cut
1558  
1559 1 ▶sub extract_all {
Subroutine extract_all: called CC=1 SCAR=0.0
Pod documented
1560 1 my ($self, $pattern) = @_;
1561 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1562 3 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1563 3 ▶ $src->emit(+{ %+ }) while m/$pattern/gc;
Branch: 1 branch
BranchTrueFalseTotal
/$pattern/cg and do { $src->emit({%+}) } 9 3 12
1564 1 }, $src);
1565 }
1566  
1567 =head2 skip
1568  
1569 Skips the first N items.
1570  
1571 =cut
1572  
1573 1 ▶sub skip {
Subroutine skip: called CC=2 SCAR=7.0
Pod documented
1574 1 my ($self, $count) = @_;
1575 1 ▶ $count //= 0;
Condition50%1/2
$count //= 0
l!l
10
MC/DC0%0/1
$count //= 0
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
1576  
1577 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1578 1 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1579 1 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 1 1
1580 1 shift->on_ready($src->_completed);
1581 1 });
1582 5 ▶ $self->each(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1583 5 ▶ $src->emit($_) unless $count-- > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $count-- > 0 3 2 5
1584 1 });
1585 1 $src
1586 }
1587  
1588 =head2 skip_last
1589  
1590 Skips the last N items.
1591  
1592 =cut
1593  
1594 1 ▶sub skip_last {
Subroutine skip_last: called CC=2 SCAR=7.0
Pod documented
1595 1 my ($self, $count) = @_;
1596 1 ▶ $count //= 0;
Condition50%1/2
$count //= 0
l!l
10
MC/DC0%0/1
$count //= 0
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
1597  
1598 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1599 1 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1600 1 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 1 1
1601 1 shift->on_ready($src->_completed);
1602 1 });
1603 1 my @pending;
1604 5 ▶ $self->each(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1605 5 push @pending, $_;
1606 5 ▶ $src->emit(shift @pending) if @pending > $count;
Branch: 1 branch
BranchTrueFalseTotal
if @pending > $count 3 2 5
1607 1 });
1608 1 $src
1609 }
1610  
1611 =head2 skip_until
1612  
1613 Skips the items that arrive before a given condition is reached.
1614  
1615 =over 4
1616  
1617 =item * Either a L<Future> instance (we skip all items until it's marked as `done`), or a coderef,
1618 which we call for each item until it first returns true
1619  
1620 =back
1621  
1622 =cut
1623  
1624 2 ▶sub skip_until {
Subroutine skip_until: called CC=4 SCAR=14.2
Pod documented
1625 2 my ($self, $condition) = @_;
1626  
1627 2 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1628 2 $self->each_while_source(do {
1629 2 ▶ if(ref($condition) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $condition eq 'CODE') { } 1 1 2
elsif (builtin::blessed($condition) and $condition->isa('Future')) { } 1 0 1
1630 1 my $reached = 0;
1631 5 ▶ sub { return $src->emit($_) if $reached ||= $condition->($_); }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if $reached ||= &$condition($_) 3 2 5
Condition100%3/3
$reached ||= &$condition($_)
l!l&&r!l&&!r
212
MC/DC100%2/2
$reached ||= &$condition($_)
$reached &$condition($_)
Truth table100%3/3
A ||= B
A= $reachedB= &$condition($_)
ABresult
1X1
011
000
1632 1 ▶ } elsif(Scalar::Util::blessed($condition) && $condition->isa('Future')) {
Condition33%1/3
builtin::blessed($condition) and $condition->isa("Future")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($condition) and $condition->isa("Future")
builtin::blessed($condition) $condition->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($condition)B= $condition->isa("Future")
ABresult
0X0
100
111
1633 1 ▶ $condition->on_ready($src->$curry::weak(sub {
Subroutine __ANON__: called CC=4 SCAR=14.9
1634 1 my ($src, $cond) = @_;
1635 1 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 1 1
1636 1 ▶ $src->fail($cond->failure) if $cond->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $cond->is_failed 0 1 1
1637 1 ▶ $src->cancel if $cond->is_cancelled
Branch: 1 branch
BranchTrueFalseTotal
if $cond->is_cancelled 0 1 1
1638 1 }));
1639 4 ▶ sub { $src->emit($_) if $condition->is_done; }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $condition->is_done 2 2 4
1640 1 } else {
1641 0 die 'unknown type for condition: ' . $condition;
1642 }
1643 }, $src);
1644 }
1645  
1646 =head2 take_until
1647  
1648 Passes through items that arrive until a given condition is reached.
1649  
1650 Expects a single parameter, which can be one of the following:
1651  
1652 =over 4
1653  
1654 =item * a L<Future> instance - we will skip all items until it's marked as C<done>
1655  
1656 =item * a coderef, which we call for each item until it first returns true
1657  
1658 =item * or a L<Ryu::Source>, in which case we stop when that first emits a value
1659  
1660 =back
1661  
1662 =cut
1663  
1664 0 ▶sub take_until {
Subroutine take_until: not called CC=6 SCAR=37.4
Pod documented
1665 0 my ($self, $condition) = @_;
1666  
1667 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1668 0 ▶ if(Scalar::Util::blessed($condition) && $condition->isa('Ryu::Source')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($condition) and $condition->isa('Ryu::Source')) { } 0 0 0
Condition0%0/3
builtin::blessed($condition) and $condition->isa("Ryu::Source")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($condition) and $condition->isa("Ryu::Source")
builtin::blessed($condition) $condition->isa("Ryu::Source")
Truth table0%0/3
A and B
A= builtin::blessed($condition)B= $condition->isa("Ryu::Source")
ABresult
0X0
100
111
1669 0 ▶ $condition->_completed->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1670 0 $log->warnf('Condition completed: %s and %s', $condition->describe, $src->describe);
1671 0 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 0 0
1672 0 $log->warnf('Mark as ready');
1673 0 shift->on_ready($src->_completed);
1674 0 });
1675 0 ▶ $condition->first->each(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1676 0 ▶ $src->finish unless $src->is_ready
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 0 0 0
1677 0 });
1678 0 return $self->each_while_source($src->curry::emit, $src);
1679 } else {
1680 0 return $self->each_while_source(do {
1681 0 ▶ if(ref($condition) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $condition eq 'CODE') { } 0 0 0
elsif (builtin::blessed($condition) and $condition->isa('Future')) { } 0 0 0
1682 0 my $reached = 0;
1683 0 ▶ sub { return $src->emit($_) unless $reached ||= $condition->($_); }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless $reached ||= &$condition($_) 0 0 0
Condition0%0/3
$reached ||= &$condition($_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$reached ||= &$condition($_)
$reached &$condition($_)
Truth table0%0/3
A ||= B
A= $reachedB= &$condition($_)
ABresult
1X1
011
000
1684 0 ▶ } elsif(Scalar::Util::blessed($condition) && $condition->isa('Future')) {
Condition0%0/3
builtin::blessed($condition) and $condition->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($condition) and $condition->isa("Future")
builtin::blessed($condition) $condition->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($condition)B= $condition->isa("Future")
ABresult
0X0
100
111
1685 0 ▶ $condition->on_ready($src->$curry::weak(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1686 0 my ($src, $cond) = @_;
1687 0 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 0 0
1688 0 ▶ $src->fail($cond->failure) if $cond->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $cond->is_failed 0 0 0
1689 0 ▶ $src->cancel if $cond->is_cancelled
Branch: 1 branch
BranchTrueFalseTotal
if $cond->is_cancelled 0 0 0
1690 0 }));
1691 0 ▶ sub { $src->emit($_) unless $condition->is_done; }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $condition->is_done 0 0 0
1692 0 } else {
1693 0 die 'unknown type for condition: ' . $condition;
1694 }
1695 }, $src);
1696 }
1697 }
1698  
1699 =head2 take
1700  
1701 Takes a limited number of items.
1702  
1703 Given a sequence of C< 1,2,3,4,5 > and C<< ->take(3) >>, you'd get 1,2,3 and then the stream
1704 would finish.
1705  
1706 =cut
1707  
1708 2 ▶sub take {
Subroutine take: called CC=3 SCAR=11.0
Pod documented
1709 2 my ($self, $count) = @_;
1710 2 ▶ $count //= 0;
Condition50%1/2
$count //= 0
l!l
20
MC/DC0%0/1
$count //= 0
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
1711 2 ▶ return $self->empty unless $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $count > 0 0 2 2
1712  
1713 2 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1714 5 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1715 5 $log->tracef("Still alive with %d remaining", $count);
1716 5 $src->emit($_);
1717 5 ▶ return if --$count;
Branch: 1 branch
BranchTrueFalseTotal
if --$count 3 2 5
1718 2 $log->tracef("Count is zero, finishing");
1719 2 $src->finish
1720 2 }, $src);
1721 }
1722  
1723 =head2 first
1724  
1725 Returns a source which provides the first item from the stream.
1726  
1727 =cut
1728  
1729 0 ▶sub first {
Subroutine first: not called CC=1 SCAR=6.9
Pod documented
1730 0 my ($self) = @_;
1731  
1732 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1733 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1734 0 $src->emit($_);
1735 0 $src->finish
1736 0 }, $src);
1737 }
1738  
1739 =head2 some
1740  
1741 Applies the given code to each item, and emits a single item:
1742  
1743 =over 4
1744  
1745 =item * 0 if the code never returned true or no items were received
1746  
1747 =item * 1 if the code ever returned a true value
1748  
1749 =back
1750  
1751 =cut
1752  
1753 1 ▶sub some {
Subroutine some: called CC=1 SCAR=0.4
Pod documented
1754 1 my ($self, $code) = @_;
1755  
1756 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1757 1 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=18.1
1758 1 my $sf = $src->_completed;
1759 1 ▶ return if $sf->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $sf->is_ready 1 0 1
1760 0 my $f = shift;
1761 0 ▶ return $f->on_ready($sf) unless $f->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_done 0 0 0
1762 0 $src->emit(0);
1763 0 $sf->done;
1764 1 });
1765 4 ▶ $self->each(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1766 4 ▶ return if $src->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->_completed->is_ready 0 4 4
1767 4 ▶ return unless $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
unless &$code($_) 3 1 4
1768 1 $src->emit(1);
1769 1 $src->_completed->done
1770 1 });
1771 1 $src
1772 }
1773  
1774 =head2 every
1775  
1776 Similar to L</some>, except this requires the coderef to return true for
1777 all values in order to emit a C<1> value.
1778  
1779 =cut
1780  
1781 1 ▶sub every {
Subroutine every: called CC=1 SCAR=0.2
Pod documented
1782 1 my ($self, $code) = @_;
1783  
1784 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1785 1 ▶ $self->_completed->on_done(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
1786 1 ▶ return if $src->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->_completed->is_ready 0 1 1
1787 1 $src->emit(1);
1788 1 $src->_completed->done
1789 1 });
1790 5 ▶ $self->each(sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
1791 5 ▶ return if $src->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->_completed->is_ready 0 5 5
1792 5 ▶ return if $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($_) 5 0 5
1793 0 $src->emit(0);
1794 0 $src->_completed->done
1795 1 });
1796 1 $src
1797 }
1798  
1799 =head2 count
1800  
1801 Emits the count of items seen once the parent source completes.
1802  
1803 =cut
1804  
1805 12 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
1806 12 my ($self) = @_;
1807  
1808 12 my $count = 0;
1809  
1810 12 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1811 27 ▶ $self->each_while_source(sub { ++$count }, $src, cleanup => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.1
1812 12 ▶ return unless shift->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless (shift())->is_done 0 12 12
1813 12 $src->emit($count)
1814 12 });
1815 }
1816  
1817 =head2 sum
1818  
1819 Emits the numeric sum of items seen once the parent completes.
1820  
1821 =cut
1822  
1823 1 ▶sub sum {
Subroutine sum: called CC=1 SCAR=0.0
Pod documented
1824 1 my ($self) = @_;
1825  
1826 1 my $sum = 0;
1827  
1828 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1829 5 ▶ $self->each_while_source(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1830 5 $sum += $_
1831 1 ▶ }, $src, cleanup => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1832 1 ▶ return unless shift->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless (shift())->is_done 0 1 1
1833 1 $src->emit($sum)
1834 1 });
1835 }
1836  
1837 =head2 mean
1838  
1839 Emits the mean (average) numerical value of all seen items.
1840  
1841 =cut
1842  
1843 1 ▶sub mean {
Subroutine mean: called CC=1 SCAR=0.0
Pod documented
1844 1 my ($self) = @_;
1845  
1846 1 my $sum = 0;
1847 1 my $count = 0;
1848  
1849 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1850 1 ▶ $self->each(sub { ++$count; $sum += $_ });
Subroutine __ANON__: called CC=1 SCAR=0.0
1851 1 ▶ $self->_completed->on_done(sub { $src->emit($sum / ($count || 1)) })
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$count || 1
l!l
10
MC/DC0%0/1
$count || 1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
1852 1 ->on_ready($src->_completed);
1853 1 $src
1854 }
1855  
1856 =head2 max
1857  
1858 Emits the maximum numerical value of all seen items.
1859  
1860 =cut
1861  
1862 1 ▶sub max {
Subroutine max: called CC=1 SCAR=0.0
Pod documented
1863 1 my ($self) = @_;
1864  
1865 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1866 1 my $max;
1867 8 ▶ $self->each(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1868 8 ▶ return if defined $max and $max > $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $max > $_ 6 2 8
Condition100%3/3
defined $max and $max > $_
!ll&&!rl&&r
116
MC/DC100%2/2
defined $max and $max > $_
defined $max $max > $_
Truth table100%3/3
A and B
A= defined $maxB= $max > $_
ABresult
0X0
100
111
1869 2 $max = $_;
1870 1 });
1871 1 ▶ $self->_completed->on_done(sub { $src->emit($max) })
Subroutine __ANON__: called CC=1 SCAR=0.0
1872 1 ->on_ready($src->_completed);
1873 1 $src
1874 }
1875  
1876 =head2 min
1877  
1878 Emits the minimum numerical value of all seen items.
1879  
1880 =cut
1881  
1882 1 ▶sub min {
Subroutine min: called CC=1 SCAR=0.0
Pod documented
1883 1 my ($self) = @_;
1884  
1885 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1886 1 my $min;
1887 8 ▶ $self->each(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1888 8 ▶ return if defined $min and $min < $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $min < $_ 4 4 8
Condition100%3/3
defined $min and $min < $_
!ll&&!rl&&r
134
MC/DC100%2/2
defined $min and $min < $_
defined $min $min < $_
Truth table100%3/3
A and B
A= defined $minB= $min < $_
ABresult
0X0
100
111
1889 4 $min = $_;
1890 1 });
1891 1 ▶ $self->_completed->on_done(sub { $src->emit($min) })
Subroutine __ANON__: called CC=1 SCAR=0.0
1892 1 ->on_ready($src->_completed);
1893 1 $src
1894 }
1895  
1896 =head2 statistics
1897  
1898 Emits a single hashref of statistics once the source completes.
1899  
1900 This will contain the following keys:
1901  
1902 =over 4
1903  
1904 =item * count
1905  
1906 =item * sum
1907  
1908 =item * min
1909  
1910 =item * max
1911  
1912 =item * mean
1913  
1914 =back
1915  
1916 =cut
1917  
1918 1 ▶sub statistics {
Subroutine statistics: called CC=1 SCAR=0.0
Pod documented
1919 1 my ($self) = @_;
1920  
1921 1 my $sum = 0;
1922 1 my $count = 0;
1923 1 my $min;
1924 my $max;
1925  
1926 1 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1927 9 ▶ $self->each(sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
1928 9 ▶ $min //= $_;
Condition66%2/3
$min //= $_
l!l&&r!l&&!r
810
MC/DC0%0/2
$min //= $_
$min $_
Truth table66%2/3
A //= B
A= $minB= $_
ABresult
1X1
011
000
1929 9 ▶ $max //= $_;
Condition66%2/3
$max //= $_
l!l&&r!l&&!r
810
MC/DC0%0/2
$max //= $_
$max $_
Truth table66%2/3
A //= B
A= $maxB= $_
ABresult
1X1
011
000
1930 9 ▶ $min = $_ if $_ < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $min 0 9 9
1931 9 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 8 1 9
1932 9 ++$count;
1933 9 $sum += $_
1934 1 });
1935 1 ▶ $self->_completed->on_done(sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
1936 1 ▶ $src->emit({
Condition50%1/2
$count || 1
l!l
10
MC/DC0%0/1
$count || 1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
1937 count => $count,
1938 sum => $sum,
1939 min => $min,
1940 max => $max,
1941 mean => ($sum / ($count || 1))
1942 })
1943 })
1944 1 ->on_ready($src->_completed);
1945 1 $src
1946 }
1947  
1948 =head2 filter
1949  
1950 Applies the given parameter to filter values.
1951  
1952 The parameter can be a regex or coderef. You can also
1953 pass (key, value) pairs to filter hashrefs or objects
1954 based on regex or coderef values.
1955  
1956 Examples:
1957  
1958 $src->filter(name => qr/^[A-Z]/, id => sub { $_ % 2 })
1959  
1960 =cut
1961  
1962 12 ▶sub filter {
Subroutine filter: called CC=5 SCAR=16.3
Pod documented
1963 12 my $self = shift;
1964  
1965 12 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
1966 $self->each_while_source((@_ > 1) ? do {
1967 10 my %args = @_;
1968 51 ▶ my $check = sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
1969 51 my ($k, $v) = @_;
1970 51 ▶ if(my $ref = ref $args{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $args{$k}) { } 37 14 51
1971 37 ▶ if($ref eq 'Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 15 22 37
elsif ($ref eq 'ARRAY') { } 12 10 22
elsif ($ref eq 'CODE') { } 10 0 10
1972 15 ▶ return 0 unless defined($v) && $v =~ $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v =~ /$args{$k}/ 8 7 15
Condition100%3/3
defined $v and $v =~ /$args{$k}/
!ll&&!rl&&r
177
MC/DC100%2/2
defined $v and $v =~ /$args{$k}/
defined $v $v =~ /$args{$k}/
Truth table100%3/3
A and B
A= defined $vB= $v =~ /$args{$k}/
ABresult
0X0
100
111
1973 } elsif($ref eq 'ARRAY') {
1974 12 ▶ return 0 unless defined($v) && List::Util::any { $v eq $_ } @{$args{$k}};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and &List::Util::any(sub { $v eq $_; } , @{$args{$k};}) 7 5 12
Condition100%3/3
defined $v and &List::Util::any(sub { $v eq $_; } , @{$args{$k};})
!ll&&!rl&&r
435
MC/DC100%2/2
defined $v and &List::Util::any(sub { $v eq $_; } , @{$args{$k};})
defined $v &List::Util::any(sub { $v eq $_; } , @{$args{$k};})
Truth table100%3/3
A and B
A= defined $vB= &List::Util::any(sub { $v eq $_; } , @{$args{$k};})
ABresult
0X0
100
111
1975 } elsif($ref eq 'CODE') {
1976 10 return 0 for grep !$args{$k}->($_), $v;
1977 } else {
1978 0 die "Unsure what to do with $args{$k} which seems to be a $ref";
1979 }
1980 } else {
1981 14 ▶ return !defined($args{$k}) if !defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 13 14
1982 13 ▶ return defined($args{$k}) && $v eq $args{$k};
Condition66%2/3
defined $args{$k} && $v eq $args{$k}
!ll&&!rl&&r
0103
MC/DC50%1/2
defined $args{$k} && $v eq $args{$k}
defined $args{$k} $v eq $args{$k}
Truth table66%2/3
A && B
A= defined $args{$k}B= $v eq $args{$k}
ABresult
0X0
100
111
1983 }
1984 18 return 1;
1985 10 };
1986 51 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=21.3
1987 51 my $item = shift;
1988 51 ▶ if(Scalar::Util::blessed $item) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($item)) { } 15 36 51
elsif (my $ref = ref $item) { } 36 0 36
1989 15 for my $k (keys %args) {
1990 15 my $v = $item->$k;
1991 15 ▶ return unless $check->($k, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless &$check($k, $v) 9 6 15
1992 }
1993 } elsif(my $ref = ref $item) {
1994 36 ▶ if($ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 36 0 36
1995 36 for my $k (keys %args) {
1996 36 my $v = $item->{$k};
1997 36 ▶ return unless $check->($k, $v);
Branch: 1 branch
BranchTrueFalseTotal
unless &$check($k, $v) 21 15 36
1998 }
1999 } else {
2000 0 die 'not a ref we know how to handle: ' . $ref;
2001 }
2002 } else {
2003 0 die 'not a ref, not sure what to do now';
2004 }
2005 21 $src->emit($item);
2006 }
2007 12 ▶ } : do {
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 10 2 12
2008 2 my $code = shift;
2009 2 ▶ if(my $ref = ref($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $code) 2 0 2
2010 2 ▶ if($ref eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 0 2 2
elsif ($ref eq 'CODE') { } 2 0 2
2011 0 my $re = $code;
2012 0 ▶ $code = sub { /$re/ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
2013 } elsif($ref eq 'CODE') {
2014 # use as-is
2015 } else {
2016 0 die "not sure how to handle $ref";
2017 }
2018 }
2019 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2020 4 my $item = shift;
2021 4 ▶ $src->emit($item) if $code->($item);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($item) 3 1 4
2022 }
2023 2 }, $src);
2024 }
2025  
2026 =head2 filter_isa
2027  
2028 Emits only the items which C<< ->isa >> one of the given parameters.
2029 Will skip non-blessed items.
2030  
2031 =cut
2032  
2033 0 ▶sub filter_isa {
Subroutine filter_isa: not called CC=1 SCAR=6.9
Pod documented
2034 0 my ($self, @isa) = @_;
2035  
2036 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
2037 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2038 0 my ($item) = @_;
2039 0 ▶ return unless Scalar::Util::blessed $item;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($item) 0 0 0
2040 0 ▶ $src->emit($_) if grep $item->isa($_), @isa;
Branch: 1 branch
BranchTrueFalseTotal
if grep $item->isa($_), @isa 0 0 0
2041 0 }, $src);
2042 }
2043  
2044 =head2 emit
2045  
2046 Emits the given item.
2047  
2048 =cut
2049  
2050 597 ▶sub emit {
Subroutine emit: called CC=6 SCAR=21.2
Pod documented
2051 597 my $self = shift;
2052 597 my $completion = $self->_completed;
2053 my @handlers = $self->{on_item}->@*
2054 597 ▶ or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my(@handlers) = @{$self->{"on_item"}} or return $self 566 31 597
2055 566 for (@_) {
2056 598 ▶ die 'already completed' if $completion->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $completion->is_ready 0 598 598
2057 598 for my $code (@handlers) {
2058 try {
2059 632 $code->($_);
2060 632 } catch {
2061 0 my $ex = $@;
2062 0 ▶ $log->warnf("Exception raised in %s - %s", (eval { $self->describe } // "<failed>"), "$ex");
Condition0%0/2
eval { do { $self->describe } } // "<failed>"
l!l
00
MC/DC0%0/1
eval { do { $self->describe } } // "<failed>"
eval { do { $self->describe } }
Truth table0%0/2
A
A= eval { do { $self->describe } }
Aresult
11
00
2063 0 $completion->fail($ex, source => 'exception in on_item callback');
2064 0 die $ex;
2065 }
2066 }
2067 }
2068 $self
2069 566 }
2070  
2071 =head2 emit_batch
2072  
2073 =cut
2074  
2075 0 ▶sub emit_batch {
Subroutine emit_batch: not called CC=9 SCAR=45.0
Pod documented
2076 0 my $self = shift;
2077 0 my $completion = $self->_completed;
2078 0 ▶ if(my @handlers = $self->{on_batch}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@handlers) = @{$self->{"on_batch"}}) 0 0 0
2079 0 for (@_) {
2080 0 ▶ die 'already completed' if $completion->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $completion->is_ready 0 0 0
2081 0 for my $code (@handlers) {
2082 try {
2083 0 $code->($_);
2084 0 } catch {
2085 0 my $ex = $@;
2086 0 ▶ $log->warnf("Exception raised in %s - %s", (eval { $self->describe } // "<failed>"), "$ex");
Condition0%0/2
eval { do { $self->describe } } // "<failed>"
l!l
00
MC/DC0%0/1
eval { do { $self->describe } } // "<failed>"
eval { do { $self->describe } }
Truth table0%0/2
A
A= eval { do { $self->describe } }
Aresult
11
00
2087 0 $completion->fail($ex, source => 'exception in on_batch callback');
2088 0 die $ex;
2089 }
2090 }
2091 }
2092 }
2093  
2094 # Support item-at-a-time callbacks if we have any
2095 0 ▶ return $self unless $self->{on_item}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"on_item"}} 0 0 0
2096 0 for my $batch (@_) {
2097 0 $self->emit($_) for $batch->@*;
2098 }
2099 0 return $self;
2100 }
2101  
2102 =head2 each
2103  
2104 =cut
2105  
2106 219 ▶sub each : method {
Subroutine each: called CC=1 SCAR=0.0
Pod documented
2107 219 my ($self, $code, %args) = @_;
2108 219 push @{$self->{on_item}}, $code;
2109 219 $self;
2110 }
2111  
2112 =head2 each_batch
2113  
2114 =cut
2115  
2116 0 ▶sub each_batch : method {
Subroutine each_batch: not called CC=1 SCAR=6.9
Pod documented
2117 0 my ($self, $code, %args) = @_;
2118 0 push @{$self->{on_batch}}, $code;
2119 0 $self;
2120 }
2121  
2122 =head2 each_as_source
2123  
2124 =cut
2125  
2126 0 ▶sub each_as_source : method {
Subroutine each_as_source: not called CC=1 SCAR=6.9
Pod documented
2127 0 my ($self, @code) = @_;
2128  
2129 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
2130 0 my @active;
2131 0 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2132 Future->needs_all(
2133 grep $_, @active
2134 0 ▶ )->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2135 0 $src->finish
2136 0 })->retain
2137 0 });
2138  
2139 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2140 0 my @pending;
2141 0 for my $code (@code) {
2142 0 push @pending, $code->($_);
2143 }
2144 0 push @active, map $_->completed, @pending;
2145 0 $src->emit($_);
2146 0 }, $src)
2147 }
2148  
2149 184 ▶sub cleanup {
Subroutine cleanup: called CC=4 SCAR=13.9
Pod undocumented
2150 184 my ($self) = @_;
2151 184 $log->tracef("Cleanup for %s (f = %s)", $self->describe, 0 + $self->_completed);
2152 184 ▶ $_->cancel for values %{$self->{cancel_on_ready} || {}};
Condition100%2/2
$self->{'cancel_on_ready'} || {}
l!l
3181
MC/DC100%1/1
$self->{'cancel_on_ready'} || {}
$self->{'cancel_on_ready'}
Truth table100%2/2
A
A= $self->{'cancel_on_ready'}
Aresult
11
00
2153 184 ▶ $self->parent->notify_child_completion($self) if $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent 83 101 184
2154 184 splice $self->{on_item}->@*;
2155 184 delete @{$self->{cancel_on_ready}}{keys %{$self->{cancel_on_ready}}};
2156 184 $log->tracef("Finished cleanup for %s", $self->describe);
2157 }
2158  
2159 78 ▶sub notify_child_completion {
Subroutine notify_child_completion: called CC=5 SCAR=16.9
Pod undocumented
2160 78 my ($self, $child) = @_;
2161 78 my $addr = Scalar::Util::refaddr($child);
2162 78 ▶ if(List::UtilsBy::extract_by { $addr == Scalar::Util::refaddr($_) } @{$self->{children}}) {
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if (&List::UtilsBy::extract_by(sub { $addr == builtin::refaddr($_); } , \@{$$self{"children"};})) 78 0 78
2163 $log->tracef(
2164 "Removed completed child %s, have %d left",
2165 $child->describe,
2166 78 0 + @{$self->{children}}
2167 );
2168 78 ▶ return $self if $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready 71 7 78
2169 7 ▶ return $self if @{$self->{children}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"children"};} 0 7 7
2170  
2171 7 $log->tracef(
2172 "This was the last child, cancelling %s",
2173 $self->describe
2174 );
2175 7 $self->cancel;
2176 7 return $self;
2177 }
2178  
2179 0 $log->warnf("Child %s (addr 0x%x) not found in list for %s", $child->describe, $addr, $self->describe);
2180 0 $log->tracef("* %s (addr 0x%x)", $_->describe, Scalar::Util::refaddr($_)) for @{$self->{children}};
2181 0 $self
2182 }
2183  
2184 =head2 await
2185  
2186 Block until this source finishes.
2187  
2188 =cut
2189  
2190 10 ▶sub await {
Subroutine await: called CC=2 SCAR=7.0
Pod documented
2191 10 my ($self) = @_;
2192 10 $self->prepare_await;
2193 10 my $f = $self->_completed;
2194 10 ▶ $f->await until $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
$f->is_ready or do { $f->await } 10 0 10
2195 10 $self
2196 }
2197  
2198 =head2 next
2199  
2200 Returns a L<Future> which will resolve to the next item emitted by this source.
2201  
2202 If the source completes before an item is emitted, the L<Future> will be cancelled.
2203  
2204 Note that these are independent - they don't stack, so if you call C<< ->next >>
2205 multiple times before an item is emitted, each of those would return the same value.
2206  
2207 See L<Ryu::Buffer> if you're dealing with protocols and want to extract sequences of
2208 bytes or characters.
2209  
2210 To access the sequence as a discrete stream of L<Future> instances, try L</as_queue>
2211 which will provide a L<Future::Queue>.
2212  
2213 =cut
2214  
2215 3 ▶sub next : method {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
2216 3 my ($self) = @_;
2217 my $f = $self->new_future(
2218 'next'
2219 3 ▶ )->on_ready($self->$curry::weak(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
2220 3 my ($self, $f) = @_;
2221 3 my $addr = Scalar::Util::refaddr($f);
2222 3 ▶ List::UtilsBy::extract_by { Scalar::Util::refaddr($_) == $addr } @{$self->{on_item} || []};
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$self->{'on_item'} || []
l!l
30
MC/DC0%0/1
$self->{'on_item'} || []
$self->{'on_item'}
Truth table50%1/2
A
A= $self->{'on_item'}
Aresult
11
00
2223 3 delete $self->{cancel_on_ready}{$f};
2224 3 }));
2225 3 $self->{cancel_on_ready}{$f} = $f;
2226 3 ▶ push @{$self->{on_item}}, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2227 3 ▶ $f->done(shift) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 2 3
2228 3 };
2229 3 return $f;
2230 }
2231  
2232 =head2 finish
2233  
2234 Mark this source as completed.
2235  
2236 =cut
2237  
2238 92 ▶sub finish { $_[0]->_completed->done unless $_[0]->_completed->is_ready; $_[0] }
Subroutine finish: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_completed->is_ready 84 8 92
2239  
2240 0 ▶sub refresh { }
Subroutine refresh: not called CC=1 SCAR=0.0
Pod undocumented
2241  
2242 =head1 METHODS - Proxied
2243  
2244 The following methods are proxied to our completion L<Future>:
2245  
2246 =over 4
2247  
2248 =item * then
2249  
2250 =item * is_ready
2251  
2252 =item * is_done
2253  
2254 =item * failure
2255  
2256 =item * is_cancelled
2257  
2258 =item * else
2259  
2260 =back
2261  
2262 =cut
2263  
2264 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod undocumented
2265 0 my ($self) = @_;
2266 0 my $f = $self->_completed;
2267 0 my @rslt;
2268 0 ▶ $self->each(sub { push @rslt, $_ }) if defined wantarray;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
2269 0 ▶ if(my $parent = $self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) 0 0 0
2270 0 $parent->await
2271 }
2272 0 ▶ $f->transform(done => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2273 @rslt
2274 0 })->get
2275 }
2276  
2277 43 for my $k (qw(then fail on_ready transform is_ready is_done is_failed failure else)) {
2278 43 ▶ do { no strict 'refs'; *$k = $_ } for sub { shift->_completed->$k(@_) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2279 }
2280 # Cancel operations are only available through the internal state, since we don't want anything
2281 # accidentally cancelling due to Future->wait_any(timeout, $src->_completed) or similar constructs
2282 43 for my $k (qw(cancel is_cancelled)) {
2283 43 ▶ do { no strict 'refs'; *$k = $_ } for sub { shift->{completed}->$k(@_) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2284 }
2285  
2286 =head1 METHODS - Internal
2287  
2288 =head2 prepare_await
2289  
2290 Run any pre-completion callbacks (recursively) before
2291 we go into an await cycle.
2292  
2293 Used for compatibility with sync bridges when there's
2294 no real async event loop available.
2295  
2296 =cut
2297  
2298 54 ▶sub prepare_await {
Subroutine prepare_await: called CC=4 SCAR=13.9
Pod documented
2299 54 my ($self) = @_;
2300 54 ▶ (delete $self->{on_get})->() if $self->{on_get};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_get"} 1 53 54
2301 54 ▶ return unless my $parent = $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless my $parent = $self->parent 27 27 54
2302 27 ▶ my $code = $parent->can('prepare_await') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $parent->can("prepare_await") or return 27 0 27
2303 27 local @_ = ($parent);
2304 27 goto &$code;
2305 }
2306  
2307 =head2 chained
2308  
2309 Returns a new L<Ryu::Source> chained from this one.
2310  
2311 =cut
2312  
2313 97 ▶sub chained {
Subroutine chained: called CC=2 SCAR=6.9
Pod documented
2314 97 my ($self) = shift;
2315 97 ▶ if(my $class = ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = ref $self) { } 96 1 97
2316 my $src = $class->new(
2317 new_future => $self->{new_future},
2318 96 parent => $self,
2319 @_
2320 );
2321 96 Scalar::Util::weaken($src->{parent});
2322 96 push @{$self->{children}}, $src;
2323 96 $log->tracef("Constructing chained source for %s from %s (%s)", $src->label, $self->label, $self->_completed->state);
2324 96 return $src;
2325 } else {
2326 1 my $src = $self->new(@_);
2327 1 $log->tracef("Constructing chained source for %s with no parent", $src->label);
2328 1 return $src;
2329 }
2330 }
2331  
2332 =head2 each_while_source
2333  
2334 Like L</each>, but removes the source from the callback list once the
2335 parent completes.
2336  
2337 =cut
2338  
2339 73 ▶sub each_while_source {
Subroutine each_while_source: called CC=1 SCAR=0.0
Pod documented
2340 73 my ($self, $code, $src, %args) = @_;
2341 73 $self->each($code);
2342 55 ▶ $src->_completed->on_ready(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2343 55 my $addr = Scalar::Util::refaddr($code);
2344 55 ▶ my $count = List::UtilsBy::extract_by { $addr == Scalar::Util::refaddr($_) } @{$self->{on_item}};
Subroutine __ANON__: called CC=1 SCAR=0.0
2345 55 $log->tracef("->each_while_source completed on %s for refaddr 0x%x, removed %d on_item handlers", $self->describe, Scalar::Util::refaddr($self), $count);
2346 73 });
2347 55 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
2348 55 my ($f) = @_;
2349 55 ▶ $args{cleanup}->($f, $src) if exists $args{cleanup};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"cleanup"} 14 41 55
2350 55 ▶ $f->on_ready($src->_completed) unless $src->is_ready or !($args{finish_source} // 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready or not $args{"finish_source"} // 1 29 26 55
Condition100%5/5
$src->is_ready or not $args{"finish_source"} // 1
l!l&&r!l&&!r
91729
$args{"finish_source"} // 1
l!l
1729
MC/DC100%2/2
$src->is_ready or not $args{"finish_source"} // 1
$src->is_ready $args{"finish_source"}
Truth table100%3/3
A or not(B)
A= $src->is_readyB= $args{"finish_source"}
ABresult
1X1
001
010
2351 73 });
2352 73 $src
2353 }
2354  
2355 =head2 map_source
2356  
2357 Provides a L</chained> source which has more control over what it
2358 emits than a standard L</map> or L</filter> implementation.
2359  
2360 $original->map_source(sub {
2361 my ($item, $src) = @_;
2362 $src->emit('' . reverse $item);
2363 });
2364  
2365 =cut
2366  
2367 0 ▶sub map_source {
Subroutine map_source: not called CC=1 SCAR=6.9
Pod documented
2368 0 my ($self, $code) = @_;
2369  
2370 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
2371 0 ▶ $self->_completed->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2372 0 ▶ return if $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_ready 0 0 0
2373 0 shift->on_ready($src->_completed);
2374 0 });
2375 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2376 0 $code->($_, $src) for $_;
2377 0 }, $src);
2378 }
2379  
2380 184 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
2381 184 my ($self) = @_;
2382 184 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 184 184
2383 184 $log->tracef("Destruction for %s", $self->describe);
2384 184 ▶ $self->_completed->cancel unless $self->_completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_completed->is_ready 10 174 184
2385 }
2386  
2387 0 ▶sub catch {
Subroutine catch: not called CC=1 SCAR=6.9
Pod undocumented
2388 0 my ($self, $code) = @_;
2389 0 my $src = $self->chained(label => (caller 0)[3] =~ /::([^:]+)$/);
2390 0 ▶ $self->_completed->on_fail(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2391 0 my @failure = @_;
2392 0 my $sub = $code->(@failure);
2393 0 ▶ if(Scalar::Util::blessed $sub && $sub->isa('Ryu::Source')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($sub) and $sub->isa('Ryu::Source')) { } 0 0 0
Condition0%0/3
builtin::blessed($sub) and $sub->isa("Ryu::Source")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($sub) and $sub->isa("Ryu::Source")
builtin::blessed($sub) $sub->isa("Ryu::Source")
Truth table0%0/3
A and B
A= builtin::blessed($sub)B= $sub->isa("Ryu::Source")
ABresult
0X0
100
111
2394 0 ▶ $sub->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2395 0 $src->emit($_)
2396 0 }, $src);
2397 } else {
2398 0 $sub->fail(@failure);
2399 }
2400 0 });
2401 0 ▶ $self->each_while_source(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2402 0 $src->emit($_)
2403 0 }, $src);
2404 }
2405  
2406 43 1;
2407  
2408 __END__
« Ryu/Node.pm ↑ Summary Ryu/Buffer.pm »