File Coverage

blib/lib/UAV/Pilot/Events.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 37 37 100.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2015 Timm Murray
2             # All rights reserved.
3             #
4             # Redistribution and use in source and binary forms, with or without
5             # modification, are permitted provided that the following conditions are met:
6             #
7             # * Redistributions of source code must retain the above copyright notice,
8             # this list of conditions and the following disclaimer.
9             # * Redistributions in binary form must reproduce the above copyright
10             # notice, this list of conditions and the following disclaimer in the
11             # documentation and/or other materials provided with the distribution.
12             #
13             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23             # POSSIBILITY OF SUCH DAMAGE.
24             package UAV::Pilot::Events;
25             $UAV::Pilot::Events::VERSION = '1.3';
26 2     2   22803 use v5.14;
  2         7  
27 2     2   915 use Moose;
  2         473292  
  2         13  
28 2     2   14357 use namespace::autoclean;
  2         8360  
  2         16  
29 2     2   3203 use AnyEvent;
  2         11670  
  2         62  
30 2     2   567 use UAV::Pilot::EventHandler;
  2         5  
  2         67  
31              
32              
33 2     2   15 use constant TIMER_INTERVAL => 1 / 60;
  2         4  
  2         437  
34              
35              
36             has 'condvar' => (
37             is => 'ro',
38             isa => 'AnyEvent::CondVar',
39             );
40             has '_handlers' => (
41             traits => ['Array'],
42             is => 'ro',
43             isa => 'ArrayRef[UAV::Pilot::EventHandler]',
44             default => sub {[]},
45             handles => {
46             register => 'push',
47             },
48             );
49              
50              
51             sub init_event_loop
52             {
53 1     1 1 7 my ($self) = @_;
54              
55 1         2 my $timer; $timer = AnyEvent->timer(
56             after => 1,
57             interval => $self->TIMER_INTERVAL,
58             cb => sub {
59 1     1   990230 $_->process_events for @{ $self->_handlers };
  1         136  
60 1         111 $timer;
61             },
62 1         14 );
63              
64 1         26 return 1;
65             }
66              
67              
68 2     2   21 no Moose;
  2         5  
  2         14  
69             __PACKAGE__->meta->make_immutable;
70             1;
71             __END__
72              
73              
74             =head1 NAME
75              
76             UAV::Pilot::Events
77              
78             =head1 SYNOPSIS
79              
80             my $condvar = AnyEvent->condvar;
81             my $events = UAV::Pilot::Events->new({
82             condvar => $condvar,
83             });
84             $events->register( ... );
85             $events->init_event_loop;
86             $condvar->recv;
87              
88             =head1 DESCRIPTION
89              
90             Handles event loops on a regular timer.
91              
92             =head1 METHODS
93              
94             =head2 new
95              
96             new({
97             condvar => $cv,
98             })
99              
100             Constructor. The C<condvar> argument is an C<AnyEvent::Condvar>.
101              
102             =head2 register
103              
104             register( $event_handler )
105              
106             Adds a object that does the C<UAV::Pilot::EventHandler> role to the list. The
107             C<process_events> method on that object will be called each time the event loop runs.
108              
109             =head2 init_event_loop
110              
111             Sets up the event loop. Note that you must still call C<recv> on the C<AnyEvent::Condvar>
112             to start the loop running.
113              
114             =cut