blib/lib/MooX/Role/POE/Emitter.pm

filter: Statementstmt 93.0% 214 / 230 Branchbran 67.1% 47 / 70 Conditioncond 42.8% 9 / 21 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 96.2% 51 / 53 Podpod 100% 7 / 7 totaltotal 83.9% 330 / 393 CC 49 SCAR 13.6
CC
49
Cov
84%
_trigger_object_states
10.1
__emitter_notify
8.0
_start_emitter
7.6
CRAP 3.9
↑ Summary
1 package MooX::Role::POE::Emitter;
2 2 $MooX::Role::POE::Emitter::VERSION = '1.001002';
3 4 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use List::Objects::WithUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Objects::Types -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use MooX::Role::Pluggable::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub E_TAG () { 'Emitter Running' }
18  
19 =pod
20  
21 =for Pod::Coverage E_TAG
22  
23 =cut
24  
25  
26 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 with 'MooX::Role::Pluggable';
28  
29  
30 has alias => (
31 lazy => 1,
32 is => 'ro',
33 isa => Str,
34 predicate => 'has_alias',
35 writer => 'set_alias',
36 2 ▶ default => sub { my $self = shift; "$self" },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 );
38  
39 2 ▶around set_alias => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
40 2 my ($orig, $self, $value) = @_;
41  
42 2 ▶ if ( $poe_kernel->alias_resolve( $self->session_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MooX::Role::POE::Emitter::poe_kernel->alias_resolve($self->session_id)) 1 1 2
43 1 $self->call( __emitter_reset_alias => $value );
44 1 $self->emit( $self->event_prefix . 'alias_set' => $value );
45 }
46  
47 2 $self->$orig($value)
48 2 };
49  
50 has event_prefix => (
51 lazy => 1,
52 is => 'ro',
53 isa => Str,
54 predicate => 'has_event_prefix',
55 writer => 'set_event_prefix',
56 3 ▶ default => sub { 'emitted_' },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 2 );
58  
59 has pluggable_type_prefixes => (
60 ## Optionally remap PROCESS / NOTIFY types
61 lazy => 1,
62 is => 'ro',
63 isa => HashObj,
64 coerce => 1,
65 predicate => 'has_pluggable_type_prefixes',
66 writer => 'set_pluggable_type_prefixes',
67 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 hash( PROCESS => 'P', NOTIFY => 'N' )
69 },
70 2 );
71  
72  
73 has object_states => (
74 lazy => 1,
75 is => 'ro',
76 isa => ArrayObj,
77 coerce => 1,
78 predicate => 'has_object_states',
79 writer => 'set_object_states',
80 trigger => 1,
81 2 ▶ default => sub { array },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 2 );
83  
84 1 ▶sub _trigger_object_states {
Subroutine _trigger_object_states: called CC=8 SCAR=23.2
85 1 my ($self, $states) = @_;
86  
87 1 ▶ $states = array(%$states) if reftype $states eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($states) eq "HASH" 0 1 1
88  
89 1 ▶ confess "object_states() should be an ARRAY or HASH"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $states and builtin::reftype($states) eq "ARRAY" 0 1 1
Condition33%1/3
ref $states and builtin::reftype($states) eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $states and builtin::reftype($states) eq "ARRAY"
ref $states builtin::reftype($states) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $statesB= builtin::reftype($states) eq "ARRAY"
ABresult
0X0
100
111
90 unless ref $states and reftype $states eq 'ARRAY';
91  
92 1 ▶ $states = array(@$states) unless is_ArrayObj $states;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_ArrayObj($states) 0 1 1
93  
94 state $disallowed = array( qw/
95 _start
96 _stop
97 _default
98 emit
99 register
100 unregister
101 subscribe
102 unsubscribe
103 1 ▶ / )->map(sub { $_ => 1 })->inflate;
Subroutine __ANON__: called CC=1 SCAR=0.0
104  
105 1 my $itr = $states->natatime(2);
106 1 ▶ while (my (undef, $events) = $itr->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $events) = &$itr()) 1 1 2
107 1 ▶ my $evarr = reftype $events eq 'ARRAY' ? array(@$events)
Branch: 2 branches
BranchTrueFalseTotal
builtin::reftype($events) eq 'ARRAY' ? : 1 0 1
builtin::reftype($events) eq 'HASH' ? : 0 0 0
108 : reftype $events eq 'HASH' ? array(keys %$events)
109 : confess "Expected ARRAY or HASH but got $events";
110 $evarr->map(
111 6 ▶ sub { confess "Disallowed handler: $_" if $disallowed->exists($_) }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $disallowed->exists($_) 0 6 6
112 1 );
113 }
114 }
115  
116  
117 has register_prefix => (
118 lazy => 1,
119 is => 'ro',
120 isa => Str,
121 predicate => 'has_register_prefix',
122 writer => 'set_register_prefix',
123 ## Emitter_register / Emitter_unregister
124 3 ▶ default => sub { 'Emitter_' },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 2 );
126  
127 has session_id => (
128 init_arg => undef,
129 lazy => 1,
130 is => 'ro',
131 isa => Defined,
132 predicate => 'has_session_id',
133 writer => 'set_session_id',
134 1 ▶ default => sub { -1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 2 );
136  
137 has shutdown_signal => (
138 lazy => 1,
139 is => 'ro',
140 isa => Str,
141 predicate => 'has_shutdown_signal',
142 writer => 'set_shutdown_signal',
143 3 ▶ default => sub { 'SHUTDOWN_EMITTER' },
Subroutine __ANON__: called CC=1 SCAR=0.0
144 2 );
145  
146 has __emitter_reg_sessions => (
147 lazy => 1,
148 is => 'ro',
149 isa => TypedHash[Object],
150 3 ▶ default => sub { hash_of Object },
Subroutine __ANON__: called CC=1 SCAR=0.0
151 2 );
152  
153  
154 has __emitter_reg_events => (
155 ## ->{ $event }->{ $session_id } = 1
156 lazy => 1,
157 is => 'ro',
158 isa => TypedHash[ TypedHash[Int] ],
159 coerce => 1,
160 3 ▶ default => sub { hash_of TypedHash[Int] },
Subroutine __ANON__: called CC=1 SCAR=0.0
161 2 );
162  
163  
164 3 ▶sub _start_emitter {
Subroutine _start_emitter: called CC=5 SCAR=20.3
165 ## Call to spawn Session.
166 ## my $emitter = MyClass->new(
167 ## alias => Emitter session alias
168 ## event_prefix => Session event prefix (emitted_)
169 ## register_prefix => _register/_unregister prefix (Emitter_)
170 ## object_states => Extra object_states for Session
171 ## )->_start_emitter();
172 3 my ($self) = @_;
173  
174 3 my %types;
175 3 ▶ if ( $self->has_pluggable_type_prefixes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pluggable_type_prefixes) { } 0 3 3
176 0 ▶ $types{PROCESS} = $self->pluggable_type_prefixes->{PROCESS} ||= 'P';
Condition0%0/2
$self->pluggable_type_prefixes->{"PROCESS"} ||= "P"
l!l
00
MC/DC0%0/1
$self->pluggable_type_prefixes->{"PROCESS"} ||= "P"
$self->pluggable_type_prefixes->{"PROCESS"}
Truth table0%0/2
A
A= $self->pluggable_type_prefixes->{"PROCESS"}
Aresult
11
00
177 0 ▶ $types{NOTIFY} = $self->pluggable_type_prefixes->{NOTIFY} ||= 'N';
Condition0%0/2
$self->pluggable_type_prefixes->{"NOTIFY"} ||= "N"
l!l
00
MC/DC0%0/1
$self->pluggable_type_prefixes->{"NOTIFY"} ||= "N"
$self->pluggable_type_prefixes->{"NOTIFY"}
Truth table0%0/2
A
A= $self->pluggable_type_prefixes->{"NOTIFY"}
Aresult
11
00
178 } else {
179 3 %types = ( PROCESS => 'P', NOTIFY => 'N' );
180 }
181  
182 3 $self->_pluggable_init(
183 event_prefix => $self->event_prefix,
184 reg_prefix => $self->register_prefix,
185 types => \%types,
186 );
187  
188 3 ▶ POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_object_states ? : 3 0 3
189 object_states => [
190  
191 $self => {
192 '_start' => '__emitter_start',
193 '_stop' => '__emitter_stop',
194 'shutdown_emitter' => '__shutdown_emitter',
195  
196 'register' => '__emitter_register',
197 'subscribe' => '__emitter_register',
198 'unregister' => '__emitter_unregister',
199 'unsubscribe' => '__emitter_unregister',
200 'emit' => '__emitter_notify',
201  
202 '_default' => '__emitter_disp_default',
203 '__emitter_real_default' => '_emitter_default',
204 },
205  
206 $self => [ qw/
207 __emitter_notify
208  
209 __emitter_timer_set
210 __emitter_timer_del
211  
212 __emitter_sigdie
213  
214 __emitter_reset_alias
215  
216 __emitter_sig_shutdown
217 / ],
218  
219 (
220 $self->has_object_states ? $self->object_states->all : ()
221 ),
222  
223 ],
224 );
225  
226 3 $self
227 }
228  
229 2 ▶around '_pluggable_event' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
230 2 my ($orig, $self) = splice @_, 0, 2;
231  
232 ## Overriden from Role::Pluggable
233 ## Receives plugin_error, plugin_add etc
234 ## Redispatch via emit()
235  
236 2 $self->emit( @_ );
237 2 };
238  
239  
240 ### Public:
241  
242 3 ▶sub timer {
Subroutine timer: called CC=3 SCAR=12.5
Pod documented
243 3 my ($self, $time, $event, @args) = @_;
244  
245 3 ▶ confess "timer() expected at least a time and event name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time and defined $event 0 3 3
Condition33%1/3
defined $time and defined $event
!ll&&!rl&&r
003
MC/DC0%0/2
defined $time and defined $event
defined $time defined $event
Truth table33%1/3
A and B
A= defined $timeB= defined $event
ABresult
0X0
100
111
246 unless defined $time and defined $event;
247  
248 3 $self->call( __emitter_timer_set => $time, $event, @args )
249 }
250  
251 3 ▶sub __emitter_timer_set {
Subroutine __emitter_timer_set: called CC=2 SCAR=7.0
252 3 my ($kernel, $self) = @_[KERNEL, OBJECT];
253 3 my ($time, $event, @args) = @_[ARG0 .. $#_];
254  
255 3 my $alarm_id = $poe_kernel->delay_set( $event, $time, @args );
256  
257 3 ▶ $self->emit( $self->event_prefix . 'timer_set',
Branch: 1 branch
BranchTrueFalseTotal
if $alarm_id 3 0 3
258 $alarm_id,
259 $event,
260 $time,
261 @args
262 ) if $alarm_id;
263  
264 3 $alarm_id
265 }
266  
267 1 ▶sub timer_del {
Subroutine timer_del: called CC=2 SCAR=7.1
Pod documented
268 1 my ($self, $alarm_id) = @_;
269  
270 1 ▶ confess "timer_del() expects an alarm ID"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alarm_id 0 1 1
271 unless defined $alarm_id;
272  
273 1 $self->call( __emitter_timer_del => $alarm_id );
274 }
275  
276 1 ▶sub __emitter_timer_del {
Subroutine __emitter_timer_del: called CC=3 SCAR=16.7
277 1 my ($kernel, $self, $alarm_id) = @_[KERNEL, OBJECT, ARG0];
278  
279 1 my @deleted = $poe_kernel->alarm_remove($alarm_id);
280 1 ▶ return unless @deleted;
Branch: 1 branch
BranchTrueFalseTotal
unless @deleted 1 0 1
281  
282 0 my ($event, undef, $params) = @deleted;
283  
284 $self->emit( $self->event_prefix . 'timer_deleted',
285 $alarm_id,
286 $event,
287 0 ▶ @{ $params || [] }
Condition0%0/2
$params || []
l!l
00
MC/DC0%0/1
$params || []
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
288 );
289  
290 0 $params
291 }
292  
293 ## yield/call provide post()/call() frontends.
294 20 ▶sub yield {
Subroutine yield: called CC=1 SCAR=0.0
Pod documented
295 20 my ($self, @args) = @_;
296  
297 20 $poe_kernel->post( $self->session_id, @args );
298  
299 20 $self
300 }
301  
302 32 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
303 32 my ($self, @args) = @_;
304  
305 32 $poe_kernel->call( $self->session_id, @args );
306  
307 32 $self
308 }
309  
310 14 ▶sub emit {
Subroutine emit: called CC=1 SCAR=0.0
Pod documented
311 ## Async NOTIFY event dispatch.
312 14 my ($self, $event, @args) = @_;
313  
314 14 $self->yield( __emitter_notify => $event, @args );
315  
316 14 $self
317 }
318  
319 1 ▶sub emit_now {
Subroutine emit_now: called CC=1 SCAR=0.0
Pod documented
320 ## Synchronous NOTIFY event dispatch.
321 1 my ($self, $event, @args) = @_;
322  
323 1 $self->call( __emitter_notify => $event, @args );
324  
325 1 $self
326 }
327  
328 10 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod documented
329 10 my ($self, $event, @args) = @_;
330 ## Dispatch PROCESS events.
331 ## process() events should _pluggable_process immediately
332 ## and return the EAT value.
333  
334 ## Dispatched to P_$event :
335 10 $self->_pluggable_process( PROCESS => $event, \@args )
336 }
337  
338  
339  
340 ## Session ref-counting bits.
341 2 { package MooX::Role::POE::Emitter::RegisteredSession;
342 2 $MooX::Role::POE::Emitter::RegisteredSession::VERSION = '1.001002';
343 2 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
344 2 has [qw/id refcount/] => ( is => 'rw', required => 1 );
345 }
346  
347 7 ▶sub __get_ses_refc {
Subroutine __get_ses_refc: called CC=2 SCAR=7.0
348 7 my ($self, $sess_id) = @_;
349 7 my $regsess_obj = $self->__emitter_reg_sessions->get($sess_id);
350 7 ▶ return unless $regsess_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $regsess_obj 0 7 7
351 7 $regsess_obj->refcount
352 }
353  
354 7 ▶sub __reg_ses_id {
Subroutine __reg_ses_id: called CC=2 SCAR=6.9
355 7 my ($self, $sess_id) = @_;
356 7 ▶ return if $self->__emitter_reg_sessions->exists($sess_id);
Branch: 1 branch
BranchTrueFalseTotal
if $self->__emitter_reg_sessions->exists($sess_id) 3 4 7
357 4 $self->__emitter_reg_sessions->set($sess_id =>
358 MooX::Role::POE::Emitter::RegisteredSession->new(
359 id => $sess_id,
360 refcount => 0
361 )
362 );
363 }
364  
365 7 ▶sub __incr_ses_refc {
Subroutine __incr_ses_refc: called CC=2 SCAR=7.2
366 7 my ($self, $sess_id) = @_;
367  
368 7 my $regsess_obj = $self->__emitter_reg_sessions->get($sess_id);
369 7 ▶ unless (defined $regsess_obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regsess_obj) 0 7 7
370 0 confess "BUG; attempted to increase nonexistant refcount for '$sess_id'";
371 }
372  
373 7 $self->__emitter_reg_sessions->set($sess_id =>
374 MooX::Role::POE::Emitter::RegisteredSession->new(
375 id => $sess_id,
376 refcount => $regsess_obj->refcount + 1,
377 )
378 );
379  
380 7 $self->__emitter_reg_sessions->get($sess_id)->refcount
381 }
382  
383 1 ▶sub __decr_ses_refc {
Subroutine __decr_ses_refc: called CC=3 SCAR=11.4
384 1 my ($self, $sess_id) = @_;
385  
386 1 my $regsess_obj = $self->__emitter_reg_sessions->get($sess_id);
387 1 ▶ unless (defined $regsess_obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regsess_obj) 0 1 1
388 0 confess "BUG; attempted to decrease nonexistant refcount for '$sess_id'"
389 }
390  
391 $self->__emitter_reg_sessions->set($sess_id =>
392 1 do {
393 1 my $refc = $regsess_obj->refcount - 1;
394 1 ▶ $refc = 0 if $refc < 0; # FIXME delete (and return above) instead?
Branch: 1 branch
BranchTrueFalseTotal
if $refc < 0 0 1 1
395 1 MooX::Role::POE::Emitter::RegisteredSession->new(
396 id => $sess_id,
397 refcount => $refc,
398 )
399 },
400 );
401 }
402  
403 10 ▶sub __emitter_drop_sessions {
Subroutine __emitter_drop_sessions: called CC=3 SCAR=11.0
404 10 my ($self) = @_;
405  
406 10 for my $id ($self->__emitter_reg_sessions->keys->all) {
407 3 my $count = $self->__get_ses_refc($id);
408 3 ▶ while ( $count-- > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count-- > 0) 6 3 9
409 6 $poe_kernel->refcount_decrement( $id, E_TAG )
410 }
411  
412 3 $self->__emitter_reg_sessions->delete($id)
413 }
414  
415 1
416 10 }
417  
418  
419 ## Our Session's handlers:
420  
421 15 ▶sub __emitter_notify {
Subroutine __emitter_notify: called CC=8 SCAR=20.8
422 ## Dispatch a NOTIFY event
423 15 my ($kernel, $self) = @_[KERNEL, OBJECT];
424 15 my ($event, @args) = @_[ARG0 .. $#_];
425  
426 15 my $prefix = $self->event_prefix;
427  
428 ## May have event_prefix (such as $prefix.'plugin_error')
429 15 ▶ substr($event, 0, length($prefix), '') if index($event, $prefix) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($event, $prefix) == 0 6 9 15
430  
431 15 my %sessions;
432  
433 15 for my $registered_ev ('all', $event) {
434 30 ▶ if (my $sess_hash = $self->__emitter_reg_events->get($registered_ev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sess_hash = $self->__emitter_reg_events->get($registered_ev)) 15 15 30
435 24 ▶ $sess_hash->keys->visit(sub { $sessions{$_} = 1 })
Subroutine __ANON__: called CC=1 SCAR=0.0
436 15 }
437 }
438  
439 15 my $meth = $prefix . $event;
440  
441 ## Our own session will get ->event_prefix . $event first
442 $kernel->call( $_[SESSION], $meth, @args )
443 15 ▶ if delete $sessions{ $_[SESSION]->ID };
Branch: 1 branch
BranchTrueFalseTotal
if delete $sessions{$_[1]->ID} 9 6 15
444  
445 ## Dispatched to N_$event after our Session has been notified:
446 15 ▶ unless ( $self->_pluggable_process('NOTIFY', $event, \@args) == EAT_ALL ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_pluggable_process("NOTIFY", $event, \@args) == 4) 13 2 15
447 ## Notify subscribed sessions.
448 13 $kernel->call( $_ => $meth, @args ) for keys %sessions;
449 }
450  
451 ## Received emitted 'shutdown', drop sessions.
452 15 ▶ $self->__emitter_drop_sessions if $event eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "shutdown" 5 10 15
453 }
454  
455 3 ▶sub __emitter_start {
Subroutine __emitter_start: called CC=2 SCAR=6.9
456 ## _start handler
457 3 my ($kernel, $self) = @_[KERNEL, OBJECT];
458 3 my ($session, $sender) = @_[SESSION, SENDER];
459  
460 3 $self->set_session_id( $session->ID );
461  
462 3 $kernel->alias_set( $self->alias );
463  
464 3 $kernel->sig( DIE => '__emitter_sigdie' );
465 3 $kernel->sig( $self->shutdown_signal => '__emitter_sig_shutdown' );
466  
467 3 ▶ unless ($sender == $kernel) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sender == $kernel) 3 0 3
468 ## Have a parent session.
469 3 my $s_id = $sender->ID;
470 3 $kernel->refcount_increment( $s_id, E_TAG );
471 3 $self->__reg_ses_id( $s_id );
472 3 $self->__incr_ses_refc( $s_id );
473  
474 ## subscribe parent session to all notification events
475 3 $self->__emitter_reg_events->{all}->{ $s_id } = 1;
476  
477 ## Detach child session.
478 3 $kernel->detach_myself;
479 }
480  
481 3 $self->call('emitter_started');
482  
483 3 $self
484 }
485  
486 1 ▶sub __emitter_reset_alias {
Subroutine __emitter_reset_alias: called CC=1 SCAR=0.0
487 1 my ($kernel, $self) = @_[KERNEL, OBJECT];
488 1 $kernel->alias_set( $_[ARG0] );
489 }
490  
491 15 ▶sub __emitter_disp_default {
Subroutine __emitter_disp_default: called CC=2 SCAR=6.9
492 15 my ($kernel, $self) = @_[KERNEL, OBJECT];
493 15 my ($event, $args) = @_[ARG0, ARG1];
494  
495 15 ▶ if (ref $event eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq 'CODE') { } 2 13 15
496 ## Anonymous coderef callback.
497 ## Cute trick from dngor:
498 ## - Shove arguments back into @_
499 ## (starting at ARG0 and replacing ARG0/ARG1)
500 ## - Set $_[STATE] to our coderef
501 ## (callback sub can retrieve itself via $_[STATE])
502 ## - Replace current subroutine
503 2 splice @_, ARG0, 2, @$args;
504 2 $_[STATE] = $event;
505 2 goto $event
506 } else {
507 13 $self->call( __emitter_real_default => $event, $args );
508 }
509 }
510  
511 13 ▶sub _emitter_default {
Subroutine _emitter_default: called CC=4 SCAR=13.9
512 13 my ($kernel, $self) = @_[KERNEL, OBJECT];
513 13 my ($event, $args) = @_[ARG0, ARG1];
514  
515 ## Session received an unknown event.
516 ## Dispatch it to any appropriate P_$event handlers.
517  
518 13 ▶ $self->process( $event, @$args )
Branch: 1 branch
BranchTrueFalseTotal
unless index($event, "_") == 0 or index($event, "emitter_") == 0 and $event =~ /(?:started|stopped)$/ 9 4 13
Condition83%5/6
index($event, "_") == 0 or index($event, "emitter_") == 0 and $event =~ /(?:started|stopped)$/
l!l&&r!l&&!r
319
index($event, "emitter_") == 0 and $event =~ /(?:started|stopped)$/
!ll&&!rl&&r
901
MC/DC66%2/3
index($event, "_") == 0 or index($event, "emitter_") == 0 and $event =~ /(?:started|stopped)$/
index($event, "_") == 0 index($event, "emitter_") == 0 $event =~ /(?:started|stopped)$/
Truth table75%3/4
A or B and C
A= index($event, "_") == 0B= index($event, "emitter_") == 0C= $event =~ /(?:started|stopped)$/
ABCresult
1XX1
0111
00X0
0100
519 unless index($event, '_') == 0
520 or index($event, 'emitter_') == 0
521 and $event =~ /(?:started|stopped)$/;
522 }
523  
524 4 ▶sub __emitter_sig_shutdown {
Subroutine __emitter_sig_shutdown: called CC=1 SCAR=0.0
525 4 my ($kernel, $self) = @_[KERNEL, OBJECT];
526 4 $self->yield( shutdown_emitter => @_[ARG2 .. $#_] )
527 }
528  
529 0 ▶sub __emitter_sigdie {
Subroutine __emitter_sigdie: not called CC=1 SCAR=6.9
530 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
531 0 my $exh = $_[ARG1];
532  
533 0 my $event = $exh->{event};
534 0 my $dest_id = $exh->{dest_session}->ID;
535 0 my $errstr = $exh->{error_str};
536  
537 0 warn
538 "SIG_DIE: Event '$event' session '$dest_id'\n",
539 " exception: $errstr\n";
540  
541 0 $kernel->sig_handled;
542 }
543  
544 3 ▶sub __emitter_stop {
Subroutine __emitter_stop: called CC=1 SCAR=0.0
545 ## _stop handler
546 3 my ($kernel, $self) = @_[KERNEL, OBJECT];
547  
548 3 $self->call('emitter_stopped');
549 }
550  
551 1 ▶sub _shutdown_emitter {
Subroutine _shutdown_emitter: called CC=1 SCAR=0.0
552 ## Opposite of _start_emitter
553 1 my $self = shift;
554  
555 1 $self->call( shutdown_emitter => @_ );
556  
557 1 1
558 }
559  
560 5 ▶sub __shutdown_emitter {
Subroutine __shutdown_emitter: called CC=1 SCAR=0.0
561 5 my ($kernel, $self) = @_[KERNEL, OBJECT];
562  
563 5 $kernel->alarm_remove_all;
564  
565 ## Destroy plugin pipeline.
566 5 $self->_pluggable_destroy;
567  
568 ## Notify sessions.
569 5 $self->emit( shutdown => @_[ARG0 .. $#_] );
570  
571 ## Drop sessions and we're spent.
572 5 $self->call( unsubscribe => () );
573 5 $self->__emitter_drop_sessions;
574 }
575  
576  
577 ## Handlers for listener sessions.
578 4 ▶sub __emitter_register {
Subroutine __emitter_register: called CC=5 SCAR=16.2
579 4 my ($kernel, $self, $sender) = @_[KERNEL, OBJECT, SENDER];
580 4 my @events = @_[ARG0 .. $#_];
581  
582 4 ▶ @events = 'all' unless @events;
Branch: 1 branch
BranchTrueFalseTotal
unless @events 2 2 4
583  
584 4 my $s_id = $sender->ID;
585  
586 ## Add to our known sessions.
587 4 $self->__reg_ses_id( $s_id );
588  
589 4 for my $event (@events) {
590 ## Add session to registered event lists.
591 4 $self->__emitter_reg_events->{$event}->{$s_id} = 1;
592  
593 ## Make sure registered session hangs around
594 ## (until _unregister or shutdown)
595 4 ▶ $kernel->refcount_increment( $s_id, E_TAG )
Branch: 1 branch
BranchTrueFalseTotal
unless $s_id == $self->session_id or $self->__get_ses_refc($s_id) 0 4 4
Condition66%2/3
$s_id == $self->session_id or $self->__get_ses_refc($s_id)
l!l&&r!l&&!r
130
MC/DC0%0/2
$s_id == $self->session_id or $self->__get_ses_refc($s_id)
$s_id == $self->session_id $self->__get_ses_refc($s_id)
Truth table66%2/3
A or B
A= $s_id == $self->session_idB= $self->__get_ses_refc($s_id)
ABresult
1X1
011
000
596 unless $s_id == $self->session_id
597 or $self->__get_ses_refc($s_id);
598  
599 4 $self->__incr_ses_refc( $s_id );
600 }
601  
602 4 $kernel->post( $s_id => $self->event_prefix . 'registered', $self )
603 }
604  
605 5 ▶sub __emitter_unregister {
Subroutine __emitter_unregister: called CC=7 SCAR=19.9
606 5 my ($kernel, $self, $sender) = @_[KERNEL, OBJECT, SENDER];
607 5 my @events = @_[ARG0 .. $#_];
608  
609 ## - An unsub without any arguments means "stop sending all events I
610 ## have subscribed to"
611 ## - An unsub for 'all' means "stop sending events I haven't asked for
612 ## by name"
613  
614 5 ▶ @events = $self->__emitter_reg_events->keys->all unless @events;
Branch: 1 branch
BranchTrueFalseTotal
unless @events 5 0 5
615  
616 5 my $s_id = $sender->ID;
617  
618 5 EV: for my $event (@events) {
619 # intentional no Lowu, leave me for autoviv:
620 5 ▶ unless (delete $self->__emitter_reg_events->{$event}->{$s_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $self->__emitter_reg_events->{$event}{$s_id}) 4 1 5
621 next EV
622 4 }
623  
624 # Sessions left for this event?
625 1 ▶ $self->__emitter_reg_events->delete($event)
Branch: 1 branch
BranchTrueFalseTotal
if $self->__emitter_reg_events->get($event)->is_empty 0 1 1
626 if $self->__emitter_reg_events->get($event)->is_empty;
627  
628 1 $self->__decr_ses_refc($s_id);
629  
630 1 ▶ unless ($self->__get_ses_refc($s_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->__get_ses_refc($s_id)) 1 0 1
631 ## No events left for this session.
632 1 $self->__emitter_reg_sessions->delete($s_id);
633  
634 1 ▶ $kernel->refcount_decrement( $s_id, E_TAG )
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] == $sender 0 1 1
635 unless $_[SESSION] == $sender;
636 }
637  
638 } ## EV
639 }
640  
641 2 1;
642  
643  
644 =pod
645  
646 =for Pod::Coverage has_\S+
647  
648 =head1 NAME
649  
650 MooX::Role::POE::Emitter - Pluggable POE event emitter role for cows
651  
652 =head1 SYNOPSIS
653  
654 ## A POE::Session that can broadcast events to listeners:
655 package My::EventEmitter;
656 use POE;
657 use Moo;
658 with 'MooX::Role::POE::Emitter';
659  
660 sub spawn {
661 my ($self, %args) = @_;
662  
663 $self->set_object_states(
664 [
665 $self => {
666 ## Add some extra handlers to our Emitter:
667 'emitter_started' => '_emitter_started',
668 'emitter_stopped' => '_emitter_stopped',
669 },
670  
671 ## Include any object_states we had previously
672 ## (e.g. states added at construction time):
673 (
674 $self->has_object_states ?
675 @{ $self->object_states } : ()
676 ),
677  
678 ## Maybe include from named arguments, for example:
679 (
680 ref $args{object_states} eq 'ARRAY' ?
681 @{ $args{object_states} } : ()
682 ),
683 ],
684 );
685  
686 ## Start our Emitter's POE::Session:
687 $self->_start_emitter;
688 }
689  
690 sub shutdown {
691 my ($self) = @_;
692 ## .. do some cleanup, whatever ..
693 $self->_shutdown_emitter;
694 }
695  
696 sub _emitter_started {
697 my ($kernel, $self) = @_[KERNEL, OBJECT];
698 ## A POE state called when the emitter's session starts.
699 ## (Analogous to a normal '_start' handler)
700 ## Could load plugins, do initialization, etc.
701 }
702  
703 sub _emitter_stopped {
704 ## Opposite of 'emitter_started'
705 }
706  
707 sub do_something {
708 my ($self, @things) = @_;
709 # ... do some work ...
710 # ... emit an event:
711 $self->emit( did_stuff => @things )
712 }
713  
714 ## A listening POE::Session:
715 package My::Listener;
716 use POE;
717  
718 sub spawn {
719 # This spawn() takes an alias/session to subscribe to:
720 my ($self, $alias_or_sessionID) = @_;
721  
722 POE::Session->create(
723 ## Set up a Session, etc
724 object_states => [
725 $self => [
726 'emitted_did_stuff',
727 # ...
728 ],
729 ],
730 );
731  
732 ## Subscribe to all events from $alias_or_sessionID:
733 $poe_kernel->call(
734 $alias_or_sessionID => subscribe => 'all'
735 );
736 }
737  
738 sub emitted_did_stuff {
739 my ($kernel, $self) = @_[KERNEL, OBJECT];
740 ## Received 'did_stuff' from Emitter
741 my @things = @_[ARG0 .. $#_];
742 # ...
743 }
744  
745 =head1 DESCRIPTION
746  
747 Consuming this L<Moo::Role> gives your class a L<POE::Session> capable of
748 processing events via loaded plugins and/or emitting them to registered
749 "listener" sessions.
750  
751 It is derived from L<POE::Component::Syndicator> by BINGOS, HINRIK, APOCAL
752 et al, but with more cows ;-) and a few extra features (such as anonymous
753 coderef callbacks; see L</yield>), as well as the
754 faster plugin dispatch system that comes with L<MooX::Role::Pluggable>.
755  
756 The Emitter role consumes L<MooX::Role::Pluggable>,
757 making your emitter pluggable (see the
758 L<MooX::Role::Pluggable> documentation for plugin-related details).
759  
760 You do not need to create your own L<POE::Session>; calling
761 L</_start_emitter> will spawn one for you.
762  
763 You also get some useful sugar over POE event dispatch; see L</Methods>.
764  
765 =head2 Creating an Emitter
766  
767 L</SYNOPSIS> contains an emitter that uses B<set_$attrib> methods to
768 configure itself when C<spawn()> is called; attributes can, of course,
769 be set when your Emitter is constructed:
770  
771 my $emitter = MyEmitter->new(
772 alias => 'my_emitter',
773 pluggable_type_prefixes => {
774 NOTIFY => 'Notify',
775 PROCESS => 'Proc',
776 },
777 # . . .
778 );
779  
780 =head3 Attributes
781  
782 Most of these can be altered via B<set_$attrib> methods at any time before
783 L</_start_emitter> is called. Changing an emitter's configuration after it has
784 been started may result in undesirable behavior ;-)
785  
786 Public attributes provide B<has_> prefixed predicates; e.g.
787 B<has_event_prefix>.
788  
789 =head4 alias
790  
791 B<alias> specifies the POE::Kernel alias used for our L<POE::Session>;
792 defaults to the stringified object.
793  
794 Set via B<set_alias>. If the emitter is running, a prefixed B<alias_set>
795 event is emitted to notify listeners that need to know where to reach the emitter.
796  
797 =head4 event_prefix
798  
799 B<event_prefix> is prepended to notification events before they are
800 dispatched to listening sessions. It is also used for the plugin
801 pipeline's internal events; see L<MooX::Role::Pluggable/_pluggable_event>
802 for details.
803  
804 Defaults to C<emitted_>
805  
806 Set via B<set_event_prefix>
807  
808 =head4 pluggable_type_prefixes
809  
810 B<pluggable_type_prefixes> is a hash reference that can optionally be set
811 to change the default L<MooX::Role::Pluggable> plugin handler prefixes for
812 C<PROCESS> and C<NOTIFY> (which default to C<P> and C<N>, respectively):
813  
814 my $emitter = $class->new(
815 pluggable_type_prefixes => {
816 PROCESS => 'P',
817 NOTIFY => 'N',
818 },
819 );
820  
821 Set via B<set_pluggable_type_prefixes>
822  
823 =head4 object_states
824  
825 B<object_states> is an array reference suitable for passing to
826 L<POE::Session>; the subclasses own handlers should be added to
827 B<object_states> prior to calling L</_start_emitter>.
828  
829 Set via B<set_object_states>
830  
831 =head4 register_prefix
832  
833 B<register_prefix> is prepended to 'register' and 'unregister' methods
834 called on plugins at load time (see L<MooX::Role::Pluggable>).
835  
836 Defaults to I<Emitter_>
837  
838 Set via B<set_register_prefix>
839  
840 =head4 session_id
841  
842 B<session_id> is our emitter's L<POE::Session> ID, set when our Session is
843 started via L</"_start_emitter">.
844  
845 =head4 shutdown_signal
846  
847 B<shutdown_signal> is the name of the L<POE> signal that will trigger a
848 shutdown (used to shut down multiple Emitters). See L</"Signals">
849  
850 =head3 _start_emitter
851  
852 B<_start_emitter()> should be called on our object to spawn the actual
853 L<POE::Session>. It takes no arguments and should be called after the
854 object has been configured.
855  
856 =head3 _shutdown_emitter
857  
858 B<_shutdown_emitter()> must be called to terminate the Emitter's
859 L<POE::Session>
860  
861 A 'shutdown' event will be emitted before sessions are dropped.
862  
863 =head2 Listening sessions
864  
865 =head3 Session event subscription
866  
867 An external L<POE::Session> can subscribe to receive events via
868 normal POE event dispatch by sending a C<subscribe>:
869  
870 $poe_kernel->post( $emitter->session_id,
871 'subscribe',
872 @events
873 );
874  
875 Listening sessions are consumers; they cannot modify event arguments in
876 any meaningful way, and will receive arguments as-normal (in @_[ARG0 ..
877 $#_] like any other POE state). Plugins operate differently and receive
878 references to arguments that can be modified -- see
879 L<MooX::Role::Pluggable> for details.
880  
881 =head3 Session event unregistration
882  
883 An external Session can unregister subscribed events using the same syntax
884 as above:
885  
886 $poe_kernel->post( $emitter->session_id,
887 'unsubscribe',
888 @events
889 );
890  
891 If no events are specified, then any previously subscribed events are
892 unregistered.
893  
894 Note that unsubscribing from 'all' does not carry the same behavior; that
895 is to say, a subscriber can subscribe/unsubscribe for 'all' separately from
896 some set of specifically named events.
897  
898 =head2 Receiving events
899  
900 =head3 Events delivered to listeners
901  
902 Events are delivered to subscribed listener sessions as normal POE events,
903 with the configured L</event_prefix> prepended and arguments available via
904 C< @_[ARG0 .. $#_] > as normal.
905  
906 sub emitted_my_event {
907 my ($kernel, $self) = @_[KERNEL, OBJECT];
908 my @args = @_[ARG0 .. $#_];
909 # . . .
910 }
911  
912 See L</"Session event subscription"> and L</"emit">
913  
914 =head3 Events delivered to this session
915  
916 The emitter's L<POE::Session> provides a '_default' handler that
917 redispatches unknown POE-delivered events to L</process>
918 (except for events prefixed with '_', which are reserved).
919  
920 You can change this behavior by overriding '_emitter_default' -- here's a
921 direct adaption of the example from L<POE::Component::Syndicator>:
922  
923 use Moo;
924 use POE;
925 with 'MooX::Role::POE::Emitter';
926 around '_emitter_default' => sub {
927 my $orig = shift;
928 my ($kernel, $self) = @_[KERNEL, OBJECT];
929 my ($event, $args) = @_[ARG0, ARG1];
930  
931 ## process(), then do something else, for example
932 return if $self->process( $event, @$args ) == EAT_ALL;
933  
934 . . .
935 };
936  
937 (Note that due to internal redispatch $_[SENDER] will be the Emitter's
938 Session.)
939  
940 =head2 EAT values
941  
942 L<MooX::Role::Pluggable> uses C<EAT_*> constants to indicate event
943 lifetime.
944  
945 If a plugin in the pipeline returns EAT_CLIENT or EAT_ALL, events
946 are not dispatched to subscribed listening sessions; a dispatched NOTIFY
947 event goes to your emitter's Session if it is subscribed to receive it,
948 then to the plugin pipeline, and finally to other subscribed listener
949 Sessions B<unless> a plugin returned EAT_CLIENT or EAT_ALL.
950  
951 See L</"emit"> for more on dispatch behavior and event lifetime. See
952 L<MooX::Role::Pluggable> for details regarding plugins.
953  
954 =head3 NOTIFY events
955  
956 B<NOTIFY> events are intended to be dispatched asynchronously to our own
957 session, any loaded plugins in the pipeline, and subscribed listening
958 sessions, respectively.
959  
960 See L</emit>.
961  
962 =head3 PROCESS events
963  
964 B<PROCESS> events are intended to be processed by the plugin pipeline
965 immediately; these are intended for message processing and similar
966 synchronous action handled by plugins.
967  
968 Handlers for B<PROCESS> events are prefixed with C<P_>
969  
970 See L</process>.
971  
972 =head2 Sending events
973  
974 =head3 emit
975  
976 $self->emit( $event, @args );
977  
978 B<emit()> dispatches L</"NOTIFY events"> -- these events are dispatched
979 first to our own session (with L</event_prefix> prepended), then any
980 loaded plugins in the pipeline (with C<N_> prepended), then registered
981 sessions (with L</event_prefix> prepended):
982  
983 ## With default event_prefix:
984 $self->emit( 'my_event', @args )
985 # -> Dispatched to own session as 'emitted_my_event'
986 # -> Dispatched to plugin pipeline as 'N_my_event'
987 # -> Dispatched to registered sessions as 'emitted_my_event'
988 # *unless* a plugin returned EAT_CLIENT or EAT_ALL
989  
990 See L</"Receiving events">, L</"EAT values">
991  
992 =head3 emit_now
993  
994 $self->emit_now( $event, @args );
995  
996 B<emit_now()> synchronously dispatches L</"NOTIFY events"> -- see
997 L</emit>.
998  
999 =head3 process
1000  
1001 $self->process( $event, @args );
1002  
1003 B<process()> calls registered plugin handlers for L</"PROCESS events">
1004 immediately; these are B<not> dispatched to listening sessions.
1005  
1006 Returns the same value as L<MooX::Role::Pluggable/"_pluggable_process">.
1007  
1008 See L<MooX::Role::Pluggable> for details on pluggable
1009 event dispatch.
1010  
1011 =head2 Methods
1012  
1013 These methods provide easy proxy mechanisms for issuing POE events and
1014 managing timers within the context of the emitter's L<POE::Session>.
1015  
1016 =head3 yield
1017  
1018 $self->yield( $poe_event, @args );
1019  
1020 Provides an interface to L<POE::Kernel>'s yield/post() method, dispatching
1021 POE events within the context of the emitter's session.
1022  
1023 The event can be either a named event/state dispatched to your Emitter's
1024 L<POE::Session>:
1025  
1026 $emitter->yield( 'some_event', @args );
1027  
1028 ... or an anonymous coderef, which is executed as if it were a named
1029 POE state belonging to your Emitter:
1030  
1031 $emitter->yield( sub {
1032 ## $_[OBJECT] is the Emitter's object:
1033 my ($kernel, $self) = @_[KERNEL, OBJECT];
1034 my @params = @_[ARG0 .. $#_];
1035  
1036 ## $_[STATE] is the current coderef
1037 ## Yield ourselves again, for example:
1038 $self->yield( $_[STATE], @new_args )
1039 if $some_condition;
1040 }, $some, $args );
1041  
1042 Inside an anonymous coderef callback such as shown above, C<$_[OBJECT]> is
1043 the Emitter's C<$self> object and C<$_[STATE]> contains the callback
1044 coderef itself.
1045  
1046 =head3 call
1047  
1048 $self->call( $poe_event, @args );
1049  
1050 The synchronous counterpart to L</yield>.
1051  
1052 =head3 timer
1053  
1054 my $alarm_id = $self->timer(
1055 $delayed_seconds,
1056 $event,
1057 @args
1058 );
1059  
1060 Set a timer in the context of the emitter's L<POE::Session>. Returns the
1061 POE alarm ID.
1062  
1063 The event can be either a named event/state or an anonymous coderef (see
1064 L</yield>).
1065  
1066 A prefixed (L</event_prefix>) 'timer_set' event is emitted when a timer is
1067 set. Arguments are the alarm ID, the event name or coderef, the delay time,
1068 and any event parameters, respectively.
1069  
1070 =head3 timer_del
1071  
1072 $self->timer_del( $alarm_id );
1073  
1074 Clears a pending L</timer>.
1075  
1076 A prefixed (L</event_prefix>) 'timer_deleted' event is emitted when a timer
1077 is deleted. Arguments are the removed alarm ID, the event name or coderef,
1078 and any event parameters, respectively.
1079  
1080 =head2 Signals
1081  
1082 =head3 Shutdown Signal
1083  
1084 The attribute L</shutdown_signal> defines a POE signal that will trigger a
1085 shutdown; it defaults to C<SHUTDOWN_EMITTER>:
1086  
1087 ## Shutdown *all* emitters (with a default shutdown_signal()):
1088 $poe_kernel->signal( $poe_kernel, 'SHUTDOWN_EMITTER' );
1089  
1090 See L<POE::Kernel/"Signal Watcher Methods"> for details on L<POE> signals.
1091  
1092 =head1 SEE ALSO
1093  
1094 For details regarding POE, see L<POE>, L<POE::Kernel>, L<POE::Session>
1095  
1096 For details regarding Moo classes and Roles, see L<Moo>, L<Moo::Role>,
1097 L<Role::Tiny>
1098  
1099 =head1 AUTHOR
1100  
1101 Jon Portnoy <avenj@cobaltirc.org>
1102  
1103 Written from the ground up, but conceptually derived from
1104 L<POE::Component::Syndicator>-0.06 copyright Hinrik Orn Sigurosson (HINRIK),
1105 Chris Williams (BINGOS), APOCAL et al -- that will probably do you for
1106 non-Moo(se) use cases; I needed something cow-like that worked with
1107 L<MooX::Role::Pluggable>.
1108  
1109 Licensed under the same terms as Perl 5; see the license that came with your
1110 Perl distribution for details.
1111  
1112 =cut
1113  
↑ Summary