| 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::Collection; # 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 | 13 | ▶ | use Scalar::Util qw(weaken blessed); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use List::Util qw(min max); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use Carp qw(carp); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | 13 | our $VERSION = '5.63'; | |||||||||||||||||||||||||||||||||||
| 18 | 13 | our $events = $Evented::Object::events; | |||||||||||||||||||||||||||||||||||
| 19 | 13 | our $props = $Evented::Object::props; | |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | 13 | my $dummy; | |||||||||||||||||||||||||||||||||||
| 22 | 13 | my %boolopts = map { $_ => 1 } qw(safe return_check fail_continue); | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | sub new { | ||||||||||||||||||||||||||||||||||||
| 25 | 13 | ▶ | return bless { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 26 | pending => {}, | ||||||||||||||||||||||||||||||||||||
| 27 | default_names => {}, | ||||||||||||||||||||||||||||||||||||
| 28 | names => {} | ||||||||||||||||||||||||||||||||||||
| 29 | }, shift; | ||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | sub push_callbacks { | ||||||||||||||||||||||||||||||||||||
| 33 | 13 | ▶ | my ($collection, $callbacks, $names) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
push_callbacks: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 34 | 13 | my $pending = $collection->{pending}; | |||||||||||||||||||||||||||||||||||
| 35 | 13 | my $my_names = $collection->{names}; | |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | # add to pending callbacks and callback name-to-ID mapping. | ||||||||||||||||||||||||||||||||||||
| 38 | 13 | @$pending { keys %$callbacks } = values %$callbacks; | |||||||||||||||||||||||||||||||||||
| 39 | 13 | @$my_names{ keys %$names } = values %$names; | |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | # set default names for any callback names which were not found | ||||||||||||||||||||||||||||||||||||
| 42 | $collection->{default_names}{ $_->[2]{name} } ||= $_->[2]{id} | ||||||||||||||||||||||||||||||||||||
| 43 | 13 | ▶ | for values %$callbacks; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$collection->{"default_names"}{$_->[2]{"name"}} ||= $_->[2]{"id"}
MC/DC0%0/2
$collection->{"default_names"}{$_->[2]{"name"}} ||= $_->[2]{"id"}
$collection->{"default_names"}{$_->[2]{"name"}}
$_->[2]{"id"}
Truth table33%1/3
A ||= B
A= $collection->{"default_names"}{$_->[2]{"name"}}B= $_->[2]{"id"}
| |||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||||||||||
| 47 | # Available fire options | ||||||||||||||||||||||||||||||||||||
| 48 | # ---------------------- | ||||||||||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||||||||||
| 50 | # safe calls all callbacks within eval blocks. | ||||||||||||||||||||||||||||||||||||
| 51 | # consumes no parameter. | ||||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||||
| 53 | # return_check causes the event to ->stop if any callback returns false | ||||||||||||||||||||||||||||||||||||
| 54 | # BUT IT WAITS until all have been fired. so if one returns false, | ||||||||||||||||||||||||||||||||||||
| 55 | # the rest will be called, but $fire->stopper will be true afterward. | ||||||||||||||||||||||||||||||||||||
| 56 | # consumes no parameter. | ||||||||||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||||||||||
| 58 | # caller specify an alternate [caller 1] value, mostly for internal use. | ||||||||||||||||||||||||||||||||||||
| 59 | # parameter = caller(1) info wrapped in an array reference. | ||||||||||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||||||||||
| 61 | # fail_continue if 'safe' is enabled and a callback raises an exception, it will | ||||||||||||||||||||||||||||||||||||
| 62 | # by default ->stop the fire. this option tells it to continue instead. | ||||||||||||||||||||||||||||||||||||
| 63 | # consumes no parameter. | ||||||||||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||||||||||
| 65 | # data some data to fire with the event. esp. good for things that might be | ||||||||||||||||||||||||||||||||||||
| 66 | # useful at times but not accessed frequently enough to be an argument. | ||||||||||||||||||||||||||||||||||||
| 67 | # parameter = the data. | ||||||||||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||||||||||
| 69 | sub fire { | ||||||||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | my ($collection, @options) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
fire: called CC=10 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | # handle options. | ||||||||||||||||||||||||||||||||||||
| 73 | 13 | my ($caller, $data) = $collection->{caller}; | |||||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | while (@options) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | 13 | my $opt = shift @options; | |||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | 13 | ▶ | if ($opt eq 'caller') { $caller = shift @options } # custom caller | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 78 | 13 | ▶ | if ($opt eq 'data') { $data = shift @options } # fire data | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | # boolean option. | ||||||||||||||||||||||||||||||||||||
| 81 | 13 | ▶ | $collection->{$opt} = 1 if $boolopts{$opt}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | # create fire object. | ||||||||||||||||||||||||||||||||||||
| 86 | 13 | ▶ | my $fire = Evented::Object::EventFire->new( | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$caller ||= [caller 1]
MC/DC100%1/1
$caller ||= [caller 1]
$caller
Truth table100%2/2
A
A= $caller
| |||||||||||||||||||||||||||||||||||||
| 87 | caller => $caller ||= [caller 1], # $fire->caller | ||||||||||||||||||||||||||||||||||||
| 88 | data => $data, # $fire->data | ||||||||||||||||||||||||||||||||||||
| 89 | collection => $collection | ||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | # if it hasn't been sorted, do so now. | ||||||||||||||||||||||||||||||||||||
| 93 | 13 | ▶ | $collection->sort if not $collection->{sorted}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 94 | 13 | ▶ | my $callbacks = $collection->{sorted} or return $fire; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | # if return_check is enabled, add a callback to be fired last that will | ||||||||||||||||||||||||||||||||||||
| 97 | # check the return values. this is basically hackery using a dummy object. | ||||||||||||||||||||||||||||||||||||
| 98 | 13 | ▶ | if ($collection->{return_check}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $cb = { | |||||||||||||||||||||||||||||||||||
| 100 | name => 'eventedObject.returnCheck', | ||||||||||||||||||||||||||||||||||||
| 101 | caller => $caller, | ||||||||||||||||||||||||||||||||||||
| 102 | code => \&_return_check | ||||||||||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $group = [ | ||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$dummy ||= "Evented::Object"->new
MC/DC0%0/2
$dummy ||= "Evented::Object"->new
$dummy
"Evented::Object"->new
Truth table0%0/3
A ||= B
A= $dummyB= "Evented::Object"->new
| |||||||||||||||||||||||||||||||||||||
| 105 | $dummy ||= Evented::Object->new, | ||||||||||||||||||||||||||||||||||||
| 106 | 'returnCheck', | ||||||||||||||||||||||||||||||||||||
| 107 | [], | ||||||||||||||||||||||||||||||||||||
| 108 | "$dummy/returnCheck" | ||||||||||||||||||||||||||||||||||||
| 109 | ]; | ||||||||||||||||||||||||||||||||||||
| 110 | 0 | push @$callbacks, [ | |||||||||||||||||||||||||||||||||||
| 111 | -inf, # [0] $priority | ||||||||||||||||||||||||||||||||||||
| 112 | $group, # [1] $group | ||||||||||||||||||||||||||||||||||||
| 113 | $cb # [2] $cb | ||||||||||||||||||||||||||||||||||||
| 114 | ]; | ||||||||||||||||||||||||||||||||||||
| 115 | 0 | $cb->{id} = "$$group[3]/$$cb{name}"; | |||||||||||||||||||||||||||||||||||
| 116 | 0 | $collection->{pending}{ $cb->{id} } = $cb; | |||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||
| 119 | # call them. | ||||||||||||||||||||||||||||||||||||
| 120 | 13 | return $collection->_call_callbacks($fire); | |||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | # sorts the callbacks, trying its best to listen to before and after. | ||||||||||||||||||||||||||||||||||||
| 125 | sub sort : method { | ||||||||||||||||||||||||||||||||||||
| 126 | 13 | ▶ | my $collection = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
sort: called CC=13 SCAR=25.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 127 | 13 | ▶ | return unless $collection->{pending}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 128 | 13 | my %callbacks = %{ $collection->{pending} }; | |||||||||||||||||||||||||||||||||||
| 129 | 13 | my (@sorted, %done, %waited); | |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | # iterate over the callback sets, | ||||||||||||||||||||||||||||||||||||
| 132 | # which are array refs of [ priority, group, cb ] | ||||||||||||||||||||||||||||||||||||
| 133 | 13 | my @callbacks = values %callbacks; | |||||||||||||||||||||||||||||||||||
| 134 | 13 | ▶ | while (my $set = shift @callbacks) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 135 | 37 | my ($priority, $group, $cb) = @$set; | |||||||||||||||||||||||||||||||||||
| 136 | 37 | my $cb_id = $cb->{id}; | |||||||||||||||||||||||||||||||||||
| 137 | 37 | my $group_id = $group->[3]; | |||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||
| 139 | 37 | ▶ | next if $done{$cb_id}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | # a real priority exists already. | ||||||||||||||||||||||||||||||||||||
| 142 | 37 | ▶ | if (defined $priority && $priority ne 'nan') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $priority and $priority ne "nan"
MC/DC50%1/2
defined $priority and $priority ne "nan"
defined $priority
$priority ne "nan"
Truth table66%2/3
A and B
A= defined $priorityB= $priority ne "nan"
| |||||||||||||||||||||||||||||||||||||
| 143 | 22 | push @sorted, $set; | |||||||||||||||||||||||||||||||||||
| 144 | 22 | $done{$cb_id} = 1; | |||||||||||||||||||||||||||||||||||
| 145 | 22 | next; | |||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | # TODO: if before and afters cannot be resolved, the callback dependencies | ||||||||||||||||||||||||||||||||||||
| 150 | # are currently skipped. maybe there should be a way to specify that a callback | ||||||||||||||||||||||||||||||||||||
| 151 | # dependency is REQUIRED, meaning to skip the callback entirely if it cannot | ||||||||||||||||||||||||||||||||||||
| 152 | # be done. or maybe something more sophisticated that can prioritize the | ||||||||||||||||||||||||||||||||||||
| 153 | # befores and afters in this way. for now though, we will just try to not | ||||||||||||||||||||||||||||||||||||
| 154 | # specify impossible befores and afters. | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | # callback priority determination can be postponed until another's | ||||||||||||||||||||||||||||||||||||
| 159 | # priority is determined. the maxmium number of times one particular | ||||||||||||||||||||||||||||||||||||
| 160 | # callback can be postponed is the number of total callbacks. | ||||||||||||||||||||||||||||||||||||
| 161 | 15 | my $wait_max = keys %callbacks; | |||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | 15 | my $name_to_id = $collection->_group_names($group_id); | |||||||||||||||||||||||||||||||||||
| 164 | my $get_befores_afters = sub { | ||||||||||||||||||||||||||||||||||||
| 165 | 25 | ▶ | my ($key, @results) = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||
| 166 | 25 | ▶ | my $list = $cb->{$key} or return; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 167 | 19 | ▶ | $list = [ $list ] if ref $list ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | # for each callback name, find its priority. | ||||||||||||||||||||||||||||||||||||
| 170 | 19 | foreach my $their_name (@$list) { | |||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | # map callback name to id, id to cbref, and cbref to priority. | ||||||||||||||||||||||||||||||||||||
| 173 | 29 | ▶ | my $their_id = $name_to_id->{$their_name} or next; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 174 | 17 | ▶ | my $their_cb = $callbacks{$their_id} or next; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 175 | 17 | my $their_p = $their_cb->[0]; | |||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | # if their priority is nan, | ||||||||||||||||||||||||||||||||||||
| 178 | # we have to wait until it is determined. | ||||||||||||||||||||||||||||||||||||
| 179 | 17 | ▶ | if ($their_p eq 'nan') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 180 | 10 | my $wait_key = "$cb_id $their_id"; | |||||||||||||||||||||||||||||||||||
| 181 | push @callbacks, $set | ||||||||||||||||||||||||||||||||||||
| 182 | 10 | ▶ | unless $waited{$key}++ > $wait_max; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 183 | 10 | return 1; | |||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | 7 | push @results, $their_p; | |||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||
| 189 | 9 | return (undef, @results); | |||||||||||||||||||||||||||||||||||
| 190 | 15 | }; | |||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||
| 192 | 15 | ▶ | my ($next, @befores) = $get_befores_afters->('before'); next if $next; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 193 | 10 | ▶ | ($next, my @afters ) = $get_befores_afters->('after'); next if $next; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | # figure the ideal priority. | ||||||||||||||||||||||||||||||||||||
| 196 | 5 | ▶ | if (@befores && @afters) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
@befores and @afters
MC/DC0%0/2
@befores and @afters
@befores
@afters
Truth table66%2/3
A and B
A= @beforesB= @afters
| |||||||||||||||||||||||||||||||||||||
| 197 | 0 | my $a_refpoint = min @afters; | |||||||||||||||||||||||||||||||||||
| 198 | 0 | my $b_refpoint = max @befores; | |||||||||||||||||||||||||||||||||||
| 199 | 0 | $priority = ($a_refpoint + $b_refpoint) / 2; | |||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | # only before. just have 1 higher priority. | ||||||||||||||||||||||||||||||||||||
| 203 | elsif (@befores) { | ||||||||||||||||||||||||||||||||||||
| 204 | 3 | my $refpoint = max @befores; | |||||||||||||||||||||||||||||||||||
| 205 | 3 | $priority = ++$refpoint; | |||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||
| 208 | # only after. | ||||||||||||||||||||||||||||||||||||
| 209 | elsif (@afters) { | ||||||||||||||||||||||||||||||||||||
| 210 | 2 | my $refpoint = min @afters; | |||||||||||||||||||||||||||||||||||
| 211 | 2 | $priority = --$refpoint; | |||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||
| 214 | 5 | ▶ | $priority = 0 if $priority eq 'nan'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | # done with this callback. | ||||||||||||||||||||||||||||||||||||
| 217 | 5 | $set->[0] = $priority; | |||||||||||||||||||||||||||||||||||
| 218 | 5 | push @sorted, $set; | |||||||||||||||||||||||||||||||||||
| 219 | 5 | $done{$cb_id} = 1; | |||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||
| 223 | # the final sort by numerical priority. | ||||||||||||||||||||||||||||||||||||
| 224 | 13 | $collection->{sorted} = [ sort { $b->[0] <=> $a->[0] } @sorted ]; | |||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||
| 228 | # Nov. 22, 2013 revision | ||||||||||||||||||||||||||||||||||||
| 229 | # ---------------------- | ||||||||||||||||||||||||||||||||||||
| 230 | # | ||||||||||||||||||||||||||||||||||||
| 231 | # collection a set of callbacks about to be fired. they might belong to multiple | ||||||||||||||||||||||||||||||||||||
| 232 | # objects or maybe even multiple events. they can each have their own | ||||||||||||||||||||||||||||||||||||
| 233 | # arguments, and they all have their own options, code references, etc. | ||||||||||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||||||||||
| 235 | # group represents the group to which a callback belongs. a group consists of | ||||||||||||||||||||||||||||||||||||
| 236 | # the associated evented object, event name, and arguments. | ||||||||||||||||||||||||||||||||||||
| 237 | # | ||||||||||||||||||||||||||||||||||||
| 238 | # This revision eliminates all of these nested structures by reworking the way | ||||||||||||||||||||||||||||||||||||
| 239 | # a callback collection works. A collection should be an array of callbacks. | ||||||||||||||||||||||||||||||||||||
| 240 | # This array, unlike before, will contain an additional element: an array | ||||||||||||||||||||||||||||||||||||
| 241 | # reference representing the "group." | ||||||||||||||||||||||||||||||||||||
| 242 | # | ||||||||||||||||||||||||||||||||||||
| 243 | # @collection = ( | ||||||||||||||||||||||||||||||||||||
| 244 | # [ $priority, $group, $cb ], | ||||||||||||||||||||||||||||||||||||
| 245 | # [ $priority, $group, $cb ], | ||||||||||||||||||||||||||||||||||||
| 246 | # ... | ||||||||||||||||||||||||||||||||||||
| 247 | # ) | ||||||||||||||||||||||||||||||||||||
| 248 | # | ||||||||||||||||||||||||||||||||||||
| 249 | # $group = $cb = | ||||||||||||||||||||||||||||||||||||
| 250 | # [ $eo, $event_name, $args, $id ] { code, caller, %opts } | ||||||||||||||||||||||||||||||||||||
| 251 | # | ||||||||||||||||||||||||||||||||||||
| 252 | # This format has several major advantages over the former one. Specifically, | ||||||||||||||||||||||||||||||||||||
| 253 | # it makes it very simple to determine which callbacks will be called in the | ||||||||||||||||||||||||||||||||||||
| 254 | # future, which ones have been called already, how many are left, etc. | ||||||||||||||||||||||||||||||||||||
| 255 | # | ||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||
| 257 | # call the passed callback priority sets. | ||||||||||||||||||||||||||||||||||||
| 258 | sub _call_callbacks { | ||||||||||||||||||||||||||||||||||||
| 259 | 13 | ▶ | my ($collection, $fire) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_call_callbacks: called CC=15 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||
| 260 | 13 | my $ef_props = $fire->{$props}; | |||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | # store the collection. | ||||||||||||||||||||||||||||||||||||
| 263 | 13 | ▶ | my $remaining = $collection->{sorted} or return; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 264 | 13 | $ef_props->{collection} = $collection; | |||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||
| 266 | # call each callback. | ||||||||||||||||||||||||||||||||||||
| 267 | 13 | ▶ | while (my $entry = shift @$remaining) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 268 | 26 | my ($priority, $group, $cb) = @$entry; | |||||||||||||||||||||||||||||||||||
| 269 | 26 | my ($eo, $event_name, $args, $group_id) = @$group; | |||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||
| 271 | # sanity check! | ||||||||||||||||||||||||||||||||||||
| 272 | 26 | ▶ | blessed $eo && $eo->isa('Evented::Object') or return; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($eo) and $eo->isa("Evented::Object")
MC/DC0%0/2
builtin::blessed($eo) and $eo->isa("Evented::Object")
builtin::blessed($eo)
$eo->isa("Evented::Object")
Truth table33%1/3
A and B
A= builtin::blessed($eo)B= $eo->isa("Evented::Object")
| |||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||
| 274 | # callback name-to-ID mapping is specific to each group. | ||||||||||||||||||||||||||||||||||||
| 275 | 26 | $ef_props->{callback_ids} = $collection->_group_names($group_id); | |||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||
| 277 | # increment the callback counter. | ||||||||||||||||||||||||||||||||||||
| 278 | 26 | $ef_props->{callback_i}++; | |||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||
| 280 | # set the evented object of this callback. | ||||||||||||||||||||||||||||||||||||
| 281 | # set the event name of this callback. | ||||||||||||||||||||||||||||||||||||
| 282 | 26 | $ef_props->{object} = $eo; weaken($ef_props->{object}); # $fire->object | |||||||||||||||||||||||||||||||||||
| 283 | 26 | $ef_props->{name} = $event_name; # $fire->event_name | |||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||
| 285 | # store identifiers. | ||||||||||||||||||||||||||||||||||||
| 286 | 26 | $ef_props->{callback_id} = my $cb_id = $cb->{id}; | |||||||||||||||||||||||||||||||||||
| 287 | 26 | $ef_props->{group_id} = $group_id; | |||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||
| 289 | # create info about the call. | ||||||||||||||||||||||||||||||||||||
| 290 | 26 | $ef_props->{callback_name} = $cb->{name}; # $fire->callback_name | |||||||||||||||||||||||||||||||||||
| 291 | 26 | $ef_props->{callback_priority} = $priority; # $fire->callback_priority | |||||||||||||||||||||||||||||||||||
| 292 | 26 | ▶ | $ef_props->{callback_data} = $cb->{data} if defined $cb->{data}; # $fire->callback_data | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||
| 294 | # this callback has been called already. | ||||||||||||||||||||||||||||||||||||
| 295 | 26 | ▶ | next if $ef_props->{called}{$cb_id}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | # this callback has probably been cancelled. | ||||||||||||||||||||||||||||||||||||
| 298 | 26 | ▶ | next unless $collection->{pending}{$cb_id}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||
| 301 | # determine arguments. | ||||||||||||||||||||||||||||||||||||
| 302 | # | ||||||||||||||||||||||||||||||||||||
| 303 | # no compat <3.0: used to always have obj unless specified with no_obj or later no_fire_obj. | ||||||||||||||||||||||||||||||||||||
| 304 | # no compat <2.9: with_obj -> eo_obj | ||||||||||||||||||||||||||||||||||||
| 305 | # compat: all later version had a variety of with_obj-like-options below. | ||||||||||||||||||||||||||||||||||||
| 306 | # | ||||||||||||||||||||||||||||||||||||
| 307 | 25 | my @cb_args = @$args; | |||||||||||||||||||||||||||||||||||
| 308 | 25 | my $include_obj = grep $cb->{$_}, qw(with_eo with_obj with_evented_obj eo_obj); | |||||||||||||||||||||||||||||||||||
| 309 | 25 | ▶ | unshift @cb_args, $fire unless $cb->{no_fire_obj}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 310 | 25 | ▶ | unshift @cb_args, $eo if $include_obj; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||
| 312 | # set return values. | ||||||||||||||||||||||||||||||||||||
| 313 | $ef_props->{last_return} = # set last return value. | ||||||||||||||||||||||||||||||||||||
| 314 | $ef_props->{return}{$cb_id} = # set this callback's return value. | ||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||
| 316 | # call the callback with proper arguments. | ||||||||||||||||||||||||||||||||||||
| 317 | 0 | $collection->{safe} ? eval { $cb->{code}(@cb_args) } | |||||||||||||||||||||||||||||||||||
| 318 | 25 | ▶ | : $cb->{code}(@cb_args); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||
| 320 | # set $fire->called($cb) true, and set $fire->last to the callback's name. | ||||||||||||||||||||||||||||||||||||
| 321 | 25 | $ef_props->{called}{$cb_id} = 1; | |||||||||||||||||||||||||||||||||||
| 322 | 25 | $ef_props->{last_callback} = $cb->{name}; | |||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||
| 324 | # no longer pending. | ||||||||||||||||||||||||||||||||||||
| 325 | 25 | delete $collection->{pending}{$cb_id}; | |||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||
| 327 | # stop if eval failed. | ||||||||||||||||||||||||||||||||||||
| 328 | 25 | ▶ | if ($collection->{safe} and my $err = $@) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$collection->{"safe"} and my $err = $@
MC/DC0%0/2
$collection->{"safe"} and my $err = $@
$collection->{"safe"}
my $err = $@
Truth table33%1/3
A and B
A= $collection->{"safe"}B= my $err = $@
| |||||||||||||||||||||||||||||||||||||
| 329 | 0 | chomp $err; | |||||||||||||||||||||||||||||||||||
| 330 | $ef_props->{error}{$cb_id} = # not used for anything | ||||||||||||||||||||||||||||||||||||
| 331 | 0 | $ef_props->{exception} = $err; | |||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | $fire->stop($err) unless $collection->{fail_continue}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||
| 335 | # if stop is true, $fire->stop was called. stop the iteration. | ||||||||||||||||||||||||||||||||||||
| 336 | 25 | ▶ | if ($ef_props->{stop}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 337 | 1 | $ef_props->{stopper} = $cb->{name}; # set $fire->stopper. | |||||||||||||||||||||||||||||||||||
| 338 | 1 | last; | |||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||
| 343 | # dispose of things that are no longer needed. | ||||||||||||||||||||||||||||||||||||
| 344 | 13 | delete @$ef_props{ qw( | |||||||||||||||||||||||||||||||||||
| 345 | callback_name callback_priority | ||||||||||||||||||||||||||||||||||||
| 346 | callback_data callback_i object | ||||||||||||||||||||||||||||||||||||
| 347 | collection | ||||||||||||||||||||||||||||||||||||
| 348 | ) }; | ||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||
| 350 | # return the event object. | ||||||||||||||||||||||||||||||||||||
| 351 | 13 | $ef_props->{complete} = 1; | |||||||||||||||||||||||||||||||||||
| 352 | 13 | return $fire; | |||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||
| 356 | sub _group_names { | ||||||||||||||||||||||||||||||||||||
| 357 | 41 | ▶ | my ($collection, $group_id) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_group_names: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||
| 358 | 41 | ▶ | return $collection->{group_names}{$group_id} ||= do { | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$collection->{"group_names"}{$group_id} ||= do {
my $names_from_group = $collection->{"names"}{$group_id} || {};
my $default_names = $collection->{"default_names"};
my(%names) = (%$default_names, %$names_from_group);
\%names
}
MC/DC0%0/2
$collection->{"group_names"}{$group_id} ||= do {
my $names_from_group = $collection->{"names"}{$group_id} || {};
my $default_names = $collection->{"default_names"};
my(%names) = (%$default_names, %$names_from_group);
\%names
}
$collection->{"group_names"}{$group_id}
do {
my $names_from_group = $collection->{"names"}{$group_id} || {};
my $default_names = $collection->{"default_names"};
my(%names) = (%$default_names, %$names_from_group);
\%names
}
Truth table66%2/3
A ||= B
A= $collection->{"group_names"}{$group_id}B= do {
my $names_from_group = $collection->{"names"}{$group_id} || {};
my $default_names = $collection->{"default_names"};
my(%names) = (%$default_names, %$names_from_group);
\%names
}
| |||||||||||||||||||||||||||||||||||||
| 359 | 14 | ▶ | my $names_from_group = $collection->{names}{$group_id} || {}; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$collection->{'names'}{$group_id} || {}
MC/DC0%0/1
$collection->{'names'}{$group_id} || {}
$collection->{'names'}{$group_id}
Truth table50%1/2
A
A= $collection->{'names'}{$group_id}
| |||||||||||||||||||||||||||||||||||||
| 360 | 14 | my $default_names = $collection->{default_names}; | |||||||||||||||||||||||||||||||||||
| 361 | 14 | my %names = (%$default_names, %$names_from_group); | |||||||||||||||||||||||||||||||||||
| 362 | 14 | \%names | |||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||
| 366 | sub _return_check { | ||||||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | my $fire = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_return_check: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||
| 368 | 0 | ▶ | my %returns = %{ $fire->{$props}{return} || {} }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 369 | 0 | foreach my $cb_id (keys %returns) { | |||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | next if $returns{$cb_id}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 371 | 0 | return $fire->stop("$cb_id returned false with return_check enabled"); | |||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||
| 373 | 0 | return 1; | |||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||
| 376 | 13 | 1; | |||||||||||||||||||||||||||||||||||