NAME
UAV::Pilot::EasyEvent
SYNOPSIS
my $cv = AnyEvent->condvar;
my $event = UAV::Pilot::EasyEvent->new({
condvar => $cv,
});
my @events;
my $event2 = $event->add_timer({
duration => 100,
duration_units => $event->UNITS_MILLISECOND,
cb => sub {
push @events => 'First event',
},
})->add_timer({
duration => 10,
duration_units => $event->UNITS_MILLISECOND,
cb => sub {
push @events => 'Second event',
},
});
$event2->add_timer({
duration => 50,
duration_units => $event->UNITS_MILLISECOND,
cb => sub {
push @events => 'Fourth event',
$cv->send;
},
});
$event2->add_timer({
duration => 10,
duration_units => $event->UNITS_MILLISECOND,
cb => sub {
push @events => 'Third event',
},
});
$event->init_event_loop;
$cv->recv;
# After time passes, prints:
# First event
# Second event
# Third event
# Fourth event
#
say $_ for @events;
DESCRIPTION
AnyEvent
is the standard event framework used for UAV::Pilot
. However, its interface isn't convenient for some of the typical things done for UAV piloting. For instance, to put the code into plain English, we might want to say:
Takeoff, wait 5 seconds, then pitch forward for 2 seconds, then pitch backwards
for 2 seconds, then land
In the usual AnyEvent
interface, this requires building the timers inside the callbacks of other timers, which leads to several levels of indentation. UAV::Pilot::EasyEvent
simplifies the handling and syntax of this kind of event workflow.
METHODS
new
new({
condvar => $cv,
})
Constructor. The condvar
argument should be an AnyEvent::CondVar
.
add_timer
add_timer({
duration => 100,
duration_units => $event->UNITS_MILLISECOND,
cb => sub { ... },
})
Add a timer to run in the event loop. It will run after duration
units of time, with the units specified by duration_units
. The cb
parameter is a reference to a subroutine to use as a callback.
Returns a child EasyEvent
object. When the timer above has finished, any timers on child objects will be setup for execution. This makes it easy to chain timers to run after each other.
init_event_loop
This method must be called after running a series of add_timer()
calls. You only need to call this on the root object, not the children.
You must call recv
on the condvar
yourself.
add_event
add_event( 'foo', sub {...}, 0 )
Add a subref that will be called when the named event is fired off. The first parameter is the name of the event, and the second is the subref.
The third is optional, and specifies if the call will be a "one-off" or not. If it's a one-off, then after the first call to the sub, it will be removed from list of callbacks. Defaults to false.
The callback will receive the arguments that were passed to send_event()
when the event is triggered.
send_event
send_event( 'foo', @args )
Trigger an event with the given name. The first arg is the name of the event. All subsequent args will be passed to the callbacks attached to that event name.