NAME
On::Event::Timer - Timer/timeout events for On::Event
VERSION
version v0.1.1
SYNOPSIS
use On::Event::Timer qw( sleep sleep_until );
# After five seconds, say Hi
On::Event::Timer->after( 5, sub { say "Hi!" } );
sleep 3; # Sleep for 3 seconds without blocking events from firing
# Two seconds from now, say At!
On::Event::Timer->at( time()+2, sub { say "At!" } );
# Every 5 seconds, starting 5 seconds from now, say Ping
On::Event::Timer->every( 5, sub { say "Ping" } );
sleep_until time()+10; # Sleep until 10 seconds from now
DESCRIPTION
Trigger events at a specific time or after a specific delay.
HELPERS
- our sub sleep( Rat $secs ) is export
-
Sleep for $secs while allowing events to emit (and Coroutine threads to run)
- our sub sleep_until( Rat $epochtime ) is export
-
Sleep until $epochtime while allowing events to emit (and Coroutine threads to run)
CLASS METHODS
- our method after( Rat $seconds, CodeRef $on_timeout ) returns On::Event::Timer
-
Asynchronously, after $seconds, calls $on_timeout. If you store the return value, it acts as a guard-- if it's destroyed then the timer is canceled.
- our method at( Rat $epochtime, CodeRef $on_timeout ) returns On::Event::Timer
-
Asychronously waits until $epochtime and then calls $on_timeout. If you store the return value, it acts as a guard-- if it's destoryed then the timer is canceled.
- our method every( Rat $seconds, CodeRef $on_timeout ) returns On::Event::Timer
-
Asychronously, after $seconds and every $seconds there after, calls $on-Timeout. If you store the return value it acts as a guard-- if it's destroyed then the timer is canceled.
- our method new( :$delay, :$interval? ) returns On::Event::Timer
-
Creates a new timer object that will emit it's "timeout" event after $delay seconds and every $interval seconds there after. Delay can be a code ref, in which case it's return value is the number of seconds to delay.
METHODS
- our method on( Str $event, CodeRef $listener ) returns CodeRef
-
Registers $listener as a listener on $event. When $event is emitted ALL registered listeners are executed.
Returns the listener coderef.
- our method emit( Str $event, Array[Any] *@args )
-
Normally called within the class using the On::Event role. This calls all of the registered listeners on $event with @args.
If you're using coroutines then each listener will get its own thread and emit will cede before returning.
- our method remove_all_listeners( Str $event )
-
Removes all listeners for $event
- our method start( $is_obj_guard = False )
-
Starts the timer object running. If $is_obj_guard is true, then destroying the object will cancel the timer.
- our method cancel()
-
Cancels a running timer. You can start the timer again by calling the start method. For after and every timers, it begins waiting all over again. At timers will still emit at the time you specified (or immediately if that time has passed).
EVENTS
- timeout
-
This event takes no arguments. It's emitted when the event time completes.
SEE ALSO
AUTHOR
Becca <becca@referencethis.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Rebecca Turner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.