| 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 | ▶ | 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.04'; | |||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | use Future; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | C<Event::Distributor::Signal> - an event that returns no result | ||||||||||
| 19 | |||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||
| 21 | |||||||||||
| 22 | This subclass of L<Event::Distributor::_Event> 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<Future> succeeds (with no value) if no subscriber failed. If one | ||||||||||
| 27 | subscriber failed, the C<Future> fails in the same way. If two or more | ||||||||||
| 28 | subscribers fail, the resulting C<Future> fails with a message composed by | ||||||||||
| 29 | combining all the individual messages, in the C<distributor> category, whose | ||||||||||
| 30 | failure details are a list of the failed component futures. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | sub fire | ||||||||||
| 35 | { | ||||||||||
| 36 | 10 | ▶ | my $self = shift; | ||||||||
|
Subroutine
fire: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 10 | my ( $dist, @args ) = @_; | |||||||||
| 38 | |||||||||||
| 39 | return Future->wait_all( | ||||||||||
| 40 | map { | ||||||||||
| 41 | 13 | my $sub = $_; local $_; # protect against corruption of $_ | |||||||||
| 42 | 13 | Future->call( $sub, $dist, @args ); | |||||||||
| 43 | } $self->subscribers | ||||||||||
| 44 | )->then( sub { | ||||||||||
| 45 | 10 | ▶ | my @failed = grep { $_->failure } @_; | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 46 | |||||||||||
| 47 | 10 | ▶ | return Future->done() if !@failed; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 2 | ▶ | return $failed[0] if @failed == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | return Future->fail( "Multiple subscribers failed:\n" . | ||||||||||
| 50 | 1 | join( "", map { " | " . $_->failure } @failed ), | |||||||||
| 51 | distributor => @failed, | ||||||||||
| 52 | ); | ||||||||||
| 53 | 10 | }); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | =head1 AUTHOR | ||||||||||
| 57 | |||||||||||
| 58 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | 3 | 0x55AA | |||||||||