line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: A Node style event Role for Moose |
2
|
|
|
|
|
|
|
package MooseX::Event::Role; |
3
|
|
|
|
|
|
|
{ |
4
|
|
|
|
|
|
|
$MooseX::Event::Role::VERSION = '0.3.0_2'; |
5
|
|
|
|
|
|
|
} |
6
|
4
|
|
|
4
|
|
22
|
use MooseX::Event (); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
90
|
|
7
|
4
|
|
|
4
|
|
19
|
use Any::Moose 'Role'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
21
|
|
8
|
4
|
|
|
4
|
|
10039
|
use Scalar::Util qw( refaddr reftype blessed ); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
609
|
|
9
|
4
|
|
|
4
|
|
2007
|
use Event::Wrappable (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub metaevent { |
13
|
|
|
|
|
|
|
my $self = shift; |
14
|
|
|
|
|
|
|
my( $event ) = @_; |
15
|
|
|
|
|
|
|
my $accessor = $self->can("event:$event"); |
16
|
|
|
|
|
|
|
return defined $accessor ? $self->$accessor() : undef; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub get_all_events { |
21
|
|
|
|
|
|
|
my $self = shift; |
22
|
|
|
|
|
|
|
return map {substr($_,6)} grep {/^event:/} map {$_->name} $self->meta->get_all_attributes; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub event_listeners { |
27
|
|
|
|
|
|
|
my $self = shift; |
28
|
|
|
|
|
|
|
my( $event ) = @_; |
29
|
|
|
|
|
|
|
my $emeta = $self->metaevent($event); |
30
|
|
|
|
|
|
|
unless ( $emeta ) { |
31
|
|
|
|
|
|
|
require Carp; |
32
|
|
|
|
|
|
|
Carp::confess("Event $event does not exist"); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
my @listeners = values %{$emeta->listeners}; |
35
|
|
|
|
|
|
|
return wantarray? @listeners : scalar @listeners; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Having the first argument flatten the argument list isn't actually allowed |
39
|
|
|
|
|
|
|
# in Rakudo (and possibly P6 too) |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub on { |
43
|
|
|
|
|
|
|
my $self = shift; |
44
|
|
|
|
|
|
|
my $listener = pop; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# If it's not an Event::Wrappable object, make it one. |
47
|
|
|
|
|
|
|
if ( ! blessed $listener or ! $listener->isa("Event::Wrappable") ) { |
48
|
|
|
|
|
|
|
$listener = &Event::Wrappable::event( $listener ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
for my $event (@_) { |
52
|
|
|
|
|
|
|
my $emeta = $self->metaevent($event); |
53
|
|
|
|
|
|
|
unless ( $emeta ) { |
54
|
|
|
|
|
|
|
require Carp; |
55
|
|
|
|
|
|
|
Carp::confess("Event $event does not exist"); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
$emeta->listen( $listener ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
return $listener; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub once { |
63
|
|
|
|
|
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
my $listener = pop; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# If it's not an Event::Wrappable object, make it one. |
67
|
|
|
|
|
|
|
if ( ! blessed $listener or ! $listener->isa("Event::Wrappable") ) { |
68
|
|
|
|
|
|
|
$listener = &Event::Wrappable::event( $listener ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
for my $event (@_) { |
72
|
|
|
|
|
|
|
my $emeta = $self->metaevent($event); |
73
|
|
|
|
|
|
|
unless ( $emeta ) { |
74
|
|
|
|
|
|
|
require Carp; |
75
|
|
|
|
|
|
|
Carp::confess("Event $event does not exist"); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
$emeta->listen_once( $listener ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
return $listener; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub emit { |
83
|
|
|
|
|
|
|
my $self = shift; |
84
|
|
|
|
|
|
|
my( $event, @args ) = @_; |
85
|
|
|
|
|
|
|
# The event object attributes are lazy, so if one doesn't exist yet |
86
|
|
|
|
|
|
|
# don't trigger the creation of it just to fire events into the void |
87
|
|
|
|
|
|
|
if ( reftype $self eq 'HASH' ) { |
88
|
|
|
|
|
|
|
return unless exists $self->{"event:$event"}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
my $emeta = $self->metaevent($event); |
91
|
|
|
|
|
|
|
unless ( $emeta ) { |
92
|
|
|
|
|
|
|
require Carp; |
93
|
|
|
|
|
|
|
Carp::confess("Event $event does not exist"); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
$emeta->emit_self( @args ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub remove_all_listeners { |
101
|
|
|
|
|
|
|
my $self = shift; |
102
|
|
|
|
|
|
|
foreach ($self->get_all_events) { |
103
|
|
|
|
|
|
|
$self->metaevent($_)->stop_all_listeners; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub remove_listener { |
109
|
|
|
|
|
|
|
my $self = shift; |
110
|
|
|
|
|
|
|
my( $event, $listener ) = @_; |
111
|
|
|
|
|
|
|
my $emeta = $self->metaevent($event); |
112
|
|
|
|
|
|
|
unless ( $emeta ) { |
113
|
|
|
|
|
|
|
require Carp; |
114
|
|
|
|
|
|
|
Carp::confess("Event $event does not exist"); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
$emeta->stop_listener($listener); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |