line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::EventEmitter; |
2
|
|
|
|
|
|
|
$Hubot::EventEmitter::VERSION = '0.2.7'; |
3
|
1
|
|
|
1
|
|
1803
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use namespace::autoclean; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has 'events' => ( is => 'rw', isa => 'HashRef', default => sub { {} }, ); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub emit { |
9
|
|
|
|
|
|
|
my ( $self, $name ) = ( shift, shift ); |
10
|
|
|
|
|
|
|
if ( my $s = $self->events->{$name} ) { |
11
|
|
|
|
|
|
|
for my $cb (@$s) { $self->$cb(@_) } |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
return $self; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub on { |
17
|
|
|
|
|
|
|
my ( $self, $name, $cb ) = @_; |
18
|
|
|
|
|
|
|
push @{ $self->{events}{$name} ||= [] }, $cb; |
19
|
|
|
|
|
|
|
return $cb; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
1; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=pod |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=encoding utf-8 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 NAME |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Hubot::EventEmitter |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 VERSION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
version 0.2.7 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 SYNOPSIS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
package Foo; |
41
|
|
|
|
|
|
|
use Moose; |
42
|
|
|
|
|
|
|
extends 'Hubot::EventEmitter'; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
package main; |
45
|
|
|
|
|
|
|
my $foo = Foo->new; |
46
|
|
|
|
|
|
|
$foo->on( |
47
|
|
|
|
|
|
|
'event1', |
48
|
|
|
|
|
|
|
sub { |
49
|
|
|
|
|
|
|
my ($e, @args) = @_; # $e is event emitter. ignore. |
50
|
|
|
|
|
|
|
print "@args\n"; # 1 2 3 4 |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$foo->emit('event1', 1, 2, 3, 4); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 DESCRIPTION |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
subscribe event via C<on> then execute callback via C<emit>. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 METHODS |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 on |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
args - C<event-name>, C<callback> |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 emit |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
args - C<event-name>, C<@arg-pass-to-callback> |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 AUTHOR |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Hyungsuk Hong. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
79
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |