Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Timer/Countdown.pm view on Meta::CPAN
=head2 on_expire
Invoked when the timer expires.
=cut
=head1 PARAMETERS
The following named parameters may be passed to C<new> or C<configure>:
=head2 on_expire => CODE
CODE reference for the C<on_expire> event.
=head2 delay => NUM
The delay in seconds after starting the timer until it expires. Cannot be
changed if the timer is running. A timer with a zero delay expires
"immediately".
=head2 remove_on_expire => BOOL
Optional. If true, remove this timer object from its parent notifier or
containing loop when it expires. Defaults to false.
Once constructed, the timer object will need to be added to the C<Loop> before
it will work. It will also need to be started by the C<start> method.
=cut
sub configure
{
my $self = shift;
my %params = @_;
foreach (qw( remove_on_expire )) {
$self->{$_} = delete $params{$_} if exists $params{$_};
}
if( exists $params{on_expire} ) {
my $on_expire = delete $params{on_expire};
ref $on_expire or croak "Expected 'on_expire' as a reference";
$self->{on_expire} = $on_expire;
undef $self->{cb}; # Will be lazily constructed when needed
}
if( exists $params{delay} ) {
$self->is_running and croak "Cannot configure 'delay' of a running timer\n";
my $delay = delete $params{delay};
$delay >= 0 or croak "Expected a 'delay' as a non-negative number";
$self->{delay} = $delay;
}
unless( $self->can_event( 'on_expire' ) ) {
croak 'Expected either a on_expire callback or an ->on_expire method';
}
$self->SUPER::configure( %params );
}
=head1 METHODS
=cut
=head2 is_expired
$expired = $timer->is_expired
Returns true if the Timer has already expired.
=cut
sub is_expired
{
my $self = shift;
return $self->{expired};
}
sub _make_cb
{
my $self = shift;
return $self->_capture_weakself( sub {
my $self = shift or return;
undef $self->{id};
$self->{expired} = 1;
$self->remove_from_parent if $self->{remove_on_expire};
$self->invoke_event( "on_expire" );
} );
}
sub _make_enqueueargs
{
my $self = shift;
undef $self->{expired};
return after => $self->{delay};
}
=head2 reset
$timer->reset
If the timer is running, restart the countdown period from now. If the timer
is not running, this method has no effect.
=cut
sub reset
{
my $self = shift;
my $loop = $self->loop or croak "Cannot reset a Timer that is not in a Loop";
return if !$self->is_running;
( run in 1.914 second using v1.01-cache-2.11-cpan-13bb782fe5a )