| 1 | 55 | ▶ | use v5.40; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 55 | ▶ | use feature 'class', 'try'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 55 | ▶ | no warnings 'experimental::class', 'experimental::try'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 55 | class Net::BitTorrent::Emitter v2.1.1 { | |||||||||||||||||||||||||||
| 5 | 55 | field %on; # event_name => [ sub { ... }, ... ] | |||||||||||||||||||||||||||
| 6 | 55 | field $parent_emitter : reader; | |||||||||||||||||||||||||||
| 7 | 55 | ▶ | use constant MAX_LISTENERS => 100; # Max callbacks per event | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1306 | ▶ | method on ( $event, $cb ) { | ||||||||||||||||||||||||||
|
Subroutine
on: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 1306 | ▶ | if ( exists $on{$event} && $on{$event}->@* >= MAX_LISTENERS ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $on{$event} and @{$on{$event}} >= 100
MC/DC100%2/2
exists $on{$event} and @{$on{$event}} >= 100
exists $on{$event}
@{$on{$event}} >= 100
Truth table100%3/3
A and B
A= exists $on{$event}B= @{$on{$event}} >= 100
| |||||||||||||||||||||||||||||
| 11 | 1 | warn "Too many listeners for event '$event', ignoring"; | |||||||||||||||||||||||||||
| 12 | 1 | return $self; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | 1305 | push $on{$event}->@*, $cb; | |||||||||||||||||||||||||||
| 15 | 1305 | return $self; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | method off ( $event, $cb = undef ) { | ||||||||||||||||||||||||||
|
Subroutine
off: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $on{$event} = [ grep { $_ ne $cb } $on{$event}->@* ] if exists $on{$event}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | else { | ||||||||||||||||||||||||||||
| 23 | 0 | delete $on{$event}; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 0 | return $self; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 257 | ▶ | method set_parent_emitter ($parent) { | ||||||||||||||||||||||||||
|
Subroutine
set_parent_emitter: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 257 | ▶ | if ( defined $parent ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 257 | my $current = $parent; | |||||||||||||||||||||||||||
| 31 | 257 | for my $depth ( 0 .. 10 ) { | |||||||||||||||||||||||||||
| 32 | 762 | ▶ | last unless defined $current; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 506 | ▶ | if ( $current eq $self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | warn 'Cycle detected in parent emitter chain, ignoring'; | |||||||||||||||||||||||||||
| 35 | 1 | return; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 505 | $current = eval { $current->parent_emitter() }; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 256 | $parent_emitter = $parent; | |||||||||||||||||||||||||||
| 41 | 256 | ▶ | builtin::weaken($parent_emitter) if defined $parent_emitter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 20955 | ▶ | method _emit ( $event, @args ) { | ||||||||||||||||||||||||||
|
Subroutine
_emit: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||
| 45 | 20955 | state $depth = 0; | |||||||||||||||||||||||||||
| 46 | 20955 | $depth++; | |||||||||||||||||||||||||||
| 47 | 20955 | ▶ | if ( $depth > 100 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $depth--; | |||||||||||||||||||||||||||
| 49 | 0 | warn "Emitter recursion depth exceeded for event '$event', stopping"; | |||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 20955 | ▶ | if ( $event eq 'log' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 259 | my %extra; | |||||||||||||||||||||||||||
| 54 | 259 | ▶ | if ( @args % 2 != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 224 | my $msg = shift @args; | |||||||||||||||||||||||||||
| 56 | 224 | %extra = ( log => $msg, @args ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 35 | %extra = @args; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 259 | ▶ | if ( ( $extra{level} // '' ) eq 'fatal' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$extra{"level"} // ""
MC/DC0%0/1
$extra{"level"} // ""
$extra{"level"}
Truth table50%1/2
A
A= $extra{"level"}
| |||||||||||||||||||||||||||||
| 62 | 6 | $depth--; | |||||||||||||||||||||||||||
| 63 | 6 | die $extra{log}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 253 | @args = %extra; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 20949 | ▶ | if ( exists $on{$event} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 76 | for my $cb ( $on{$event}->@* ) { | |||||||||||||||||||||||||||
| 69 | 90 | try { | |||||||||||||||||||||||||||
| 70 | 90 | $cb->( $self, @args ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | catch ($e) { | ||||||||||||||||||||||||||||
| 73 | 0 | warn "Callback for $event failed: $e"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 20949 | ▶ | if ( defined $parent_emitter ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 10365 | $parent_emitter->_emit( $event, @args ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 20949 | $depth--; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 224 | ▶ | method _emit_log ( $level, $message, @extra ) { | ||||||||||||||||||||||||||
|
Subroutine
_emit_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 224 | $message =~ s/\n+$//; | |||||||||||||||||||||||||||
| 85 | 224 | $self->_emit( log => $message, level => $level, @extra ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | 55 | 1; | |||||||||||||||||||||||||||