| 1 | package MooX::Role::EventEmitter; | ||||||||||||
| 2 | 1 | ▶ | use Moo::Role 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use 5.020; # signatures | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use feature 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Carp 'croak'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | use Scalar::Util 'weaken'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | # Basically modeled after Mojo::EventEmitter | ||||||||||||
| 11 | |||||||||||||
| 12 | 1 | our $VERSION = '0.04'; | |||||||||||
| 13 | |||||||||||||
| 14 | =head1 NAME | ||||||||||||
| 15 | |||||||||||||
| 16 | MooX::Role::EventEmitter - Event emitter role | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||
| 19 | |||||||||||||
| 20 | package My::Thing; | ||||||||||||
| 21 | use 5.020; | ||||||||||||
| 22 | use feature 'signatures'; | ||||||||||||
| 23 | no warnings 'experimental::signatures'; | ||||||||||||
| 24 | use Moo 2; | ||||||||||||
| 25 | with 'MooX::Role::EventEmitter'; | ||||||||||||
| 26 | |||||||||||||
| 27 | sub event_received( $self, $ev ) { | ||||||||||||
| 28 | $self->emit( myevent => $ev ); | ||||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | # ... later, in your client | ||||||||||||
| 32 | |||||||||||||
| 33 | package main; | ||||||||||||
| 34 | my $foo = My::Thing->new(); | ||||||||||||
| 35 | $foo->on( myevent => sub( $ev ) { | ||||||||||||
| 36 | say "I receivend an event"; | ||||||||||||
| 37 | }); | ||||||||||||
| 38 | |||||||||||||
| 39 | =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | has 'events' => ( | ||||||||||||
| 42 | is => 'lazy', | ||||||||||||
| 43 | 0 | ▶ | default => sub { +{} }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 44 | 1 | ); | |||||||||||
| 45 | |||||||||||||
| 46 | =head1 METHODS | ||||||||||||
| 47 | |||||||||||||
| 48 | =head2 C<< $obj->emit $name, @args >> | ||||||||||||
| 49 | |||||||||||||
| 50 | Emit an event | ||||||||||||
| 51 | |||||||||||||
| 52 | =cut | ||||||||||||
| 53 | |||||||||||||
| 54 | 0 | ▶ | sub emit($self, $name, @args) { | ||||||||||
|
Subroutine
emit: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 55 | 0 | ▶ | if (my $s = $self->events->{$name}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | #warn "-- Emit $name in @{[blessed $self]} (@{[scalar @$s]})\n" if DEBUG; | ||||||||||||
| 57 | 0 | for my $cb (@$s) { | |||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | try { $self->$cb(@args) } | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 60 | 0 | ▶ | catch { warn "on $name callback died: $_" } | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 61 | 0 | } | |||||||||||
| 62 | } | ||||||||||||
| 63 | #else { | ||||||||||||
| 64 | # warn "-- Emit $name in @{[blessed $self]} (0)\n" if DEBUG; | ||||||||||||
| 65 | # die "@{[blessed $self]}: $_[0]" if $name eq 'error'; | ||||||||||||
| 66 | #} | ||||||||||||
| 67 | 0 | return $self; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | =head2 C<< $obj->has_subscribers $name >> | ||||||||||||
| 71 | |||||||||||||
| 72 | Check if an event has subscribers. | ||||||||||||
| 73 | |||||||||||||
| 74 | =cut | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | sub has_subscribers( $self, $name ) { | ||||||||||
|
Subroutine
has_subscribers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 77 | 0 | !!$self->events->{$name} | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | =head2 C<< $obj->on $name, @args >> | ||||||||||||
| 81 | |||||||||||||
| 82 | my $cb = $obj->on( myevent => sub { ... }); | ||||||||||||
| 83 | |||||||||||||
| 84 | Subscribe to an event. | ||||||||||||
| 85 | |||||||||||||
| 86 | =cut | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | ▶ | sub on($self, $name, $cb) { | ||||||||||
|
Subroutine
on: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 89 | 0 | ▶ | push @{$self->events->{$name}}, $cb and return $cb | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | =head2 C<< $obj->once $name, @args >> | ||||||||||||
| 93 | |||||||||||||
| 94 | my $cb = $obj->once( myevent => sub { ... }); | ||||||||||||
| 95 | |||||||||||||
| 96 | Subscribe to an event for just one event. | ||||||||||||
| 97 | |||||||||||||
| 98 | =cut | ||||||||||||
| 99 | |||||||||||||
| 100 | 0 | ▶ | sub once($self, $name, $cb) { | ||||||||||
|
Subroutine
once: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 101 | 0 | weaken $self; | |||||||||||
| 102 | 0 | ▶ | my $wrapper = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 103 | 0 | $self->unsubscribe($name => __SUB__); | |||||||||||
| 104 | 0 | goto &$cb; | |||||||||||
| 105 | 0 | }; | |||||||||||
| 106 | 0 | $self->on($name => $wrapper); | |||||||||||
| 107 | |||||||||||||
| 108 | 0 | return $wrapper; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | =head2 C<< $obj->subscribers( $name ) >> | ||||||||||||
| 112 | |||||||||||||
| 113 | my $s = $obj->subscribers( 'myevent' ); | ||||||||||||
| 114 | |||||||||||||
| 115 | Return an arrayref of the subscribers for an event. | ||||||||||||
| 116 | |||||||||||||
| 117 | =cut | ||||||||||||
| 118 | |||||||||||||
| 119 | 0 | ▶ | sub subscribers($self,$name) { | ||||||||||
|
Subroutine
subscribers: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 120 | 0 | ▶ | $self->events->{ $name } //= [] | ||||||||||
|
Condition0%0/2
$self->events->{$name} //= []
MC/DC0%0/1
$self->events->{$name} //= []
$self->events->{$name}
Truth table0%0/2
A
A= $self->events->{$name}
| |||||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | =head2 C<< $obj->unsubscribe( $name => $cb ) >> | ||||||||||||
| 124 | |||||||||||||
| 125 | $obj->unsubscribe('myevent', $cb); # a specific callback | ||||||||||||
| 126 | $obj->unsubscribe('myevent'); # all callbacks | ||||||||||||
| 127 | |||||||||||||
| 128 | Unsubscribe from event. | ||||||||||||
| 129 | |||||||||||||
| 130 | =cut | ||||||||||||
| 131 | |||||||||||||
| 132 | 0 | ▶ | sub unsubscribe($self, $name, $cb=undef) { | ||||||||||
|
Subroutine
unsubscribe: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 133 | # One | ||||||||||||
| 134 | 0 | ▶ | if ($cb) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | 0 | @{$self->events->{$name}} = grep { $cb ne $_ } @{$self->events->{$name}}; | |||||||||||
| 136 | 0 | ▶ | delete $self->events->{$name} unless @{$self->events->{$name}}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | } else { | ||||||||||||
| 138 | 0 | delete $self->events->{$name} | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 0 | return $self; | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 1 | 1; | |||||||||||
| 145 | |||||||||||||
| 146 | =head1 SEE ALSO | ||||||||||||
| 147 | |||||||||||||
| 148 | L<Mojo::EventEmitter> - the module this API is based on | ||||||||||||
| 149 | |||||||||||||
| 150 | =cut | ||||||||||||