File Coverage

blib/lib/Event/Distributor/Query.pm
Criterion Covered Total %
statement 32 32 100.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 8 8 100.0
pod 1 1 100.0
total 50 52 96.1


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-2017 -- leonerd@leonerd.org.uk
5              
6             package Event::Distributor::Query;
7              
8 3     3   58130 use strict;
  3         10  
  3         61  
9 3     3   10 use warnings;
  3         4  
  3         61  
10 3     3   10 use base qw( Event::Distributor::_Event );
  3         5  
  3         487  
11              
12             our $VERSION = '0.05';
13              
14 3     3   14 use Future;
  3         3  
  3         606  
15              
16             =head1 NAME
17              
18             C - an event that collects a result
19              
20             =head1 DESCRIPTION
21              
22             This subclass of L invokes each of its subscribers
23             in turn, yielding either the (first) successful and non-empty result, or a
24             failure if they all fail. Yields a (successful) empty result if there are no
25             subscribers.
26              
27             =cut
28              
29             sub fire
30             {
31 6     6 1 16 my $self = shift;
32 6         11 my ( $dist, @args ) = @_;
33              
34 6         9 my $await = $self->{await};
35 6         7 my @f;
36              
37 6         15 foreach my $sub ( $self->subscribers ) {
38             my $f = $sub->( $dist, @args )->then_with_f( sub {
39 6     6   384 my $f = shift;
40 6 100       16 return $f if @_;
41 2         6 die "No result\n";
42 7         51 });
43              
44 7         148 push @f, $f;
45              
46 7 100 66     15 last if $f->is_ready and !$f->failure;
47             }
48              
49 6 100       55 return Future->done if !@f;
50              
51             return Future->needs_any( @f )->then( sub {
52 4     4   476 my @results = @_;
53             # TODO: conversions?
54 4         10 Future->done( @results );
55             })->else_with_f( sub {
56 1     1   140 my $f = shift;
57 1         3 my @other_fails = grep { $_->failure ne "No result\n" } $f->failed_futures;
  1         11  
58              
59 1 50       8 return $other_fails[0] if @other_fails;
60 1         2 Future->done();
61 5         15 });
62             }
63              
64             =head1 AUTHOR
65              
66             Paul Evans
67              
68             =cut
69              
70             0x55AA