| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$f->is_ready and not $f->failure
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
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 6 | ▶ | return Future->done if !@f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||