| 1 | # Copyright (c) 2011-17, Mitchell Cooper | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Evented::Object: a simple yet featureful base class event framework. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Evented::Object is based on the libuic UIC::Evented::Object: | ||||||||||||||||||||||||||||
| 6 | # ... which is based on Evented::Object from foxy-java IRC Bot, | ||||||||||||||||||||||||||||
| 7 | # ... which is based on Evented::Object from Arinity IRC Services, | ||||||||||||||||||||||||||||
| 8 | # ... which is based on Evented::Object from ntirc IRC Client, | ||||||||||||||||||||||||||||
| 9 | # ... which is based on IRC::Evented::Object from libirc IRC Library. | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # Evented::Object and its very detailed documentation can be found | ||||||||||||||||||||||||||||
| 12 | # in their latest versions at https://github.com/cooper/evented-object. | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | package Evented::Object; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 13 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 13 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # these must be set before loading EventFire. | ||||||||||||||||||||||||||||
| 22 | 13 | our ($events, $props, %monitors); | |||||||||||||||||||||||||||
| 23 | BEGIN { | ||||||||||||||||||||||||||||
| 24 | 13 | ▶ | $events = 'eventedObject.events'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 13 | $props = 'eventedObject.props'; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 13 | ▶ | use Scalar::Util qw(weaken blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 13 | ▶ | use Evented::Object::EventFire; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 13 | ▶ | use Evented::Object::Collection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # always use 2 decimals. change other packages too. | ||||||||||||||||||||||||||||
| 33 | 13 | our $VERSION = '5.63'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # creates a new evented object. | ||||||||||||||||||||||||||||
| 36 | sub new { | ||||||||||||||||||||||||||||
| 37 | 15 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 15 | bless \%opts, $class; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | ############################# | ||||||||||||||||||||||||||||
| 42 | ### REGISTERING CALLBACKS ### | ||||||||||||||||||||||||||||
| 43 | ############################# | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # ->register_callback() | ||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | # aliases: ->register_event(), ->on() | ||||||||||||||||||||||||||||
| 48 | # attaches an event callback. | ||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||
| 50 | # $eo->register_callback(myEvent => sub { | ||||||||||||||||||||||||||||
| 51 | # ... | ||||||||||||||||||||||||||||
| 52 | # }, 'some.callback.name', priority => 200); | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | sub register_callback { | ||||||||||||||||||||||||||||
| 55 | 35 | ▶ | my ($eo, $event_name, $code, @opts_) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register_callback: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # if there is an odd number of options, the first is the callback name. | ||||||||||||||||||||||||||||
| 58 | # this also implies with_eo. | ||||||||||||||||||||||||||||
| 59 | 35 | my %opts; | |||||||||||||||||||||||||||
| 60 | 35 | ▶ | if (@opts_ % 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | %opts = ( | |||||||||||||||||||||||||||
| 62 | name => shift @opts_, | ||||||||||||||||||||||||||||
| 63 | with_eo => 1, | ||||||||||||||||||||||||||||
| 64 | @opts_ | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||
| 68 | 35 | %opts = @opts_; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # no name was provided, so we shall construct one using pure hackery. | ||||||||||||||||||||||||||||
| 72 | # this is one of the most criminal things I've ever done. | ||||||||||||||||||||||||||||
| 73 | 35 | my @caller = caller; | |||||||||||||||||||||||||||
| 74 | 35 | ▶ | if (!defined $opts{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 21 | state $c = -1; $c++; | |||||||||||||||||||||||||||
| 76 | 21 | $opts{name} = "$event_name:$caller[0]($caller[2],$c)"; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # determine the event store. | ||||||||||||||||||||||||||||
| 80 | 35 | my $event_store = _event_store($eo); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # before/after a callback. | ||||||||||||||||||||||||||||
| 83 | 35 | ▶ | my $priority = delete $opts{priority} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{'priority'} || 0
MC/DC100%1/1
delete $opts{'priority'} || 0
delete $opts{'priority'}
Truth table100%2/2
A
A= delete $opts{'priority'}
| |||||||||||||||||||||||||||||
| 84 | 35 | ▶ | if (defined $opts{before} or defined $opts{after}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opts{"before"} or defined $opts{"after"}
MC/DC100%2/2
defined $opts{"before"} or defined $opts{"after"}
defined $opts{"before"}
defined $opts{"after"}
Truth table100%3/3
A or B
A= defined $opts{"before"}B= defined $opts{"after"}
| |||||||||||||||||||||||||||||
| 85 | 7 | $priority = 'nan'; | |||||||||||||||||||||||||||
| 86 | # nan priority indicates it should be determined at a later time. | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # add the callback. | ||||||||||||||||||||||||||||
| 90 | 35 | ▶ | my $callbacks = $event_store->{$event_name}{$priority} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$event_store->{$event_name}{$priority} ||= []
MC/DC100%1/1
$event_store->{$event_name}{$priority} ||= []
$event_store->{$event_name}{$priority}
Truth table100%2/2
A
A= $event_store->{$event_name}{$priority}
| |||||||||||||||||||||||||||||
| 91 | 35 | push @$callbacks, my $cb = { | |||||||||||||||||||||||||||
| 92 | %opts, | ||||||||||||||||||||||||||||
| 93 | code => $code, | ||||||||||||||||||||||||||||
| 94 | caller => \@caller | ||||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # tell class monitor. | ||||||||||||||||||||||||||||
| 98 | _monitor_fire( | ||||||||||||||||||||||||||||
| 99 | 35 | ▶ | $opts{_caller} // $caller[0], | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts{"_caller"} // $caller[0]
MC/DC0%0/2
$opts{"_caller"} // $caller[0]
$opts{"_caller"}
$caller[0]
Truth table33%1/3
A // B
A= $opts{"_caller"}B= $caller[0]
| |||||||||||||||||||||||||||||
| 100 | register_callback => $eo, $event_name, $cb | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 35 | return $cb; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # ->register_callbacks() | ||||||||||||||||||||||||||||
| 107 | # | ||||||||||||||||||||||||||||
| 108 | # attaches several event callbacks at once. | ||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||
| 110 | sub register_callbacks { | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $eo = shift; | ||||||||||||||||||||||||||
|
Subroutine
register_callbacks: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | return map { $eo->register_callback(%$_, _caller => caller) } @_; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | ########################## | ||||||||||||||||||||||||||||
| 116 | ### DELETING CALLBACKS ### | ||||||||||||||||||||||||||||
| 117 | ########################## | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # ->delete_callback(event_name => 'callback.name') | ||||||||||||||||||||||||||||
| 120 | # ->delete_event('event_name') | ||||||||||||||||||||||||||||
| 121 | # | ||||||||||||||||||||||||||||
| 122 | # deletes an event callback or all callbacks of an event. | ||||||||||||||||||||||||||||
| 123 | # returns a true value if any events were deleted, false otherwise. | ||||||||||||||||||||||||||||
| 124 | # more specifically, it returns the number of callbacks deleted. | ||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | sub delete_callback { | ||||||||||||||||||||||||||||
| 127 | 16 | ▶ | my ($eo, $event_name, $name, $caller) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete_callback: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 16 | ▶ | my @caller = $caller && ref $caller eq 'ARRAY' ? @$caller : caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$caller and ref $caller eq "ARRAY"
MC/DC0%0/2
$caller and ref $caller eq "ARRAY"
$caller
ref $caller eq "ARRAY"
Truth table33%1/3
A and B
A= $callerB= ref $caller eq "ARRAY"
| |||||||||||||||||||||||||||||
| 129 | 16 | my $amount = 0; | |||||||||||||||||||||||||||
| 130 | 16 | my $event_store = _event_store($eo); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # event does not have any callbacks. | ||||||||||||||||||||||||||||
| 133 | 16 | ▶ | return 0 unless $event_store->{$event_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # if no callback is specified, delete all events of this type. | ||||||||||||||||||||||||||||
| 136 | 16 | ▶ | if (!$name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 15 | $amount = scalar keys %{ $event_store->{$event_name} }; | |||||||||||||||||||||||||||
| 138 | 15 | delete $event_store->{$event_name}; | |||||||||||||||||||||||||||
| 139 | 15 | _monitor_fire($caller[0], delete_event => $eo, $event_name); | |||||||||||||||||||||||||||
| 140 | 15 | return $amount; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # iterate through callbacks and delete matches. | ||||||||||||||||||||||||||||
| 144 | 1 | PRIORITY: foreach my $priority (keys %{ $event_store->{$event_name} }) { | |||||||||||||||||||||||||||
| 145 | 2 | my $callbacks = $event_store->{$event_name}{$priority}; | |||||||||||||||||||||||||||
| 146 | 2 | my @goodbacks; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | CALLBACK: foreach my $cb (@$callbacks) { | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # don't want this one. | ||||||||||||||||||||||||||||
| 151 | 2 | ▶ | if (ref $cb ne 'HASH' || $cb->{name} eq $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $cb ne "HASH" or $cb->{"name"} eq $name
MC/DC50%1/2
ref $cb ne "HASH" or $cb->{"name"} eq $name
ref $cb ne "HASH"
$cb->{"name"} eq $name
Truth table66%2/3
A or B
A= ref $cb ne "HASH"B= $cb->{"name"} eq $name
| |||||||||||||||||||||||||||||
| 152 | 1 | $amount++; | |||||||||||||||||||||||||||
| 153 | 1 | next CALLBACK; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | push @goodbacks, $cb; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # no callbacks left in this priority. | ||||||||||||||||||||||||||||
| 160 | 2 | ▶ | if (!scalar @goodbacks) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 1 | delete $event_store->{$event_name}{$priority}; | |||||||||||||||||||||||||||
| 162 | 1 | next PRIORITY; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # keep these callbacks. | ||||||||||||||||||||||||||||
| 166 | 1 | @$callbacks = @goodbacks; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 1 | return $amount; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # ->delete_all_events() | ||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||
| 175 | # deletes all the callbacks of EVERY event. | ||||||||||||||||||||||||||||
| 176 | # useful when you're done with an object to ensure any possible self-referencing | ||||||||||||||||||||||||||||
| 177 | # callbacks are properly destroyed for garbage collection to work. | ||||||||||||||||||||||||||||
| 178 | # | ||||||||||||||||||||||||||||
| 179 | sub delete_all_events { | ||||||||||||||||||||||||||||
| 180 | 15 | ▶ | my ($eo, $amount) = (shift, 0); | ||||||||||||||||||||||||||
|
Subroutine
delete_all_events: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | 15 | ▶ | my $event_store = _event_store($eo) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 15 | ▶ | ref $event_store eq 'HASH' or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # delete one-by-one. | ||||||||||||||||||||||||||||
| 185 | # we can't simply set an empty list because monitor events must be fired. | ||||||||||||||||||||||||||||
| 186 | 15 | foreach my $event_name (keys %$event_store) { | |||||||||||||||||||||||||||
| 187 | 14 | $eo->delete_event($event_name); | |||||||||||||||||||||||||||
| 188 | 14 | $amount++; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # just clear it to be safe. | ||||||||||||||||||||||||||||
| 192 | 15 | %$event_store = (); | |||||||||||||||||||||||||||
| 193 | 15 | delete $eo->{$events}; | |||||||||||||||||||||||||||
| 194 | 15 | delete $eo->{$props}; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 15 | return $amount; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | ######################## | ||||||||||||||||||||||||||||
| 200 | ### PREPARING EVENTS ### | ||||||||||||||||||||||||||||
| 201 | ######################## | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # ->prepare() | ||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||
| 205 | # automatically guesses whether to use | ||||||||||||||||||||||||||||
| 206 | # ->prepare_event() or ->prepare_together(). | ||||||||||||||||||||||||||||
| 207 | # | ||||||||||||||||||||||||||||
| 208 | sub prepare { | ||||||||||||||||||||||||||||
| 209 | 2 | ▶ | my ($eo_maybe, $eo) = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=5 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 2 | ▶ | $eo = shift if blessed $eo_maybe && $eo_maybe->isa(__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($eo_maybe) and $eo_maybe->isa(__PACKAGE__)
MC/DC0%0/2
builtin::blessed($eo_maybe) and $eo_maybe->isa(__PACKAGE__)
builtin::blessed($eo_maybe)
$eo_maybe->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($eo_maybe)B= $eo_maybe->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 211 | 2 | ▶ | if (ref $_[0] && ref $_[0] eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[0] and ref $_[0] eq "ARRAY"
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "ARRAY"
ref $_[0]
ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 212 | 0 | return $eo->prepare_together(@_); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 2 | return $eo->prepare_event(@_); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # ->prepare_event() | ||||||||||||||||||||||||||||
| 218 | # | ||||||||||||||||||||||||||||
| 219 | # prepares a single event fire by creating a callback collection. | ||||||||||||||||||||||||||||
| 220 | # returns the collection. | ||||||||||||||||||||||||||||
| 221 | # | ||||||||||||||||||||||||||||
| 222 | sub prepare_event { | ||||||||||||||||||||||||||||
| 223 | 13 | ▶ | my ($eo, $event_name, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prepare_event: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 224 | 13 | return $eo->prepare_together([ $event_name, @args ]); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # ->prepare_together() | ||||||||||||||||||||||||||||
| 228 | # | ||||||||||||||||||||||||||||
| 229 | # prepares several events fire by creating a callback collection. | ||||||||||||||||||||||||||||
| 230 | # returns the collection. | ||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||
| 232 | sub prepare_together { | ||||||||||||||||||||||||||||
| 233 | 13 | ▶ | my $obj; | ||||||||||||||||||||||||||
|
Subroutine
prepare_together: called CC=8 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 234 | 13 | my $collection = Evented::Object::Collection->new; | |||||||||||||||||||||||||||
| 235 | 13 | foreach my $set (@_) { | |||||||||||||||||||||||||||
| 236 | 26 | my $eo; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # called with evented object. | ||||||||||||||||||||||||||||
| 239 | 26 | ▶ | if (blessed $set) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 13 | ▶ | $set->isa(__PACKAGE__) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 13 | $obj = $set; | |||||||||||||||||||||||||||
| 242 | 13 | next; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # okay, it's an array ref of | ||||||||||||||||||||||||||||
| 246 | # [ $eo (optional), $event_name => @args ] | ||||||||||||||||||||||||||||
| 247 | 13 | ▶ | ref $set eq 'ARRAY' or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 13 | my ($eo_maybe, $event_name, @args); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # was an object specified? | ||||||||||||||||||||||||||||
| 251 | 13 | $eo_maybe = shift @$set; | |||||||||||||||||||||||||||
| 252 | 13 | ▶ | if (blessed $eo_maybe && $eo_maybe->isa(__PACKAGE__)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($eo_maybe) and $eo_maybe->isa(__PACKAGE__)
MC/DC0%0/2
builtin::blessed($eo_maybe) and $eo_maybe->isa(__PACKAGE__)
builtin::blessed($eo_maybe)
$eo_maybe->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($eo_maybe)B= $eo_maybe->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 253 | 0 | $eo = $eo_maybe; | |||||||||||||||||||||||||||
| 254 | 0 | ($event_name, @args) = @$set; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # no object; fall back to $obj. | ||||||||||||||||||||||||||||
| 258 | else { | ||||||||||||||||||||||||||||
| 259 | 13 | ▶ | $eo = $obj or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 13 | ($event_name, @args) = ($eo_maybe, @$set); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # add to the collection. | ||||||||||||||||||||||||||||
| 264 | 13 | my ($callbacks, $names) = | |||||||||||||||||||||||||||
| 265 | _get_callbacks($eo, $event_name, @args); | ||||||||||||||||||||||||||||
| 266 | 13 | $collection->push_callbacks($callbacks, $names); | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 13 | return $collection; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | ##################### | ||||||||||||||||||||||||||||
| 274 | ### FIRING EVENTS ### | ||||||||||||||||||||||||||||
| 275 | ##################### | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # ->fire_event() | ||||||||||||||||||||||||||||
| 278 | # | ||||||||||||||||||||||||||||
| 279 | # prepares an event and then fires it. | ||||||||||||||||||||||||||||
| 280 | # | ||||||||||||||||||||||||||||
| 281 | sub fire_event { | ||||||||||||||||||||||||||||
| 282 | 11 | ▶ | shift->prepare_event(shift, @_)->fire(caller => [caller 1]); | ||||||||||||||||||||||||||
|
Subroutine
fire_event: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # ->fire_events_together() | ||||||||||||||||||||||||||||
| 286 | # fire_events_together() | ||||||||||||||||||||||||||||
| 287 | # | ||||||||||||||||||||||||||||
| 288 | # prepares several events and then fires them together. | ||||||||||||||||||||||||||||
| 289 | # | ||||||||||||||||||||||||||||
| 290 | sub fire_events_together { | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | prepare_together(@_)->fire(caller => [caller 1]); | ||||||||||||||||||||||||||
|
Subroutine
fire_events_together: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # ->fire_once() | ||||||||||||||||||||||||||||
| 295 | # | ||||||||||||||||||||||||||||
| 296 | # prepares an event, fires it, and deletes all callbacks afterward. | ||||||||||||||||||||||||||||
| 297 | # | ||||||||||||||||||||||||||||
| 298 | sub fire_once { | ||||||||||||||||||||||||||||
| 299 | 0 | ▶ | my ($eo, $event_name, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fire_once: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # fire with this caller. | ||||||||||||||||||||||||||||
| 302 | 0 | my $fire = $eo->prepare_event($event_name, @args)->fire( | |||||||||||||||||||||||||||
| 303 | caller => [caller 1] | ||||||||||||||||||||||||||||
| 304 | ); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # delete the event. | ||||||||||||||||||||||||||||
| 307 | 0 | $eo->delete_event($event_name); | |||||||||||||||||||||||||||
| 308 | 0 | return $fire; | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | ######################## | ||||||||||||||||||||||||||||
| 313 | ### LISTENER OBJECTS ### | ||||||||||||||||||||||||||||
| 314 | ######################## | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # ->add_listener() | ||||||||||||||||||||||||||||
| 317 | # | ||||||||||||||||||||||||||||
| 318 | # adds an object as a listener of another object's events. | ||||||||||||||||||||||||||||
| 319 | # see "listeners" in the documentation. | ||||||||||||||||||||||||||||
| 320 | # | ||||||||||||||||||||||||||||
| 321 | sub add_listener { | ||||||||||||||||||||||||||||
| 322 | 3 | ▶ | my ($eo, $obj, $prefix) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_listener: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | # find listeners list. | ||||||||||||||||||||||||||||
| 325 | 3 | ▶ | my $listeners = $eo->{$props}{listeners} ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$eo->{$Evented::Object::props}{"listeners"} ||= []
MC/DC0%0/1
$eo->{$Evented::Object::props}{"listeners"} ||= []
$eo->{$Evented::Object::props}{"listeners"}
Truth table50%1/2
A
A= $eo->{$Evented::Object::props}{"listeners"}
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | # store this listener. | ||||||||||||||||||||||||||||
| 328 | 3 | push @$listeners, [$prefix, $obj]; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | # weaken the reference to the listener. | ||||||||||||||||||||||||||||
| 331 | 3 | weaken($listeners->[$#$listeners][1]); | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 3 | return 1; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | # ->delete_listener() | ||||||||||||||||||||||||||||
| 337 | # | ||||||||||||||||||||||||||||
| 338 | # removes an object which was listening to another object's events. | ||||||||||||||||||||||||||||
| 339 | # see "listeners" in the documentation. | ||||||||||||||||||||||||||||
| 340 | # | ||||||||||||||||||||||||||||
| 341 | sub delete_listener { | ||||||||||||||||||||||||||||
| 342 | 0 | ▶ | my ($eo, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete_listener: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 343 | 0 | ▶ | return 1 unless my $listeners = $eo->{$props}{listeners}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | @$listeners = grep { | ||||||||||||||||||||||||||||
| 345 | 0 | ▶ | ref $_->[1] eq 'ARRAY' and $_->[1] != $obj | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | } @$listeners; | ||||||||||||||||||||||||||||
| 347 | 0 | return 1; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | ###################### | ||||||||||||||||||||||||||||
| 351 | ### CLASS MONITORS ### | ||||||||||||||||||||||||||||
| 352 | ###################### | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # for objective use $eo->monitor_events($pkg) | ||||||||||||||||||||||||||||
| 355 | 0 | ▶ | sub monitor_events { add_class_monitor(reverse @_) } | ||||||||||||||||||||||||||
|
Subroutine
monitor_events: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 356 | 0 | ▶ | sub stop_monitoring { delete_class_monitor(reverse @_) } | ||||||||||||||||||||||||||
|
Subroutine
stop_monitoring: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # add_class_monitor() | ||||||||||||||||||||||||||||
| 359 | # | ||||||||||||||||||||||||||||
| 360 | # set the monitor object of a class. | ||||||||||||||||||||||||||||
| 361 | # | ||||||||||||||||||||||||||||
| 362 | # TODO: honestly class monitors need to track individual callbacks so that the | ||||||||||||||||||||||||||||
| 363 | # monitor is notified of all deletes of callbacks added by the class being | ||||||||||||||||||||||||||||
| 364 | # monitored even if the delete action was not committed by that package. | ||||||||||||||||||||||||||||
| 365 | # | ||||||||||||||||||||||||||||
| 366 | sub add_class_monitor { | ||||||||||||||||||||||||||||
| 367 | 0 | ▶ | my ($pkg, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_class_monitor: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | # ensure it's an evented object. | ||||||||||||||||||||||||||||
| 370 | 0 | ▶ | return unless $obj->isa(__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # it's already in the list. | ||||||||||||||||||||||||||||
| 373 | 0 | ▶ | my $m = $monitors{$pkg} ||= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$Evented::Object::monitors{$pkg} ||= []
MC/DC0%0/1
$Evented::Object::monitors{$pkg} ||= []
$Evented::Object::monitors{$pkg}
Truth table0%0/2
A
A= $Evented::Object::monitors{$pkg}
| |||||||||||||||||||||||||||||
| 374 | 0 | ▶ | return if grep { $_ == $obj } @$m = grep { defined } @$m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # hold a weak reference to the monitor. | ||||||||||||||||||||||||||||
| 377 | 0 | push @$m, $obj; | |||||||||||||||||||||||||||
| 378 | 0 | weaken($monitors{$pkg}[$#$m]); | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | return 1; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | # delete_class_monitor() | ||||||||||||||||||||||||||||
| 384 | # | ||||||||||||||||||||||||||||
| 385 | # remove a class monitor object from a class. | ||||||||||||||||||||||||||||
| 386 | # | ||||||||||||||||||||||||||||
| 387 | sub delete_class_monitor { | ||||||||||||||||||||||||||||
| 388 | 0 | ▶ | my ($pkg, $obj) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete_class_monitor: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 389 | 0 | ▶ | my $m = $monitors{$pkg} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | @$m = grep { defined && $_ != $obj } @$m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | ####################### | ||||||||||||||||||||||||||||
| 394 | ### CLASS FUNCTIONS ### | ||||||||||||||||||||||||||||
| 395 | ####################### | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # safe_fire($obj, event => ...) | ||||||||||||||||||||||||||||
| 398 | # | ||||||||||||||||||||||||||||
| 399 | # checks that an object is blessed and that it is an evented object. | ||||||||||||||||||||||||||||
| 400 | # if so, prepares and fires an event with optional arguments. | ||||||||||||||||||||||||||||
| 401 | # | ||||||||||||||||||||||||||||
| 402 | sub safe_fire { | ||||||||||||||||||||||||||||
| 403 | 0 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||
|
Subroutine
safe_fire: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 404 | 0 | ▶ | return if !blessed $obj || !$obj->isa(__PACKAGE__); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($obj) and $obj->isa(__PACKAGE__)
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa(__PACKAGE__)
builtin::blessed($obj)
$obj->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($obj)B= $obj->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 405 | 0 | return $obj->fire_event(@_); | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | ######################### | ||||||||||||||||||||||||||||
| 409 | ### INTERNAL ROUTINES ### | ||||||||||||||||||||||||||||
| 410 | ######################### | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | # access package storage. | ||||||||||||||||||||||||||||
| 413 | sub _package_store { | ||||||||||||||||||||||||||||
| 414 | 13 | ▶ | my $package = shift; | ||||||||||||||||||||||||||
|
Subroutine
_package_store: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 415 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 416 | 13 | my $ref = "${package}::__EO__"; | |||||||||||||||||||||||||||
| 417 | 13 | ▶ | if (!keys %$ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | 12 | %$ref = (); | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | 13 | return *$ref{HASH}; | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | # fetch the event store of object or package. | ||||||||||||||||||||||||||||
| 424 | sub _event_store { | ||||||||||||||||||||||||||||
| 425 | 79 | ▶ | my $eo = shift; | ||||||||||||||||||||||||||
|
Subroutine
_event_store: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 426 | 79 | ▶ | return $eo->{$events} ||= {} if blessed $eo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$eo->{$Evented::Object::events} ||= {}
MC/DC100%1/1
$eo->{$Evented::Object::events} ||= {}
$eo->{$Evented::Object::events}
Truth table100%2/2
A
A= $eo->{$Evented::Object::events}
| |||||||||||||||||||||||||||||
| 427 | 13 | my $store = _package_store($eo); | |||||||||||||||||||||||||||
| 428 | 13 | ▶ | return $store->{events} ||= {} if not blessed $eo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$store->{"events"} ||= {}
MC/DC100%1/1
$store->{"events"} ||= {}
$store->{"events"}
Truth table100%2/2
A
A= $store->{"events"}
| |||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | # fetch the property store of object or package. | ||||||||||||||||||||||||||||
| 432 | sub _prop_store { | ||||||||||||||||||||||||||||
| 433 | 0 | ▶ | my $eo = shift; | ||||||||||||||||||||||||||
|
Subroutine
_prop_store: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 434 | 0 | ▶ | return $eo->{$props} ||= {} if blessed $eo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$eo->{$props} ||= {}
MC/DC0%0/1
$eo->{$props} ||= {}
$eo->{$props}
Truth table0%0/2
A
A= $eo->{$props}
| |||||||||||||||||||||||||||||
| 435 | 0 | my $store = _package_store($eo); | |||||||||||||||||||||||||||
| 436 | 0 | ▶ | return $store->{props} ||= {} if not blessed $eo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$store->{"props"} ||= {}
MC/DC0%0/1
$store->{"props"} ||= {}
$store->{"props"}
Truth table0%0/2
A
A= $store->{"props"}
| |||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # fetch a callback from its name. | ||||||||||||||||||||||||||||
| 440 | sub _get_callback_named { | ||||||||||||||||||||||||||||
| 441 | 0 | ▶ | my ($eo, $event_name, $callback_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_callback_named: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 442 | 0 | foreach my $callback (@{ _get_callbacks($eo, $event_name) }) { | |||||||||||||||||||||||||||
| 443 | 0 | ▶ | return $callback if $callback->[2]{name} eq $callback_name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | 0 | return; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | # fetches callbacks of an event. | ||||||||||||||||||||||||||||
| 449 | # internal use only. | ||||||||||||||||||||||||||||
| 450 | sub _get_callbacks { | ||||||||||||||||||||||||||||
| 451 | 13 | ▶ | my ($eo, $event_name, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_callbacks: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||
| 452 | 13 | my (%callbacks, %callback_names); | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # start out with two stores: the object and the package. | ||||||||||||||||||||||||||||
| 455 | my @stores = ( | ||||||||||||||||||||||||||||
| 456 | 13 | [ $event_name => $eo->{$events} ], | |||||||||||||||||||||||||||
| 457 | [ $event_name => _event_store(blessed $eo) ] | ||||||||||||||||||||||||||||
| 458 | ); | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | # if there are any listening objects, add those stores. | ||||||||||||||||||||||||||||
| 462 | 13 | ▶ | if (my $listeners = $eo->{$props}{listeners}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | 3 | my @delete; | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 3 | LISTENER: foreach my $i (0 .. $#$listeners) { | |||||||||||||||||||||||||||
| 466 | 3 | ▶ | my $l = $listeners->[$i] or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 467 | 3 | my ($prefix, $lis) = @$l; | |||||||||||||||||||||||||||
| 468 | 3 | my $listener_event_name = $prefix.q(.).$event_name; | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | # object has been deallocated by garbage disposal, | ||||||||||||||||||||||||||||
| 471 | # so we can delete this listener. | ||||||||||||||||||||||||||||
| 472 | 3 | ▶ | if (!$lis) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 473 | 0 | push @delete, $i; | |||||||||||||||||||||||||||
| 474 | 0 | next LISTENER; | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 3 | push @stores, [ $listener_event_name => $lis->{$events} ]; | |||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | # delete listeners if necessary. | ||||||||||||||||||||||||||||
| 483 | 3 | splice @$listeners, $_, 1 foreach @delete; | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | # add callbacks from each store. | ||||||||||||||||||||||||||||
| 488 | 13 | foreach my $st (@stores) { | |||||||||||||||||||||||||||
| 489 | 29 | my ($event_name, $event_store) = @$st; | |||||||||||||||||||||||||||
| 490 | 29 | ▶ | my $store = $event_store->{$event_name} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 15 | foreach my $priority (keys %$store) { | |||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | # create a group reference. | ||||||||||||||||||||||||||||
| 494 | 24 | my $group_id = "$eo/$event_name"; | |||||||||||||||||||||||||||
| 495 | 24 | my $group = [ $eo, $event_name, \@args, $group_id ]; | |||||||||||||||||||||||||||
| 496 | 24 | weaken($group->[0]); | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | # add each callback set. inject callback name. | ||||||||||||||||||||||||||||
| 499 | 24 | foreach my $cb_ref (@{ $store->{$priority} }) { | |||||||||||||||||||||||||||
| 500 | 29 | my %cb = %$cb_ref; # make a copy | |||||||||||||||||||||||||||
| 501 | 29 | $cb{id} = "$group_id/$cb{name}"; | |||||||||||||||||||||||||||
| 502 | 29 | $callbacks{ $cb{id} } = [ $priority, $group, \%cb ]; | |||||||||||||||||||||||||||
| 503 | 29 | $callback_names{$group_id}{ $cb{name} } = $cb{id}; | |||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | 13 | ▶ | return wantarray ? (\%callbacks, \%callback_names) : \%callbacks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | # fire a class monitor event. | ||||||||||||||||||||||||||||
| 513 | sub _monitor_fire { | ||||||||||||||||||||||||||||
| 514 | 50 | ▶ | my ($pkg, $event_name, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_monitor_fire: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||
| 515 | 50 | ▶ | my $m = $monitors{$pkg} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 516 | 0 | safe_fire($_, "monitor:$event_name" => @args) foreach @$m; | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 14 | ▶ | sub DESTROY { shift->delete_all_events } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | ############### | ||||||||||||||||||||||||||||
| 522 | ### ALIASES ### | ||||||||||||||||||||||||||||
| 523 | ############### | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | sub register_event; | ||||||||||||||||||||||||||||
| 526 | sub register_events; | ||||||||||||||||||||||||||||
| 527 | sub delete_event; | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | sub on; | ||||||||||||||||||||||||||||
| 530 | sub del; | ||||||||||||||||||||||||||||
| 531 | sub fire; | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | BEGIN { | ||||||||||||||||||||||||||||
| 534 | 13 | ▶ | *register_event = *register_callback; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 535 | 13 | *register_events = *register_callbacks; | |||||||||||||||||||||||||||
| 536 | 13 | *delete_event = *delete_callback; | |||||||||||||||||||||||||||
| 537 | 13 | *on = *register_callback; | |||||||||||||||||||||||||||
| 538 | 13 | *del = *delete_callback; | |||||||||||||||||||||||||||
| 539 | 13 | *fire = *fire_event; | |||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 13 | 1; | |||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =head1 NAME | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | B<Evented::Object> - a base class which allows you to attach callbacks to | ||||||||||||||||||||||||||||
| 547 | objects and then fire events on them. | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | package Person; | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | use warnings; | ||||||||||||||||||||||||||||
| 554 | use strict; | ||||||||||||||||||||||||||||
| 555 | use 5.010; | ||||||||||||||||||||||||||||
| 556 | use parent 'Evented::Object'; | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | use Evented::Object; | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | # Creates a new person object. This is nothing special. | ||||||||||||||||||||||||||||
| 561 | # Evented::Object does not require any specific constructor to be called. | ||||||||||||||||||||||||||||
| 562 | sub new { | ||||||||||||||||||||||||||||
| 563 | my ($class, %opts) = @_; | ||||||||||||||||||||||||||||
| 564 | bless \%opts, $class; | ||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # Fires birthday event and increments age. | ||||||||||||||||||||||||||||
| 568 | sub have_birthday { | ||||||||||||||||||||||||||||
| 569 | my $person = shift; | ||||||||||||||||||||||||||||
| 570 | $person->fire(birthday => ++$person->{age}); | ||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | In some other package... | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | package main; | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | # Create a person named Jake at age 19. | ||||||||||||||||||||||||||||
| 578 | my $jake = Person->new(name => 'Jake', age => 19); | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | # Add an event callback that assumes Jake is under 21. | ||||||||||||||||||||||||||||
| 581 | $jake->on(birthday => sub { | ||||||||||||||||||||||||||||
| 582 | my ($fire, $new_age) = @_; | ||||||||||||||||||||||||||||
| 583 | say 'not quite 21 yet...'; | ||||||||||||||||||||||||||||
| 584 | }, name => '21-soon'); | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | # Add an event callback that checks if Jake is 21 | ||||||||||||||||||||||||||||
| 587 | # and cancels the above callback if so. | ||||||||||||||||||||||||||||
| 588 | $jake->on(birthday => sub { | ||||||||||||||||||||||||||||
| 589 | my ($fire, $new_age) = @_; | ||||||||||||||||||||||||||||
| 590 | if ($new_age == 21) { | ||||||||||||||||||||||||||||
| 591 | say 'time to get drunk!'; | ||||||||||||||||||||||||||||
| 592 | $fire->cancel('21-soon'); | ||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||
| 594 | }, name => 'finally-21', priority => 1); | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | # Jake has two birthdays. | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | # Jake's 20th birthday. | ||||||||||||||||||||||||||||
| 599 | $jake->have_birthday; | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | # Jake's 21st birthday. | ||||||||||||||||||||||||||||
| 602 | $jake->have_birthday; | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | # Because 21-soon has a lower priority than finally-21, | ||||||||||||||||||||||||||||
| 605 | # finally-21 will cancel 21-soon if Jake is 21. | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | # The result: | ||||||||||||||||||||||||||||
| 608 | # | ||||||||||||||||||||||||||||
| 609 | # not quite 21 yet... | ||||||||||||||||||||||||||||
| 610 | # time to get drunk! | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | B<I doubt your objects have ever been this evented in your entire life.> | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | Evented::Object supplies an (obviously objective) interface to store and manage | ||||||||||||||||||||||||||||
| 617 | callbacks for events, fire events upon objects, and more. | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | Evented::Object allows you to attach event callbacks to an object | ||||||||||||||||||||||||||||
| 620 | (i.e., a blessed hash reference) and then fire events on that object. Event | ||||||||||||||||||||||||||||
| 621 | fires are much like method calls, except that there can be many responders. | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | Whereas many event systems involve globally unique event names, Evented::Object | ||||||||||||||||||||||||||||
| 624 | allows you to attach events on specific objects. The event callbacks, | ||||||||||||||||||||||||||||
| 625 | priority options, and other data are stored within the object itself. | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | =head2 Glossary | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | These terms are used throughout the Evented::Object documentation. | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | =over | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | =item B<Evented::Object> | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | This base class, providing methods for managing events. | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | =item B<Evented object> - C<$eo> | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | Refers to any object instance of Evented::Object or a package which inherits | ||||||||||||||||||||||||||||
| 640 | from it. | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | See L</"Evented object methods">. | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | =item B<Callback> | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | Event responders, known as callbacks, consist of a code reference and sometimes | ||||||||||||||||||||||||||||
| 647 | additional options describing how and when they should be executed. | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | They are executed in descending order by priority. Numerically larger priorities | ||||||||||||||||||||||||||||
| 650 | are called first. This allows you to place a certain callback in front of or | ||||||||||||||||||||||||||||
| 651 | behind another. | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | =item B<Fire object> - C<$fire> or C<$event> | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | An object representing an event fire. | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | The fire object provides methods for fetching information related to the current | ||||||||||||||||||||||||||||
| 658 | event fire. It also provides an interface for modifying the behavior of the | ||||||||||||||||||||||||||||
| 659 | remaining callbacks. | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | Fire objects are specific to the particular event fire, not the event. If you | ||||||||||||||||||||||||||||
| 662 | fire the same event twice in a row, the event object used the first time will | ||||||||||||||||||||||||||||
| 663 | not be the same as the second time. Therefore, all modifications made by the | ||||||||||||||||||||||||||||
| 664 | fire object's methods apply only to the callbacks remaining in this particular | ||||||||||||||||||||||||||||
| 665 | fire. For example, C<< $fire->cancel($callback) >> will only cancel the supplied | ||||||||||||||||||||||||||||
| 666 | callback once. | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | See L</"Fire object methods">. | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | =item B<Collection> - C<$col> or C<$collection> | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | An object representing a group of callbacks waiting to be fired. | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | Sometimes it is useful to prepare an event fire before actually calling it. | ||||||||||||||||||||||||||||
| 675 | This way, you can provide special options for the fire. Collections are returned | ||||||||||||||||||||||||||||
| 676 | by the 'prepare' methods. | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | $eo->prepare(event_name => @args)->fire(some_fire_option => $value); | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | See L</"Collection methods">. | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | =item B<Listener object> | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | An evented object that receives event notifications from another evented object. | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | Additional evented objects can be registered as "listeners," which allows them | ||||||||||||||||||||||||||||
| 687 | to respond to the events of another evented object. | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | Consider a scenario where you have a class whose objects represent a farm. You | ||||||||||||||||||||||||||||
| 690 | have another class which represents a cow. You would like to use the same | ||||||||||||||||||||||||||||
| 691 | callback for all of the moos that occur on the farm, regardless of which cow | ||||||||||||||||||||||||||||
| 692 | initiated it. | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | Rather than attaching an event callback to every cow, you can instead make the | ||||||||||||||||||||||||||||
| 695 | farm a listener of the cow. Then, you can attach a single callback to your farm. | ||||||||||||||||||||||||||||
| 696 | If your cow's event for mooing is C<moo>, your farm's event for mooing is | ||||||||||||||||||||||||||||
| 697 | C<cow.moo>. | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | B<Fire objects and listeners> | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | When an event is fired on an object, the same fire object is used for callbacks | ||||||||||||||||||||||||||||
| 702 | belonging to both the evented object and its listening objects. Therefore, | ||||||||||||||||||||||||||||
| 703 | callback names should be unique not only to the listener object but to the | ||||||||||||||||||||||||||||
| 704 | object being listened on as well. | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | You should also note the values of the fire object: | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | =over | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | =item * | ||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | B<< $fire->event_name >>: the name of the event from the perspective of the | ||||||||||||||||||||||||||||
| 713 | listener; i.e. C<cow.moo> (NOT C<moo>) | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | =item * | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | B<< $fire->object >>: the object being listened to; i.e. C<$cow> (NOT C<$farm>) | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | =back | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | This also means that stopping the event from a listener object will cancel all | ||||||||||||||||||||||||||||
| 722 | remaining callbacks, including those belonging to the evented object. | ||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | =back | ||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | Evented::Object's included core packages are prefixed with C<Evented::Object::>. | ||||||||||||||||||||||||||||
| 727 | Other packages which are specifically designed for use with Evented::Object are | ||||||||||||||||||||||||||||
| 728 | prefixed with C<Evented::>. | ||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | =head1 Evented object methods | ||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||
| 732 | The Evented::Object package provides several convenient methods for managing an | ||||||||||||||||||||||||||||
| 733 | event-driven object. | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | =head2 Evented::Object->new() | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | Creates a new Evented::Object. Typically, this method is overriden by a child | ||||||||||||||||||||||||||||
| 738 | class of Evented::Object. | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | my $eo = Evented::Object->new(); | ||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | =head2 $eo->register_callback($event_name => \&callback, %options) | ||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | Attaches an event callback the object. When the specified event is fired, each | ||||||||||||||||||||||||||||
| 745 | of the callbacks registered using this method will be called by descending | ||||||||||||||||||||||||||||
| 746 | priority order (numerically higher priority numbers are called first.) | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | $eo->register_callback(myEvent => sub { | ||||||||||||||||||||||||||||
| 749 | ... | ||||||||||||||||||||||||||||
| 750 | }, name => 'some.callback', priority => 200); | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | B<Parameters> | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | =over | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | =item * | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | B<event_name>: the name of the event. | ||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | =item * | ||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||
| 762 | B<callback>: a CODE reference to be called when the event is fired. | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | =item * | ||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | B<options>: I<optional>, a hash (NOT a hash reference) of any of the below | ||||||||||||||||||||||||||||
| 767 | options. | ||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||
| 769 | =back | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | B<%options - event handler options> | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | All of these options are B<optional>, but the use of a callback name is B<highly | ||||||||||||||||||||||||||||
| 774 | recommended>. | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | =over | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | =item * | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | B<name>: the name of the callback being registered. must be unique to this | ||||||||||||||||||||||||||||
| 781 | particular event. | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | =item * | ||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||
| 785 | B<priority>: a numerical priority of the callback. | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | =item * | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | B<before>: the name of a callback or an array reference of callback names to | ||||||||||||||||||||||||||||
| 790 | precede. | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | =item * | ||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | B<after>: the name of a callback or an array reference of callback names to | ||||||||||||||||||||||||||||
| 795 | succeed. | ||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||
| 797 | =item * | ||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | B<data>: any data that will be stored as C<< $fire->callback_data >> as the | ||||||||||||||||||||||||||||
| 800 | callback is fired. If C<data> is a hash reference, its values can be fetched | ||||||||||||||||||||||||||||
| 801 | conveniently with C<< $fire->callback_data('key') >>. | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | =item * | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | B<with_eo>: if true, the evented object will prepended to the argument list. | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | =item * | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | B<no_fire_obj>: if true, the fire object will not be prepended to the argument | ||||||||||||||||||||||||||||
| 810 | list. | ||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||
| 812 | =back | ||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||
| 814 | Note: the order of objects will always be C<$eo>, C<$fire>, C<@args>, regardless | ||||||||||||||||||||||||||||
| 815 | of omissions. By default, the argument list is C<$fire>, C<@args>. | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | You may have any number of C<before> and any | ||||||||||||||||||||||||||||
| 818 | number of C<after> options for any given callback. For instance, one callback | ||||||||||||||||||||||||||||
| 819 | may specify to be before 'b' and 'c' but after 'a'. Evented::Object will resolve | ||||||||||||||||||||||||||||
| 820 | these priorities to its best ability. | ||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | In the case that the priorities can not be resolved (for instance, if a callback | ||||||||||||||||||||||||||||
| 823 | asks to be before 'a' and after 'b' while 'b' has already asked to be before | ||||||||||||||||||||||||||||
| 824 | 'a'), the behavior of Evented::Object is not guaranteed to be consistent. In | ||||||||||||||||||||||||||||
| 825 | other words, please do your best to not request impossible priorities. | ||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||
| 827 | In any case, C<before> and C<after> options are completely ignored when a | ||||||||||||||||||||||||||||
| 828 | C<priority> is explicitly specified. | ||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | =head2 $eo->register_callback($event_name => \&callback, $cb_name, %options) | ||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||
| 832 | If the list of options is odd, it is assumed that the first element is the | ||||||||||||||||||||||||||||
| 833 | callback name. In this case, the C<with_eo> option is also automatically | ||||||||||||||||||||||||||||
| 834 | enabled. | ||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||
| 836 | $eo->register_callback(myEvent => sub { | ||||||||||||||||||||||||||||
| 837 | ... | ||||||||||||||||||||||||||||
| 838 | }, 'some.callback, priority => 200); | ||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||
| 840 | See the above method specification for parameters and supported options. | ||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||
| 842 | =head2 $eo->register_callbacks(@events) | ||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | Registers several events at once. The arguments should be a list of hash | ||||||||||||||||||||||||||||
| 845 | references. These references take the same options as | ||||||||||||||||||||||||||||
| 846 | C<< ->register_callback() >>. Returns a list of return values in the order that | ||||||||||||||||||||||||||||
| 847 | the events were specified. | ||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||
| 849 | $eo->register_callbacks( | ||||||||||||||||||||||||||||
| 850 | { myEvent => \&my_event_1, name => 'cb.1', priority => 200 }, | ||||||||||||||||||||||||||||
| 851 | { myEvent => \&my_event_2, name => 'cb.2', priority => 100 } | ||||||||||||||||||||||||||||
| 852 | ); | ||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||
| 854 | B<Parameters> | ||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||
| 856 | =over | ||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||
| 858 | =item * | ||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||
| 860 | B<events>: an array of hash references to pass to C<< ->register_callback() >>. | ||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||
| 862 | =back | ||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | =head2 $eo->delete_event($event_name) | ||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||
| 866 | Deletes all callbacks registered for the supplied event. | ||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||
| 868 | Returns a true value if any events were deleted, false otherwise. | ||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||
| 870 | $eo->delete_event('myEvent'); | ||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||
| 872 | B<Parameters> | ||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||
| 874 | =over | ||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||
| 876 | =item * | ||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||
| 878 | B<event_name>: the name of the event. | ||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||
| 880 | =back | ||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||
| 882 | =head2 $eo->delete_callback($event_name) | ||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||
| 884 | Deletes an event callback from the object with the given callback name. | ||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||
| 886 | Returns a true value if any events were deleted, false otherwise. | ||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||
| 888 | $eo->delete_callback(myEvent => 'my.callback'); | ||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||
| 890 | B<Parameters> | ||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||
| 892 | =over | ||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||
| 894 | =item * | ||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||
| 896 | B<event_name>: the name of the event. | ||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||
| 898 | =item * | ||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||
| 900 | B<callback_name>: the name of the callback being removed. | ||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||
| 902 | =back | ||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||
| 904 | =head2 $eo->fire_event($event_name => @arguments) | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | Fires the specified event, calling each callback that was registered with | ||||||||||||||||||||||||||||
| 907 | C<< ->register_callback() >> in descending order of their priorities. | ||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||
| 909 | $eo->fire_event('some_event'); | ||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||
| 911 | $eo->fire_event(some_event => $some_argument, $some_other_argument); | ||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||
| 913 | B<Parameters> | ||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||
| 915 | =over | ||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | =item * | ||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||
| 919 | B<event_name>: the name of the event being fired. | ||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||
| 921 | =item * | ||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||
| 923 | B<arguments>: I<optional>, list of arguments to pass to event callbacks. | ||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||
| 925 | =back | ||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||
| 927 | =head2 $eo->fire_once($event_name => @arguments) | ||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||
| 929 | Fires the specified event, calling each callback that was registered with | ||||||||||||||||||||||||||||
| 930 | C<< ->register_callback() >> in descending order of their priorities. | ||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||
| 932 | Then, all callbacks for the event are deleted. This method is useful for | ||||||||||||||||||||||||||||
| 933 | situations where an event will never be fired more than once. | ||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||
| 935 | $eo->fire_once('some_event'); | ||||||||||||||||||||||||||||
| 936 | $eo->fire_event(some_event => $some_argument, $some_other_argument); | ||||||||||||||||||||||||||||
| 937 | # the second does nothing because the first deleted the callbacks | ||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||
| 939 | B<Parameters> | ||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||
| 941 | =over | ||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||
| 943 | =item * | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | B<event_name>: the name of the event being fired. | ||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||
| 947 | =item * | ||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||
| 949 | B<arguments>: I<optional>, list of arguments to pass to event callbacks. | ||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||
| 951 | =back | ||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||
| 953 | =head2 $eo->add_listener($other_eo, $prefix) | ||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||
| 955 | Makes the passed evented object a listener of this evented object. See the | ||||||||||||||||||||||||||||
| 956 | "listener objects" section for more information on this feature. | ||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||
| 958 | $cow->add_listener($farm, 'cow'); | ||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||
| 960 | B<Parameters> | ||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||
| 962 | =over | ||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||
| 964 | =item * | ||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||
| 966 | B<other_eo>: the evented object that will listen. | ||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||
| 968 | =item * | ||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||
| 970 | B<prefix>: a string that event names will be prefixed with on the listener. | ||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||
| 972 | =back | ||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||
| 974 | =head2 $eo->fire_events_together(@events) | ||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||
| 976 | The C<fire_events_together()> function can be used as a method on evented | ||||||||||||||||||||||||||||
| 977 | objects. See the documentation for the function in L</"Procedural functions">. | ||||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||
| 979 | =head2 $eo->delete_listener($other_eo) | ||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||
| 981 | Removes a listener of this evented object. See the "listener objects" section | ||||||||||||||||||||||||||||
| 982 | for more information on this feature. | ||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||
| 984 | $cow->delete_listener($farm, 'cow'); | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | B<Parameters> | ||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||
| 988 | =over | ||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||
| 990 | =item * | ||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||
| 992 | B<other_eo>: the evented object that will listen. | ||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||
| 994 | =item * | ||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||
| 996 | B<prefix>: a string that event names will be prefixed with on the listener. | ||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||
| 998 | =back | ||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||
| 1000 | =head2 $eo->delete_all_events() | ||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||
| 1002 | Deletes all events and all callbacks from the object. If you know that an | ||||||||||||||||||||||||||||
| 1003 | evented object will no longer be used in your program, by calling this method | ||||||||||||||||||||||||||||
| 1004 | you can be sure that no cyclical references from within callbacks will cause the | ||||||||||||||||||||||||||||
| 1005 | object to be leaked. | ||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||
| 1007 | =head1 Preparation methods | ||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||
| 1009 | Callbacks can be prepared before being fired. This is most useful for firing | ||||||||||||||||||||||||||||
| 1010 | events with special fire options. | ||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||
| 1012 | =head2 $eo->prepare_event(event_name => @arguments) | ||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||
| 1014 | Prepares a single event for firing. Returns a collection object representing the | ||||||||||||||||||||||||||||
| 1015 | callbacks for the event. | ||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||
| 1017 | # an example using the fire option return_check. | ||||||||||||||||||||||||||||
| 1018 | $eo->prepare_event(some_event => @arguments)->fire('return_check'); | ||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||
| 1020 | =head2 $eo->prepare_together(@events) | ||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||
| 1022 | The preparatory method equivalent to C<< ->fire_events_together >>. | ||||||||||||||||||||||||||||
| 1023 | |||||||||||||||||||||||||||||
| 1024 | =head2 $eo->prepare(...) | ||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||
| 1026 | A smart method that uses the best guess between C<< ->prepare_event >> and | ||||||||||||||||||||||||||||
| 1027 | C<< ->prepare_together >>. | ||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||
| 1029 | # uses ->prepare_event() | ||||||||||||||||||||||||||||
| 1030 | $eo->prepare(some_event => @arguments); | ||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||
| 1032 | # uses ->prepare_together() | ||||||||||||||||||||||||||||
| 1033 | $eo->prepare( | ||||||||||||||||||||||||||||
| 1034 | [ some_event => @arguments ], | ||||||||||||||||||||||||||||
| 1035 | [ some_other => @other_arg ] | ||||||||||||||||||||||||||||
| 1036 | ); | ||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||
| 1038 | =head1 Class monitors | ||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||
| 1040 | =head2 $eo->monitor_events($pkg) | ||||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||
| 1042 | Registers an evented object as the class monitor for a specific package. See the | ||||||||||||||||||||||||||||
| 1043 | section above for more details on class monitors and their purpose. | ||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||
| 1045 | my $some_eo = Evented::Object->new; | ||||||||||||||||||||||||||||
| 1046 | my $other_eo = Evented::Object->new; | ||||||||||||||||||||||||||||
| 1047 | |||||||||||||||||||||||||||||
| 1048 | $some_eo->on('monitor:register_callback', sub { | ||||||||||||||||||||||||||||
| 1049 | my ($event, $eo, $event_name, $cb) = @_; | ||||||||||||||||||||||||||||
| 1050 | # $eo == $other_eo | ||||||||||||||||||||||||||||
| 1051 | # $event_name == "blah" | ||||||||||||||||||||||||||||
| 1052 | # $cb == callback hash from ->register_callback() | ||||||||||||||||||||||||||||
| 1053 | say "Registered $$cb{name} to $eo for $event_name"; | ||||||||||||||||||||||||||||
| 1054 | }); | ||||||||||||||||||||||||||||
| 1055 | |||||||||||||||||||||||||||||
| 1056 | $some_eo->monitor_events('Some::Class'); | ||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||
| 1058 | package Some::Class; | ||||||||||||||||||||||||||||
| 1059 | $other_eo->on(blah => sub{}); # will trigger the callback above | ||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||
| 1061 | =over | ||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||
| 1063 | =item * | ||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||
| 1065 | B<pkg>: a package whose event activity you wish to monitor. | ||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||
| 1067 | =back | ||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||
| 1069 | =head2 $eo->stop_monitoring($pkg) | ||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||
| 1071 | Removes an evented object from its current position as a monitor for a specific | ||||||||||||||||||||||||||||
| 1072 | package. See the section above for more details on class monitors and their | ||||||||||||||||||||||||||||
| 1073 | purpose. | ||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||
| 1075 | $some_eo->stop_monitoring('Some::Class'); | ||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||
| 1077 | =over | ||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||
| 1079 | =item * | ||||||||||||||||||||||||||||
| 1080 | |||||||||||||||||||||||||||||
| 1081 | B<pkg>: a package whose event activity you're monitoring. | ||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||
| 1083 | =back | ||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||
| 1085 | =head1 Procedural functions | ||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||
| 1087 | The Evented::Object package provides some functions for use. These functions | ||||||||||||||||||||||||||||
| 1088 | typically are associated with more than one evented object or none at all. | ||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||
| 1090 | =head2 fire_events_together(@events) | ||||||||||||||||||||||||||||
| 1091 | |||||||||||||||||||||||||||||
| 1092 | Fires multiple events at the same time. This allows you to fire multiple similar | ||||||||||||||||||||||||||||
| 1093 | events on several evented objects at the same time. It essentially pretends that | ||||||||||||||||||||||||||||
| 1094 | the callbacks are all for the same event and all on the same object. | ||||||||||||||||||||||||||||
| 1095 | |||||||||||||||||||||||||||||
| 1096 | It follows priorities throughout all of the events and all of the objects, so it | ||||||||||||||||||||||||||||
| 1097 | is ideal for firing similar or identical events on multiple objects. | ||||||||||||||||||||||||||||
| 1098 | |||||||||||||||||||||||||||||
| 1099 | The same fire object is used throughout this entire routine. This means that | ||||||||||||||||||||||||||||
| 1100 | callback names must unique among all of these objects and events. It also means | ||||||||||||||||||||||||||||
| 1101 | that stopping an event from any callback will cancel all remaining callbacks, | ||||||||||||||||||||||||||||
| 1102 | regardless to which event or which object they belong. | ||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||
| 1104 | The function takes a list of array references in the form of: | ||||||||||||||||||||||||||||
| 1105 | C<< [ $evented_object, event_name => @arguments ] >> | ||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||
| 1107 | Evented::Object::fire_events_together( | ||||||||||||||||||||||||||||
| 1108 | [ $server, user_joined_channel => $user, $channel ], | ||||||||||||||||||||||||||||
| 1109 | [ $channel, user_joined => $user ], | ||||||||||||||||||||||||||||
| 1110 | [ $user, joined_channel => $channel ] | ||||||||||||||||||||||||||||
| 1111 | ); | ||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||
| 1113 | C<< ->fire_events_together >> can also be used as a method | ||||||||||||||||||||||||||||
| 1114 | on any evented object. | ||||||||||||||||||||||||||||
| 1115 | |||||||||||||||||||||||||||||
| 1116 | $eo->fire_events_together( | ||||||||||||||||||||||||||||
| 1117 | [ some_event => @arguments ], | ||||||||||||||||||||||||||||
| 1118 | [ some_other => @other_arg ] | ||||||||||||||||||||||||||||
| 1119 | ); | ||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||
| 1121 | The above example would formerly be achieved as: | ||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||
| 1123 | Evented::Object::fire_events_together( | ||||||||||||||||||||||||||||
| 1124 | [ $eo, some_event => @arguments ], | ||||||||||||||||||||||||||||
| 1125 | [ $eo, some_other => @other_arg ] | ||||||||||||||||||||||||||||
| 1126 | ); | ||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||
| 1128 | However, other evented objects may be specified even when this is used as a | ||||||||||||||||||||||||||||
| 1129 | method. Basically, anywhere that an object is missing will fall back to the | ||||||||||||||||||||||||||||
| 1130 | object on which the method was called. | ||||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||
| 1132 | $eo->fire_events_together( | ||||||||||||||||||||||||||||
| 1133 | [ $other_eo, some_event => @arguments ], | ||||||||||||||||||||||||||||
| 1134 | [ some_other => @other_arg ] # no object, falls back to $eo | ||||||||||||||||||||||||||||
| 1135 | ); | ||||||||||||||||||||||||||||
| 1136 | |||||||||||||||||||||||||||||
| 1137 | B<Parameters> | ||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||
| 1139 | =over | ||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||
| 1141 | =item * | ||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||
| 1143 | B<events>: an array of events in the form of | ||||||||||||||||||||||||||||
| 1144 | C<< [$eo, event_name => @arguments] >>. | ||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||
| 1146 | =back | ||||||||||||||||||||||||||||
| 1147 | |||||||||||||||||||||||||||||
| 1148 | =head2 safe_fire($eo, $event_name, @args) | ||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||
| 1150 | Safely fires an event. In other words, if the C<< $eo >> is not an evented | ||||||||||||||||||||||||||||
| 1151 | object or is not blessed at all, the call will be ignored. This eliminates the | ||||||||||||||||||||||||||||
| 1152 | need to use C<blessed()> and C<< ->isa() >> on a value for testing whether it is | ||||||||||||||||||||||||||||
| 1153 | an evented object. | ||||||||||||||||||||||||||||
| 1154 | |||||||||||||||||||||||||||||
| 1155 | Evented::Object::safe_fire($eo, myEvent => 'my argument'); | ||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||
| 1157 | B<Parameters> | ||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||
| 1159 | =over | ||||||||||||||||||||||||||||
| 1160 | |||||||||||||||||||||||||||||
| 1161 | =item * | ||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||
| 1163 | B<eo>: the evented object. | ||||||||||||||||||||||||||||
| 1164 | |||||||||||||||||||||||||||||
| 1165 | =item * | ||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||
| 1167 | B<event_name>: the name of the event. | ||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||
| 1169 | =item * | ||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||
| 1171 | B<args>: the arguments for the event fire. | ||||||||||||||||||||||||||||
| 1172 | |||||||||||||||||||||||||||||
| 1173 | =back | ||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||
| 1175 | =head1 Collection methods | ||||||||||||||||||||||||||||
| 1176 | |||||||||||||||||||||||||||||
| 1177 | L</Collections> are returned by the 'prepare' methods. They represent a group of | ||||||||||||||||||||||||||||
| 1178 | callbacks that are about to be fired. | ||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||
| 1180 | =head2 $col->fire(@options) | ||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||
| 1182 | Fires the pending callbacks with the specified options, if any. If the callbacks | ||||||||||||||||||||||||||||
| 1183 | have not yet been sorted, they are sorted before the event is fired. | ||||||||||||||||||||||||||||
| 1184 | |||||||||||||||||||||||||||||
| 1185 | $eo->prepare(some_event => @arguments)->fire('safe'); | ||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||
| 1187 | B<Parameters> | ||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||
| 1189 | =over | ||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||
| 1191 | =item * | ||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||
| 1193 | B<options>: I<optional>, a mixture of boolean and key:value options for the | ||||||||||||||||||||||||||||
| 1194 | event fire. | ||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||
| 1196 | =back | ||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||
| 1198 | B<@options> | ||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||
| 1200 | =over | ||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||
| 1202 | =item * | ||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||
| 1204 | B<caller>: I<requires value>, use an alternate C<[caller 1]> value for the event | ||||||||||||||||||||||||||||
| 1205 | fire. This is typically only used internally. | ||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||
| 1207 | =item * | ||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||
| 1209 | B<return_check>: I<boolean>, if true, the event will yield that it was stopped | ||||||||||||||||||||||||||||
| 1210 | if any of the callbacks return a false value. Note however that if one callbacks | ||||||||||||||||||||||||||||
| 1211 | returns false, the rest will still be called. The fire object will only yield | ||||||||||||||||||||||||||||
| 1212 | stopped status after all callbacks have been called and any number of them | ||||||||||||||||||||||||||||
| 1213 | returned false. | ||||||||||||||||||||||||||||
| 1214 | |||||||||||||||||||||||||||||
| 1215 | =item * | ||||||||||||||||||||||||||||
| 1216 | |||||||||||||||||||||||||||||
| 1217 | B<safe>: I<boolean>, wrap all callback calls in C<eval> for safety. if any of | ||||||||||||||||||||||||||||
| 1218 | them fail, the event will be stopped at that point with the error. | ||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||
| 1220 | =item * | ||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||
| 1222 | B<fail_continue>: I<boolean>, if C<safe> above is enabled, this tells the fire | ||||||||||||||||||||||||||||
| 1223 | to continue even if one of the callbacks fails. This could be dangerous if any | ||||||||||||||||||||||||||||
| 1224 | of the callbacks expected a previous callback to be done when it actually | ||||||||||||||||||||||||||||
| 1225 | failed. | ||||||||||||||||||||||||||||
| 1226 | |||||||||||||||||||||||||||||
| 1227 | =item * | ||||||||||||||||||||||||||||
| 1228 | |||||||||||||||||||||||||||||
| 1229 | B<data>: I<requires value>, a scalar value that can be fetched by | ||||||||||||||||||||||||||||
| 1230 | C<< $fire->data >> from within the callbacks. Good for data that might be useful | ||||||||||||||||||||||||||||
| 1231 | sometimes but not frequently enough to deserve a spot in the argument list. If | ||||||||||||||||||||||||||||
| 1232 | C<data> is a hash reference, its values can be fetched conveniently with | ||||||||||||||||||||||||||||
| 1233 | C<< $fire->data('key') >>. | ||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||
| 1235 | =back | ||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||
| 1237 | =head2 $col->sort | ||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||
| 1239 | Sorts the callbacks according to C<priority>, C<before>, and C<after> options. | ||||||||||||||||||||||||||||
| 1240 | |||||||||||||||||||||||||||||
| 1241 | =head1 Fire object methods | ||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||
| 1243 | L</"Fire objects"> are passed to all callbacks of an Evented::Object (unless the | ||||||||||||||||||||||||||||
| 1244 | silent parameter was specified.) Fire objects contain information about the | ||||||||||||||||||||||||||||
| 1245 | event itself, the callback, the caller of the event, event data, and more. | ||||||||||||||||||||||||||||
| 1246 | |||||||||||||||||||||||||||||
| 1247 | =head2 $fire->object | ||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||
| 1249 | Returns the evented object. | ||||||||||||||||||||||||||||
| 1250 | |||||||||||||||||||||||||||||
| 1251 | $fire->object->delete_event('myEvent'); | ||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||
| 1253 | =head2 $fire->caller | ||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||
| 1255 | Returns the value of C<caller(1)> from within the C<< ->fire() >> method. | ||||||||||||||||||||||||||||
| 1256 | This allows you to determine from where the event was fired. | ||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||
| 1258 | my $name = $fire->event_name; | ||||||||||||||||||||||||||||
| 1259 | my @caller = $fire->caller; | ||||||||||||||||||||||||||||
| 1260 | say "Package $caller[0] line $caller[2] called event $name"; | ||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||
| 1262 | =head2 $fire->stop($reason) | ||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||
| 1264 | Cancels all remaining callbacks. This stops the rest of the event firing. After | ||||||||||||||||||||||||||||
| 1265 | a callback calls $fire->stop, the name of that callback is stored as | ||||||||||||||||||||||||||||
| 1266 | C<< $fire->stopper >>. | ||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||
| 1268 | If the event has already been stopped, this method returns the reason for which | ||||||||||||||||||||||||||||
| 1269 | the fire was stopped or "unspecified" if no reason was given. | ||||||||||||||||||||||||||||
| 1270 | |||||||||||||||||||||||||||||
| 1271 | # ignore messages from trolls | ||||||||||||||||||||||||||||
| 1272 | if ($user eq 'noah') { | ||||||||||||||||||||||||||||
| 1273 | # user is a troll. | ||||||||||||||||||||||||||||
| 1274 | # stop further callbacks. | ||||||||||||||||||||||||||||
| 1275 | return $fire->stop; | ||||||||||||||||||||||||||||
| 1276 | } | ||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||
| 1278 | =over | ||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||
| 1280 | =item * | ||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||
| 1282 | B<reason>: I<optional>, the reason for stopping the event fire. | ||||||||||||||||||||||||||||
| 1283 | |||||||||||||||||||||||||||||
| 1284 | =back | ||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||
| 1286 | =head2 $fire->stopper | ||||||||||||||||||||||||||||
| 1287 | |||||||||||||||||||||||||||||
| 1288 | Returns the callback which called C<< $fire->stop >>. | ||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||
| 1290 | if ($fire->stopper) { | ||||||||||||||||||||||||||||
| 1291 | say 'Fire was stopped by '.$fire->stopper; | ||||||||||||||||||||||||||||
| 1292 | } | ||||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||
| 1294 | =head2 $fire->exception | ||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||
| 1296 | If the event was fired with the C<< safe >> option, it is possible that an | ||||||||||||||||||||||||||||
| 1297 | exception occurred in one (or more if C<< fail_continue >> enabled) callbacks. | ||||||||||||||||||||||||||||
| 1298 | This method returns the last exception that occurred or C<< undef >> if none | ||||||||||||||||||||||||||||
| 1299 | did. | ||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||
| 1301 | if (my $e = $fire->exception) { | ||||||||||||||||||||||||||||
| 1302 | say "Exception! $e"; | ||||||||||||||||||||||||||||
| 1303 | } | ||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||
| 1305 | =head2 $fire->called($callback) | ||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||
| 1307 | If no argument is supplied, returns the number of callbacks called so far, | ||||||||||||||||||||||||||||
| 1308 | including the current one. If a callback argument is supplied, returns whether | ||||||||||||||||||||||||||||
| 1309 | that particular callback has been called. | ||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||
| 1311 | say $fire->called, 'callbacks have been called so far.'; | ||||||||||||||||||||||||||||
| 1312 | |||||||||||||||||||||||||||||
| 1313 | if ($fire->called('some.callback')) { | ||||||||||||||||||||||||||||
| 1314 | say 'some.callback has been called already.'; | ||||||||||||||||||||||||||||
| 1315 | } | ||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||
| 1317 | B<Parameters> | ||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||
| 1319 | =over | ||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||
| 1321 | =item * | ||||||||||||||||||||||||||||
| 1322 | |||||||||||||||||||||||||||||
| 1323 | B<callback>: I<optional>, the callback being checked. | ||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||
| 1325 | =back | ||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||
| 1327 | =head2 $fire->pending($callback) | ||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||
| 1329 | If no argument is supplied, returns the number of callbacks pending to be | ||||||||||||||||||||||||||||
| 1330 | called, excluding the current one. If a callback argument is supplied, returns | ||||||||||||||||||||||||||||
| 1331 | whether that particular callback is pending for being called. | ||||||||||||||||||||||||||||
| 1332 | |||||||||||||||||||||||||||||
| 1333 | say $fire->pending, ' callbacks are left.'; | ||||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||
| 1335 | if ($fire->pending('some.callback')) { | ||||||||||||||||||||||||||||
| 1336 | say 'some.callback will be called soon (unless it gets canceled)'; | ||||||||||||||||||||||||||||
| 1337 | } | ||||||||||||||||||||||||||||
| 1338 | |||||||||||||||||||||||||||||
| 1339 | B<Parameters> | ||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||
| 1341 | =over | ||||||||||||||||||||||||||||
| 1342 | |||||||||||||||||||||||||||||
| 1343 | =item * | ||||||||||||||||||||||||||||
| 1344 | |||||||||||||||||||||||||||||
| 1345 | B<callback>: I<optional>, the callback being checked. | ||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||
| 1347 | =back | ||||||||||||||||||||||||||||
| 1348 | |||||||||||||||||||||||||||||
| 1349 | =head2 $fire->cancel($callback) | ||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||
| 1351 | Cancels the supplied callback once. | ||||||||||||||||||||||||||||
| 1352 | |||||||||||||||||||||||||||||
| 1353 | if ($user eq 'noah') { | ||||||||||||||||||||||||||||
| 1354 | # we don't love noah! | ||||||||||||||||||||||||||||
| 1355 | $fire->cancel('send.hearts'); | ||||||||||||||||||||||||||||
| 1356 | } | ||||||||||||||||||||||||||||
| 1357 | |||||||||||||||||||||||||||||
| 1358 | B<Parameters> | ||||||||||||||||||||||||||||
| 1359 | |||||||||||||||||||||||||||||
| 1360 | =over | ||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||
| 1362 | =item * | ||||||||||||||||||||||||||||
| 1363 | |||||||||||||||||||||||||||||
| 1364 | B<callback>: the callback to be cancelled. | ||||||||||||||||||||||||||||
| 1365 | |||||||||||||||||||||||||||||
| 1366 | =back | ||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||
| 1368 | =head2 $fire->return_of($callback) | ||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||
| 1370 | Returns the return value of the supplied callback. | ||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||
| 1372 | if ($fire->return_of('my.callback')) { | ||||||||||||||||||||||||||||
| 1373 | say 'my.callback returned a true value'; | ||||||||||||||||||||||||||||
| 1374 | } | ||||||||||||||||||||||||||||
| 1375 | |||||||||||||||||||||||||||||
| 1376 | B<Parameters> | ||||||||||||||||||||||||||||
| 1377 | |||||||||||||||||||||||||||||
| 1378 | =over | ||||||||||||||||||||||||||||
| 1379 | |||||||||||||||||||||||||||||
| 1380 | =item * | ||||||||||||||||||||||||||||
| 1381 | |||||||||||||||||||||||||||||
| 1382 | B<callback>: the desired callback. | ||||||||||||||||||||||||||||
| 1383 | |||||||||||||||||||||||||||||
| 1384 | =back | ||||||||||||||||||||||||||||
| 1385 | |||||||||||||||||||||||||||||
| 1386 | =head2 $fire->last | ||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||
| 1388 | Returns the most recent previous callback called. | ||||||||||||||||||||||||||||
| 1389 | This is also useful for determining which callback was the last to be called. | ||||||||||||||||||||||||||||
| 1390 | |||||||||||||||||||||||||||||
| 1391 | say $fire->last, ' was called before this one.'; | ||||||||||||||||||||||||||||
| 1392 | |||||||||||||||||||||||||||||
| 1393 | my $fire = $eo->fire_event('myEvent'); | ||||||||||||||||||||||||||||
| 1394 | say $fire->last, ' was the last callback called.'; | ||||||||||||||||||||||||||||
| 1395 | |||||||||||||||||||||||||||||
| 1396 | =head2 $fire->last_return | ||||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||
| 1398 | Returns the last callback's return value. | ||||||||||||||||||||||||||||
| 1399 | |||||||||||||||||||||||||||||
| 1400 | if ($fire->last_return) { | ||||||||||||||||||||||||||||
| 1401 | say 'the callback before this one returned a true value.'; | ||||||||||||||||||||||||||||
| 1402 | } | ||||||||||||||||||||||||||||
| 1403 | else { | ||||||||||||||||||||||||||||
| 1404 | die 'the last callback returned a false value.'; | ||||||||||||||||||||||||||||
| 1405 | } | ||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||
| 1407 | =head2 $fire->event_name | ||||||||||||||||||||||||||||
| 1408 | |||||||||||||||||||||||||||||
| 1409 | Returns the name of the event. | ||||||||||||||||||||||||||||
| 1410 | |||||||||||||||||||||||||||||
| 1411 | say 'the event being fired is ', $fire->event_name; | ||||||||||||||||||||||||||||
| 1412 | |||||||||||||||||||||||||||||
| 1413 | =head2 $fire->callback_name | ||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||
| 1415 | Returns the name of the current callback. | ||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||
| 1417 | say 'the current callback being called is ', $fire->callback_name; | ||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||
| 1419 | =head2 $fire->callback_priority | ||||||||||||||||||||||||||||
| 1420 | |||||||||||||||||||||||||||||
| 1421 | Returns the priority of the current callback. | ||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||
| 1423 | say 'the priority of the current callback is ', $fire->callback_priority; | ||||||||||||||||||||||||||||
| 1424 | |||||||||||||||||||||||||||||
| 1425 | =head2 $fire->callback_data($key) | ||||||||||||||||||||||||||||
| 1426 | |||||||||||||||||||||||||||||
| 1427 | Returns the data supplied to the callback when it was registered, if any. If the | ||||||||||||||||||||||||||||
| 1428 | data is a hash reference, an optional key parameter can specify a which value to | ||||||||||||||||||||||||||||
| 1429 | fetch. | ||||||||||||||||||||||||||||
| 1430 | |||||||||||||||||||||||||||||
| 1431 | say 'my data is ', $fire->callback_data; | ||||||||||||||||||||||||||||
| 1432 | say 'my name is ', $fire->callback_data('name'); | ||||||||||||||||||||||||||||
| 1433 | |||||||||||||||||||||||||||||
| 1434 | B<Parameters> | ||||||||||||||||||||||||||||
| 1435 | |||||||||||||||||||||||||||||
| 1436 | =over | ||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||
| 1438 | =item * | ||||||||||||||||||||||||||||
| 1439 | |||||||||||||||||||||||||||||
| 1440 | B<key>: I<optional>, a key to fetch a value if the data registered was a hash. | ||||||||||||||||||||||||||||
| 1441 | |||||||||||||||||||||||||||||
| 1442 | =back | ||||||||||||||||||||||||||||
| 1443 | |||||||||||||||||||||||||||||
| 1444 | =head2 $fire->data($key) | ||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||
| 1446 | Returns the data supplied to the collection when it was fired, if any. If the | ||||||||||||||||||||||||||||
| 1447 | data is a hash reference, an optional key parameter can specify a which value to | ||||||||||||||||||||||||||||
| 1448 | fetch. | ||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||
| 1450 | say 'fire data is ', $fire->data; | ||||||||||||||||||||||||||||
| 1451 | say 'fire time was ', $fire->data('time'); | ||||||||||||||||||||||||||||
| 1452 | |||||||||||||||||||||||||||||
| 1453 | B<Parameters> | ||||||||||||||||||||||||||||
| 1454 | |||||||||||||||||||||||||||||
| 1455 | =over | ||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||
| 1457 | =item * | ||||||||||||||||||||||||||||
| 1458 | |||||||||||||||||||||||||||||
| 1459 | B<key>: I<optional>, a key to fetch a value if the data registered was a hash. | ||||||||||||||||||||||||||||
| 1460 | |||||||||||||||||||||||||||||
| 1461 | =back | ||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||
| 1463 | =head1 Aliases | ||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||
| 1465 | A number of aliases exist for convenience, but some of the names are rather | ||||||||||||||||||||||||||||
| 1466 | broad. For that reason, they are only recommended for use when you are sure that | ||||||||||||||||||||||||||||
| 1467 | other subclassing will not interfere. | ||||||||||||||||||||||||||||
| 1468 | |||||||||||||||||||||||||||||
| 1469 | =head2 $eo->on(...) | ||||||||||||||||||||||||||||
| 1470 | |||||||||||||||||||||||||||||
| 1471 | Alias for C<< $eo->register_callback() >>. | ||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||
| 1473 | =head2 $eo->del(...) | ||||||||||||||||||||||||||||
| 1474 | |||||||||||||||||||||||||||||
| 1475 | If one argument provided, alias for C<< $eo->delete_event >>. | ||||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||
| 1477 | If two arguments provided, alias for C<< $eo->delete_callback >>. | ||||||||||||||||||||||||||||
| 1478 | |||||||||||||||||||||||||||||
| 1479 | =head2 $eo->fire(...) | ||||||||||||||||||||||||||||
| 1480 | |||||||||||||||||||||||||||||
| 1481 | Alias for C<< $eo->fire_event() >>. | ||||||||||||||||||||||||||||
| 1482 | |||||||||||||||||||||||||||||
| 1483 | =head2 $eo->register_event(...) | ||||||||||||||||||||||||||||
| 1484 | |||||||||||||||||||||||||||||
| 1485 | Alias for C<< $eo->register_callback() >>. | ||||||||||||||||||||||||||||
| 1486 | |||||||||||||||||||||||||||||
| 1487 | =head2 $eo->register_events(...) | ||||||||||||||||||||||||||||
| 1488 | |||||||||||||||||||||||||||||
| 1489 | Alias for C<< $eo->register_callbacks() >>. | ||||||||||||||||||||||||||||
| 1490 | |||||||||||||||||||||||||||||
| 1491 | =head2 $fire->eo | ||||||||||||||||||||||||||||
| 1492 | |||||||||||||||||||||||||||||
| 1493 | Alias for C<< $fire->object >>. | ||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||
| 1495 | =head1 ADVANCED FEATURES | ||||||||||||||||||||||||||||
| 1496 | |||||||||||||||||||||||||||||
| 1497 | =head2 Registering callbacks to package names | ||||||||||||||||||||||||||||
| 1498 | |||||||||||||||||||||||||||||
| 1499 | The methods C<< ->register_callback() >>, C<< ->delete_event() >>, | ||||||||||||||||||||||||||||
| 1500 | C<< ->delete_callback >>, etc. can be called in the form of | ||||||||||||||||||||||||||||
| 1501 | C<< MyClass->method() >>. Evented::Object will store these callbacks in the | ||||||||||||||||||||||||||||
| 1502 | package's symbol table. | ||||||||||||||||||||||||||||
| 1503 | |||||||||||||||||||||||||||||
| 1504 | Any object of this class will borrow these callbacks from the class. They will | ||||||||||||||||||||||||||||
| 1505 | be incorporated into the callback collection as though they were registered | ||||||||||||||||||||||||||||
| 1506 | directly on the object. | ||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||
| 1508 | Note that events cannot be fired on a class, only on evented objects. | ||||||||||||||||||||||||||||
| 1509 | |||||||||||||||||||||||||||||
| 1510 | Note that if an evented object is blessed to a subclass of a class with | ||||||||||||||||||||||||||||
| 1511 | callbacks registered to it, the object will NOT inherit the callbacks associated | ||||||||||||||||||||||||||||
| 1512 | with the parent class. Callbacks registered to classes ONLY apply to objects | ||||||||||||||||||||||||||||
| 1513 | directly blessed to the class. | ||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||
| 1515 | =head2 Class monitors | ||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||
| 1517 | An evented object can be registered as a "monitor" of a specific class/package. | ||||||||||||||||||||||||||||
| 1518 | |||||||||||||||||||||||||||||
| 1519 | I<All> event callbacks that are added from that class to I<any> evented object | ||||||||||||||||||||||||||||
| 1520 | of I<any> type will trigger an event on the monitor object. | ||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||
| 1522 | An example scenario of when this might be useful is an evented object for | ||||||||||||||||||||||||||||
| 1523 | debugging all events being registered by a certain package. It would log all of | ||||||||||||||||||||||||||||
| 1524 | them, making it easier to find a problem. | ||||||||||||||||||||||||||||
| 1525 | |||||||||||||||||||||||||||||
| 1526 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 1527 | |||||||||||||||||||||||||||||
| 1528 | L<Mitchell Cooper|https://github.com/cooper> <cooper@cpan.org> | ||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||
| 1530 | Copyright E<copy> 2011-2017. Released under BSD license. | ||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||
| 1532 | Comments, complaints, and recommendations are accepted. Bugs may be reported on | ||||||||||||||||||||||||||||
| 1533 | L<RT|https://rt.cpan.org/Public/Dist/Display.html?Name=Evented-Object>. | ||||||||||||||||||||||||||||