line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: /mirror/perl/Event-Notify/trunk/lib/Event/Notify.pm 31297 2007-11-29T11:30:40.898880Z daisuke $ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 2007 Daisuke Maki |
4
|
|
|
|
|
|
|
# All rights reserved. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Event::Notify; |
7
|
2
|
|
|
2
|
|
43871
|
use strict; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
90
|
|
8
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
94
|
|
9
|
2
|
|
|
2
|
|
22
|
use vars qw($VERSION); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
147
|
|
10
|
|
|
|
|
|
|
$VERSION = '0.00005'; |
11
|
2
|
|
|
2
|
|
11
|
use Carp (); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1545
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new |
14
|
|
|
|
|
|
|
{ |
15
|
1
|
|
|
1
|
1
|
13
|
my $class = shift; |
16
|
1
|
|
|
|
|
6
|
return bless { observers => {} }, $class; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub register |
20
|
|
|
|
|
|
|
{ |
21
|
2
|
|
|
2
|
1
|
1487
|
my ($self, $observer) = @_; |
22
|
2
|
50
|
|
|
|
8
|
$observer->register($self) if $observer->can('register'); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub register_event |
26
|
|
|
|
|
|
|
{ |
27
|
11
|
|
|
11
|
1
|
1060
|
my($self, $event, $observer, $opts) = @_; |
28
|
|
|
|
|
|
|
|
29
|
11
|
|
100
|
|
|
39
|
$opts ||= {}; |
30
|
|
|
|
|
|
|
|
31
|
11
|
50
|
|
|
|
24
|
if (! $event) { |
32
|
0
|
|
|
|
|
0
|
Carp::croak( "No event was specified" ); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
11
|
|
50
|
|
|
57
|
my $ref = ref($observer || ''); |
36
|
11
|
50
|
|
|
|
21
|
if (! $ref) { |
37
|
0
|
|
|
|
|
0
|
Carp::croak( "Non-ref observer passed. Expected an object or a CODE ref"); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
11
|
|
|
|
|
10
|
my $slot; |
41
|
11
|
100
|
|
|
|
27
|
if ($ref eq 'CODE') { |
42
|
|
|
|
|
|
|
# You're not passing me a method name, are you? if so, |
43
|
|
|
|
|
|
|
# I won't croak, but I *will* complain |
44
|
3
|
50
|
|
|
|
8
|
if ($opts->{method}) { |
45
|
0
|
|
|
|
|
0
|
carp( "Useless use of method name for a CODE observer in Event::Notify->register_event()" ); |
46
|
|
|
|
|
|
|
} |
47
|
3
|
|
|
|
|
7
|
$slot = [ $observer, undef ] |
48
|
|
|
|
|
|
|
} else { |
49
|
8
|
|
100
|
|
|
25
|
my $method = $opts->{method} || 'notify'; |
50
|
|
|
|
|
|
|
|
51
|
8
|
100
|
|
|
|
26
|
if (! $observer->can($method)) { |
52
|
2
|
|
|
|
|
359
|
Carp::croak("$observer does not implement a $method() method"); |
53
|
|
|
|
|
|
|
} |
54
|
6
|
|
|
|
|
95
|
$slot = [ $observer, $method ] |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
9
|
|
50
|
|
|
44
|
$self->{observers}{$event} ||= []; |
58
|
9
|
|
|
|
|
11
|
push @{ $self->{observers}{$event} }, $slot; |
|
9
|
|
|
|
|
36
|
|
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub unregister_event |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
|
0
|
1
|
0
|
my($self, $event, $observer) = @_; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
my $observers = $self->{observers}{$event}; |
66
|
0
|
0
|
|
|
|
0
|
return () unless $observers; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
for my $i (0 .. $#{$observers}) { |
|
0
|
|
|
|
|
0
|
|
69
|
0
|
0
|
|
|
|
0
|
next unless $observers->[$i]->[0] == $observer; |
70
|
0
|
|
|
|
|
0
|
return splice(@$observers, $i, 1); |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
0
|
return (); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub notify |
76
|
|
|
|
|
|
|
{ |
77
|
12
|
|
|
12
|
1
|
6291
|
my ($self, $event, @args) = @_; |
78
|
|
|
|
|
|
|
|
79
|
12
|
|
100
|
|
|
48
|
my $observers = $self->{observers}{$event} || []; |
80
|
12
|
|
|
|
|
32
|
foreach my $data (@$observers) { |
81
|
9
|
|
|
|
|
13
|
my($o, $method) = @$data; |
82
|
9
|
100
|
|
|
|
69
|
ref $o eq 'CODE' ? |
83
|
|
|
|
|
|
|
$o->($event, @args) : |
84
|
|
|
|
|
|
|
$o->$method($event, @args) |
85
|
|
|
|
|
|
|
; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub clear_observers |
90
|
|
|
|
|
|
|
{ |
91
|
4
|
|
|
4
|
1
|
703
|
my $self = shift; |
92
|
4
|
|
|
|
|
17
|
$self->{observers} = {}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |