blib/lib/Event/Distributor/Query.pm

filter: Statementstmt 100% 34 / 34 Branchbran 87.5% 7 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 94.6% 53 / 56 CC 7 SCAR 10.0
CC
7
Cov
96%
fire
5.0
__ANON__
2.0
__ANON__
2.0
CRAP 2.7
« Build ↑ Summary blib/lib/Event/Distributor.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use base qw( Event::Distributor::_Event );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.05';
13  
14 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 C<Event::Distributor::Query> - an event that collects a result
19  
20 =head1 DESCRIPTION
21  
22 This subclass of L<Event::Distributor::_Event> 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 ▶ my $self = shift;
Subroutine fire: called CC=5 SCAR=16.1
Pod documented
32 6 my ( $dist, @args ) = @_;
33  
34 6 my $await = $self->{await};
35 6 my @f;
36  
37 6 foreach my $sub ( $self->subscribers ) {
38 my $f = $sub->( $dist, @args )->then_with_f( sub {
39 6 ▶ my $f = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
40 6 ▶ return $f if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 2 6
41 2 die "No result\n";
42 7 });
43  
44 7 push @f, $f;
45  
46 7 ▶ last if $f->is_ready and !$f->failure;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready and not $f->failure 4 3 7
Condition66%2/3
$f->is_ready and not $f->failure
!ll&&!rl&&r
034
MC/DC50%1/2
$f->is_ready and not $f->failure
$f->is_ready not $f->failure
Truth table66%2/3
A and not(B)
A= $f->is_readyB= not $f->failure
ABresult
0X0
100
111
47 }
48  
49 6 ▶ return Future->done if !@f;
Branch: 1 branch
BranchTrueFalseTotal
unless @f 1 5 6
50  
51 return Future->needs_any( @f )->then( sub {
52 4 ▶ my @results = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
53 # TODO: conversions?
54 4 Future->done( @results );
55 })->else_with_f( sub {
56 1 ▶ my $f = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
57 1 my @other_fails = grep { $_->failure ne "No result\n" } $f->failed_futures;
58  
59 1 ▶ return $other_fails[0] if @other_fails;
Branch: 1 branch
BranchTrueFalseTotal
if @other_fails 0 1 1
60 1 Future->done();
61 5 });
62 }
63  
64 =head1 AUTHOR
65  
66 Paul Evans <leonerd@leonerd.org.uk>
67  
68 =cut
69  
70 3 0x55AA
« Build ↑ Summary blib/lib/Event/Distributor.pm »