| 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 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 13 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | 13 | ▶ | use utf8; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | 13 | ▶ | use 5.010; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | ########################## | ||||||||||||||||||||||||
| 14 | ### EVENT FIRE OBJECTS ### | ||||||||||||||||||||||||
| 15 | ########################## | ||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | 13 | our $VERSION = '5.63'; | |||||||||||||||||||||||
| 18 | 13 | our $events = $Evented::Object::events; | |||||||||||||||||||||||
| 19 | 13 | our $props = $Evented::Object::props; | |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | # create a new event object. | ||||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||||
| 23 | 13 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||
| 24 | 13 | ▶ | $opts{callback_i} ||= 0; | ||||||||||||||||||||||
|
Condition50%1/2
$opts{"callback_i"} ||= 0
MC/DC0%0/1
$opts{"callback_i"} ||= 0
$opts{"callback_i"}
Truth table50%1/2
A
A= $opts{"callback_i"}
| |||||||||||||||||||||||||
| 25 | 13 | 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 | ▶ | my ($fire, $reason) = @_; | ||||||||||||||||||||||
|
Subroutine
stop: called CC=3 SCAR=13.1
Pod
undocumented
| |||||||||||||||||||||||||
| 32 | 1 | ▶ | $fire->{$props}{stop} ||= $reason || 'unspecified'; | ||||||||||||||||||||||
|
Condition40%2/5
$fire->{$Evented::Object::EventFire::props}{"stop"} ||= $reason || "unspecified"
$reason || 'unspecified'
MC/DC0%0/2
$fire->{$Evented::Object::EventFire::props}{"stop"} ||= $reason || "unspecified"
$fire->{$Evented::Object::EventFire::props}{"stop"}
$reason
Truth table0%0/3
A ||= B
A= $fire->{$Evented::Object::EventFire::props}{"stop"}B= $reason
| |||||||||||||||||||||||||
| 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 | ▶ | my ($fire, $cb_name) = @_; | ||||||||||||||||||||||
|
Subroutine
called: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | # return the number of callbacks called. | ||||||||||||||||||||||||
| 41 | # this includes the current callback. | ||||||||||||||||||||||||
| 42 | 0 | ▶ | if (!length $cb_name) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | 0 | my $called = scalar keys %{ $fire->{$props}{called} }; | |||||||||||||||||||||||
| 44 | 0 | ▶ | $called++ unless $fire->{$props}{complete}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 45 | 0 | return $called; | |||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | # return whether the specified callback was called. | ||||||||||||||||||||||||
| 49 | 0 | ▶ | my $cb_id = $fire->_cb_id($cb_name) or return; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 50 | 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 | ▶ | my ($fire, $cb_name) = @_; | ||||||||||||||||||||||
|
Subroutine
pending: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||
| 57 | 2 | my $pending = $fire->{$props}{collection}{pending}; | |||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||
| 59 | # return number of callbacks remaining. | ||||||||||||||||||||||||
| 60 | 2 | ▶ | if (!length $cb_name) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 61 | 0 | return scalar keys %$pending; | |||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||
| 64 | # return whether the specified callback is pending. | ||||||||||||||||||||||||
| 65 | 2 | ▶ | my $cb_id = $fire->_cb_id($cb_name) or return; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 66 | 2 | return $pending->{$cb_id}; | |||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||
| 69 | # cancels a future callback once. | ||||||||||||||||||||||||
| 70 | sub cancel { | ||||||||||||||||||||||||
| 71 | 1 | ▶ | my ($fire, $cb_name) = @_; | ||||||||||||||||||||||
|
Subroutine
cancel: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | # if there is no argument given, we will just | ||||||||||||||||||||||||
| 74 | # treat this like a ->stop on the event. | ||||||||||||||||||||||||
| 75 | 1 | ▶ | length $cb_name or return $fire->stop; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | # cancel the callback. | ||||||||||||||||||||||||
| 78 | 1 | ▶ | my $cb_id = $fire->_cb_id($cb_name) or return; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 79 | 1 | delete $fire->{$props}{collection}{pending}{$cb_id}; | |||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | 1 | 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 | ▶ | my ($fire, $cb_name) = @_; | ||||||||||||||||||||||
|
Subroutine
return_of: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||
| 90 | 0 | ▶ | my $cb_id = $fire->_cb_id($cb_name) or return; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 91 | 0 | return $fire->{$props}{return}{$cb_id}; | |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||
| 94 | # returns the callback that was last called. | ||||||||||||||||||||||||
| 95 | sub last { | ||||||||||||||||||||||||
| 96 | 0 | ▶ | shift->{$props}{last_callback}; | ||||||||||||||||||||||
|
Subroutine
last: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | # returns the return value of the last-called callback. | ||||||||||||||||||||||||
| 100 | sub last_return { | ||||||||||||||||||||||||
| 101 | 0 | ▶ | shift->{$props}{last_return}; | ||||||||||||||||||||||
|
Subroutine
last_return: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||
| 104 | # returns the callback that stopped the event. | ||||||||||||||||||||||||
| 105 | sub stopper { | ||||||||||||||||||||||||
| 106 | 0 | ▶ | shift->{$props}{stopper}; | ||||||||||||||||||||||
|
Subroutine
stopper: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 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 | ▶ | shift->{$props}{name}; | ||||||||||||||||||||||
|
Subroutine
event_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||
| 115 | # returns the name of the callback being called. | ||||||||||||||||||||||||
| 116 | sub callback_name { | ||||||||||||||||||||||||
| 117 | 6 | ▶ | shift->{$props}{callback_name}; | ||||||||||||||||||||||
|
Subroutine
callback_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | # returns the caller(1) value of ->fire_event(). | ||||||||||||||||||||||||
| 121 | sub caller { | ||||||||||||||||||||||||
| 122 | 0 | ▶ | @{ shift->{$props}{caller} }; | ||||||||||||||||||||||
|
Subroutine
caller: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | # returns the priority of the callback being called. | ||||||||||||||||||||||||
| 126 | sub callback_priority { | ||||||||||||||||||||||||
| 127 | 0 | ▶ | shift->{$props}{callback_priority}; | ||||||||||||||||||||||
|
Subroutine
callback_priority: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 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 | ▶ | my $data = shift->{$props}{callback_data}; | ||||||||||||||||||||||
|
Subroutine
callback_data: called CC=4 SCAR=20.4
Pod
undocumented
| |||||||||||||||||||||||||
| 134 | 1 | my $key_maybe = shift; | |||||||||||||||||||||||
| 135 | 1 | ▶ | if (ref $data eq 'HASH') { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 136 | 0 | ▶ | return $data->{$key_maybe} if defined $key_maybe; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 137 | 0 | ▶ | return $data->{data} // $data; | ||||||||||||||||||||||
|
Condition0%0/3
$data->{"data"} // $data
MC/DC0%0/2
$data->{"data"} // $data
$data->{"data"}
$data
Truth table0%0/3
A // B
A= $data->{"data"}B= $data
| |||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||
| 139 | 1 | 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 | ▶ | my $data = shift->{$props}{data}; | ||||||||||||||||||||||
|
Subroutine
data: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||
| 146 | 3 | my $key_maybe = shift; | |||||||||||||||||||||||
| 147 | 3 | ▶ | if (ref $data eq 'HASH') { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 148 | 2 | ▶ | return $data->{$key_maybe} if defined $key_maybe; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 149 | 1 | ▶ | return $data->{data} // $data; | ||||||||||||||||||||||
|
Condition33%1/3
$data->{"data"} // $data
MC/DC0%0/2
$data->{"data"} // $data
$data->{"data"}
$data
Truth table33%1/3
A // B
A= $data->{"data"}B= $data
| |||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||
| 151 | 1 | return $data; | |||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||
| 154 | # returns the evented object. | ||||||||||||||||||||||||
| 155 | sub object { | ||||||||||||||||||||||||
| 156 | 2 | ▶ | shift->{$props}{object}; | ||||||||||||||||||||||
|
Subroutine
object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||
| 159 | # returns the exception from 'safe' option, if any. | ||||||||||||||||||||||||
| 160 | sub exception { | ||||||||||||||||||||||||
| 161 | 0 | ▶ | shift->{$props}{exception}; | ||||||||||||||||||||||
|
Subroutine
exception: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||
| 164 | # find a callback ID from a callback name. | ||||||||||||||||||||||||
| 165 | sub _cb_id { | ||||||||||||||||||||||||
| 166 | 3 | ▶ | my ($fire, $cb_name) = @_; | ||||||||||||||||||||||
|
Subroutine
_cb_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 167 | 3 | return $fire->{$props}{callback_ids}{$cb_name}; | |||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||
| 170 | ############### | ||||||||||||||||||||||||
| 171 | ### ALIASES ### | ||||||||||||||||||||||||
| 172 | ############### | ||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||
| 174 | sub object; | ||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||
| 176 | BEGIN { | ||||||||||||||||||||||||
| 177 | 13 | ▶ | *eo = *object; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||
| 180 | 13 | 1; | |||||||||||||||||||||||