File Coverage

blib/lib/UAV/Pilot/Events.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package UAV::Pilot::Events;
2 2     2   33618 use v5.14;
  2         9  
  2         94  
3 2     2   6973 use Moose;
  0            
  0            
4             use namespace::autoclean;
5             use AnyEvent;
6             use UAV::Pilot::EventHandler;
7              
8              
9             use constant TIMER_INTERVAL => 1 / 60;
10              
11              
12             has 'condvar' => (
13             is => 'ro',
14             isa => 'AnyEvent::CondVar',
15             );
16             has '_handlers' => (
17             traits => ['Array'],
18             is => 'ro',
19             isa => 'ArrayRef[UAV::Pilot::EventHandler]',
20             default => sub {[]},
21             handles => {
22             register => 'push',
23             },
24             );
25              
26              
27             sub init_event_loop
28             {
29             my ($self) = @_;
30              
31             my $timer; $timer = AnyEvent->timer(
32             after => 1,
33             interval => $self->TIMER_INTERVAL,
34             cb => sub {
35             $_->process_events for @{ $self->_handlers };
36             $timer;
37             },
38             );
39              
40             return 1;
41             }
42              
43              
44             no Moose;
45             __PACKAGE__->meta->make_immutable;
46             1;
47             __END__
48              
49              
50             =head1 NAME
51              
52             UAV::Pilot::Events
53              
54             =head1 SYNOPSIS
55              
56             my $condvar = AnyEvent->condvar;
57             my $events = UAV::Pilot::Events->new({
58             condvar => $condvar,
59             });
60             $events->register( ... );
61             $events->init_event_loop;
62             $condvar->recv;
63              
64             =head1 DESCRIPTION
65              
66             Handles event loops on a regular timer.
67              
68             =head1 METHODS
69              
70             =head2 new
71              
72             new({
73             condvar => $cv,
74             })
75              
76             Constructor. The C<condvar> argument is an C<AnyEvent::Condvar>.
77              
78             =head2 register
79              
80             register( $event_handler )
81              
82             Adds a object that does the C<UAV::Pilot::EventHandler> role to the list. The
83             C<process_events> method on that object will be called each time the event loop runs.
84              
85             =head2 init_event_loop
86              
87             Sets up the event loop. Note that you must still call C<recv> on the C<AnyEvent::Condvar>
88             to start the loop running.
89              
90             =cut