File Coverage

blib/lib/Net/BitTorrent/Emitter.pm
Criterion Covered Total %
statement 8 8 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 11 11 100.0


line stmt bran cond sub pod time code
1 49     49   664 use v5.40;
  49         232  
2 49     49   318 use feature 'class', 'try';
  49         136  
  49         7190  
3 49     49   293 no warnings 'experimental::class', 'experimental::try';
  49         100  
  49         41952  
4             class Net::BitTorrent::Emitter v2.0.0 {
5             field %on; # event_name => [ sub { ... }, ... ]
6             field $parent_emitter : writer;
7              
8             method on ( $event, $cb ) {
9             push $on{$event}->@*, $cb;
10             return $self;
11             }
12              
13             method _emit ( $event, @args ) {
14             if ( $event eq 'log' ) {
15             my %extra;
16             if ( @args % 2 != 0 ) {
17             my $msg = shift @args;
18             %extra = ( log => $msg, @args );
19             }
20             else {
21             %extra = @args;
22             }
23             if ( ( $extra{level} // '' ) eq 'fatal' ) {
24             die $extra{log};
25             }
26             }
27             if ( exists $on{$event} ) {
28             for my $cb ( $on{$event}->@* ) {
29             try {
30             $cb->( $self, @args );
31             }
32             catch ($e) {
33             warn " [ERROR] Callback for $event failed: $e";
34             }
35             }
36             }
37             if ( defined $parent_emitter ) {
38             $parent_emitter->_emit( $event, @args );
39             }
40             }
41             };
42             1;