File Coverage

blib/lib/Event/Distributor/Signal.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 34 34 100.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::Signal 0.06;
7              
8 3     3   88186 use v5.14;
  3         22  
9 3     3   18 use warnings;
  3         5  
  3         88  
10 3     3   15 use base qw( Event::Distributor::_Event );
  3         5  
  3         1395  
11              
12 3     3   21 use Future;
  3         6  
  3         574  
13              
14             =head1 NAME
15              
16             C - an event that returns no result
17              
18             =head1 DESCRIPTION
19              
20             This subclass of L invokes each of its subscribers
21             in turn, ensuring each has a chance to be invoked regardless of the result of
22             the others.
23              
24             Its L succeeds (with no value) if no subscriber failed. If one
25             subscriber failed, the C fails in the same way. If two or more
26             subscribers fail, the resulting C fails with a message composed by
27             combining all the individual messages, in the C category, whose
28             failure details are a list of the failed component futures.
29              
30             =cut
31              
32             sub fire
33             {
34 10     10 1 212 my $self = shift;
35 10         23 my ( $dist, @args ) = @_;
36              
37             return Future->wait_all(
38             map {
39 13         133 my $sub = $_; local $_; # protect against corruption of $_
  13         18  
40 13         58 Future->call( $sub, $dist, @args );
41             } $self->subscribers
42             )->then( sub {
43 10     10   6276 my @failed = grep { $_->failure } @_;
  13         58  
44              
45 10 100       100 return Future->done() if !@failed;
46 2 100       7 return $failed[0] if @failed == 1;
47             return Future->fail( "Multiple subscribers failed:\n" .
48 1         4 join( "", map { " | " . $_->failure } @failed ),
  2         13  
49             distributor => @failed,
50             );
51 10         33 });
52             }
53              
54             =head1 AUTHOR
55              
56             Paul Evans
57              
58             =cut
59              
60             0x55AA