File Coverage

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