line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (c) 2011-16, Mitchell Cooper |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Evented::Object: a simple yet featureful base class event framework. |
4
|
|
|
|
|
|
|
# https://github.com/cooper/evented-object |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Evented::Object::EventFire; # leave this package name the same FOREVER. |
7
|
|
|
|
|
|
|
|
8
|
13
|
|
|
13
|
|
74
|
use warnings; |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
323
|
|
9
|
13
|
|
|
13
|
|
56
|
use strict; |
|
13
|
|
|
|
|
24
|
|
|
13
|
|
|
|
|
199
|
|
10
|
13
|
|
|
13
|
|
63
|
use utf8; |
|
13
|
|
|
|
|
27
|
|
|
13
|
|
|
|
|
55
|
|
11
|
13
|
|
|
13
|
|
405
|
use 5.010; |
|
13
|
|
|
|
|
55
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
########################## |
14
|
|
|
|
|
|
|
### EVENT FIRE OBJECTS ### |
15
|
|
|
|
|
|
|
########################## |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '5.63'; |
18
|
|
|
|
|
|
|
our $events = $Evented::Object::events; |
19
|
|
|
|
|
|
|
our $props = $Evented::Object::props; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# create a new event object. |
22
|
|
|
|
|
|
|
sub new { |
23
|
13
|
|
|
13
|
0
|
78
|
my ($class, %opts) = @_; |
24
|
13
|
|
50
|
|
|
113
|
$opts{callback_i} ||= 0; |
25
|
13
|
|
|
|
|
60
|
return bless { $props => \%opts }, $class; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# cancel all future callbacks once. |
29
|
|
|
|
|
|
|
# if stopped already, returns the reason. |
30
|
|
|
|
|
|
|
sub stop { |
31
|
1
|
|
|
1
|
0
|
8
|
my ($fire, $reason) = @_; |
32
|
1
|
|
50
|
|
|
13
|
$fire->{$props}{stop} ||= $reason || 'unspecified'; |
|
|
|
33
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# returns a true value if the given callback has been called. |
36
|
|
|
|
|
|
|
# with no argument, returns number of callbacks called so far. |
37
|
|
|
|
|
|
|
sub called { |
38
|
0
|
|
|
0
|
0
|
0
|
my ($fire, $cb_name) = @_; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# return the number of callbacks called. |
41
|
|
|
|
|
|
|
# this includes the current callback. |
42
|
0
|
0
|
|
|
|
0
|
if (!length $cb_name) { |
43
|
0
|
|
|
|
|
0
|
my $called = scalar keys %{ $fire->{$props}{called} }; |
|
0
|
|
|
|
|
0
|
|
44
|
0
|
0
|
|
|
|
0
|
$called++ unless $fire->{$props}{complete}; |
45
|
0
|
|
|
|
|
0
|
return $called; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# return whether the specified callback was called. |
49
|
0
|
0
|
|
|
|
0
|
my $cb_id = $fire->_cb_id($cb_name) or return; |
50
|
0
|
|
|
|
|
0
|
return $fire->{$props}{called}{$cb_id}; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# returns a true value if the given callback will be called soon. |
54
|
|
|
|
|
|
|
# with no argument, returns number of callbacks pending. |
55
|
|
|
|
|
|
|
sub pending { |
56
|
2
|
|
|
2
|
0
|
14
|
my ($fire, $cb_name) = @_; |
57
|
2
|
|
|
|
|
5
|
my $pending = $fire->{$props}{collection}{pending}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# return number of callbacks remaining. |
60
|
2
|
50
|
|
|
|
8
|
if (!length $cb_name) { |
61
|
0
|
|
|
|
|
0
|
return scalar keys %$pending; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# return whether the specified callback is pending. |
65
|
2
|
50
|
|
|
|
5
|
my $cb_id = $fire->_cb_id($cb_name) or return; |
66
|
2
|
|
|
|
|
6
|
return $pending->{$cb_id}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# cancels a future callback once. |
70
|
|
|
|
|
|
|
sub cancel { |
71
|
1
|
|
|
1
|
0
|
7
|
my ($fire, $cb_name) = @_; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# if there is no argument given, we will just |
74
|
|
|
|
|
|
|
# treat this like a ->stop on the event. |
75
|
1
|
50
|
|
|
|
3
|
length $cb_name or return $fire->stop; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# cancel the callback. |
78
|
1
|
50
|
|
|
|
4
|
my $cb_id = $fire->_cb_id($cb_name) or return; |
79
|
1
|
|
|
|
|
4
|
delete $fire->{$props}{collection}{pending}{$cb_id}; |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
5
|
return 1; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# returns the return value of the given callback. |
85
|
|
|
|
|
|
|
# if it has not yet been called, this will return undef. |
86
|
|
|
|
|
|
|
# if the return value has a possibility of being undef, |
87
|
|
|
|
|
|
|
# the only way to be sure is to first test ->callback_called. |
88
|
|
|
|
|
|
|
sub return_of { |
89
|
0
|
|
|
0
|
0
|
0
|
my ($fire, $cb_name) = @_; |
90
|
0
|
0
|
|
|
|
0
|
my $cb_id = $fire->_cb_id($cb_name) or return; |
91
|
0
|
|
|
|
|
0
|
return $fire->{$props}{return}{$cb_id}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# returns the callback that was last called. |
95
|
|
|
|
|
|
|
sub last { |
96
|
0
|
|
|
0
|
0
|
0
|
shift->{$props}{last_callback}; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# returns the return value of the last-called callback. |
100
|
|
|
|
|
|
|
sub last_return { |
101
|
0
|
|
|
0
|
0
|
0
|
shift->{$props}{last_return}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# returns the callback that stopped the event. |
105
|
|
|
|
|
|
|
sub stopper { |
106
|
0
|
|
|
0
|
0
|
0
|
shift->{$props}{stopper}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# returns the name of the event being fired. |
110
|
|
|
|
|
|
|
# this isn't reliable afterward because it can differ within one fire. |
111
|
|
|
|
|
|
|
sub event_name { |
112
|
2
|
|
|
2
|
0
|
19
|
shift->{$props}{name}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# returns the name of the callback being called. |
116
|
|
|
|
|
|
|
sub callback_name { |
117
|
6
|
|
|
6
|
0
|
33
|
shift->{$props}{callback_name}; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# returns the caller(1) value of ->fire_event(). |
121
|
|
|
|
|
|
|
sub caller { |
122
|
0
|
|
|
0
|
0
|
0
|
@{ shift->{$props}{caller} }; |
|
0
|
|
|
|
|
0
|
|
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# returns the priority of the callback being called. |
126
|
|
|
|
|
|
|
sub callback_priority { |
127
|
0
|
|
|
0
|
0
|
0
|
shift->{$props}{callback_priority}; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# returns the value of the 'data' option when the callback was registered. |
131
|
|
|
|
|
|
|
# if an argument is provided, it is used as the key to the data hash. |
132
|
|
|
|
|
|
|
sub callback_data { |
133
|
1
|
|
|
1
|
0
|
6
|
my $data = shift->{$props}{callback_data}; |
134
|
1
|
|
|
|
|
2
|
my $key_maybe = shift; |
135
|
1
|
50
|
|
|
|
4
|
if (ref $data eq 'HASH') { |
136
|
0
|
0
|
|
|
|
0
|
return $data->{$key_maybe} if defined $key_maybe; |
137
|
0
|
|
0
|
|
|
0
|
return $data->{data} // $data; |
138
|
|
|
|
|
|
|
} |
139
|
1
|
|
|
|
|
5
|
return $data; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# returns the value of the 'data' option on the ->fire(). |
143
|
|
|
|
|
|
|
# if an argument is provided, it is used as the key to the data hash. |
144
|
|
|
|
|
|
|
sub data { |
145
|
3
|
|
|
3
|
0
|
19
|
my $data = shift->{$props}{data}; |
146
|
3
|
|
|
|
|
6
|
my $key_maybe = shift; |
147
|
3
|
100
|
|
|
|
12
|
if (ref $data eq 'HASH') { |
148
|
2
|
100
|
|
|
|
9
|
return $data->{$key_maybe} if defined $key_maybe; |
149
|
1
|
|
33
|
|
|
6
|
return $data->{data} // $data; |
150
|
|
|
|
|
|
|
} |
151
|
1
|
|
|
|
|
9
|
return $data; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# returns the evented object. |
155
|
|
|
|
|
|
|
sub object { |
156
|
2
|
|
|
2
|
0
|
9
|
shift->{$props}{object}; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# returns the exception from 'safe' option, if any. |
160
|
|
|
|
|
|
|
sub exception { |
161
|
0
|
|
|
0
|
0
|
0
|
shift->{$props}{exception}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# find a callback ID from a callback name. |
165
|
|
|
|
|
|
|
sub _cb_id { |
166
|
3
|
|
|
3
|
|
6
|
my ($fire, $cb_name) = @_; |
167
|
3
|
|
|
|
|
13
|
return $fire->{$props}{callback_ids}{$cb_name}; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
############### |
171
|
|
|
|
|
|
|
### ALIASES ### |
172
|
|
|
|
|
|
|
############### |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub object; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
BEGIN { |
177
|
13
|
|
|
13
|
|
277
|
*eo = *object; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; |