File Coverage

blib/lib/Event/Distributor/Query.pm
Criterion Covered Total %
statement 31 31 100.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 8 8 100.0
pod 1 1 100.0
total 49 51 96.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2014-2021 -- leonerd@leonerd.org.uk
5              
6             package Event::Distributor::Query 0.06;
7              
8 3     3   92832 use v5.14;
  3         22  
9 3     3   17 use warnings;
  3         15  
  3         126  
10 3     3   16 use base qw( Event::Distributor::_Event );
  3         6  
  3         728  
11              
12 3     3   23 use Future;
  3         7  
  3         907  
13              
14             =head1 NAME
15              
16             C - an event that collects a result
17              
18             =head1 DESCRIPTION
19              
20             This subclass of L invokes each of its subscribers
21             in turn, yielding either the (first) successful and non-empty result, or a
22             failure if they all fail. Yields a (successful) empty result if there are no
23             subscribers.
24              
25             =cut
26              
27             sub fire
28             {
29 6     6 1 27 my $self = shift;
30 6         17 my ( $dist, @args ) = @_;
31              
32 6         15 my $await = $self->{await};
33 6         11 my @f;
34              
35 6         21 foreach my $sub ( $self->subscribers ) {
36             my $f = $sub->( $dist, @args )->then_with_f( sub {
37 6     6   517 my $f = shift;
38 6 100       26 return $f if @_;
39 2         13 die "No result\n";
40 7         79 });
41              
42 7         199 push @f, $f;
43              
44 7 100 66     23 last if $f->is_ready and !$f->failure;
45             }
46              
47 6 100       95 return Future->done if !@f;
48              
49             return Future->needs_any( @f )->then( sub {
50 4     4   634 my @results = @_;
51             # TODO: conversions?
52 4         28 Future->done( @results );
53             })->else_with_f( sub {
54 1     1   172 my $f = shift;
55 1         5 my @other_fails = grep { $_->failure ne "No result\n" } $f->failed_futures;
  1         14  
56              
57 1 50       13 return $other_fails[0] if @other_fails;
58 1         4 Future->done();
59 5         27 });
60             }
61              
62             =head1 AUTHOR
63              
64             Paul Evans
65              
66             =cut
67              
68             0x55AA