From 1425eea04dd872dc6313f5315f317b2de288037c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Mon, 1 Jun 2015 14:15:30 +0000 Subject: IO-Async-0.67 --- lib/IO/Async/Future.pm | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 lib/IO/Async/Future.pm (limited to 'lib/IO/Async/Future.pm') diff --git a/lib/IO/Async/Future.pm b/lib/IO/Async/Future.pm new file mode 100644 index 0000000..5bf8395 --- /dev/null +++ b/lib/IO/Async/Future.pm @@ -0,0 +1,150 @@ +# You may distribute under the terms of either the GNU General Public License +# or the Artistic License (the same terms as Perl itself) +# +# (C) Paul Evans, 2013 -- leonerd@leonerd.org.uk + +package IO::Async::Future; + +use strict; +use warnings; + +our $VERSION = '0.67'; + +use base qw( Future ); +Future->VERSION( '0.05' ); # to respect subclassing + +use Carp; + +=head1 NAME + +C - use L with L + +=head1 SYNOPSIS + + use IO::Async::Loop; + + my $loop = IO::Async::Loop->new; + + my $future = $loop->new_future; + + $loop->watch_time( after => 3, code => sub { $future->done( "Done" ) } ); + + print $future->get, "\n"; + +=head1 DESCRIPTION + +This subclass of L stores a reference to the L +instance that created it, allowing the C method to block until the +Future is ready. These objects should not be constructed directly; instead +the C method on the containing Loop should be used. + +For a full description on how to use Futures, see the L documentation. + +=cut + +=head1 CONSTRUCTORS + +New C objects should be constructed by using the following +methods on the C. For more detail see the L +documentation. + +=head2 $future = $loop->new_future + +Returns a new pending Future. + +=head2 $future = $loop->delay_future( %args ) + +Returns a new Future that will become done at a given time. + +=head2 $future = $loop->timeout_future( %args ) + +Returns a new Future that will become failed at a given time. + +=cut + +sub new +{ + my $proto = shift; + my $self = $proto->SUPER::new; + + if( ref $proto ) { + $self->{loop} = $proto->{loop}; + } + else { + $self->{loop} = shift; + } + + return $self; +} + +=head1 METHODS + +=cut + +=head2 $loop = $future->loop + +Returns the underlying C object. + +=cut + +sub loop +{ + my $self = shift; + return $self->{loop}; +} + +sub await +{ + my $self = shift; + $self->{loop}->loop_once; +} + +=head2 $future->done_later( @result ) + +A shortcut to calling the C method in a C idle watch on the +underlying Loop object. Ensures that a returned Future object is not ready +immediately, but will wait for the next IO round. + +Like C, returns C<$future> itself to allow easy chaining. + +=cut + +sub done_later +{ + my $self = shift; + my @result = @_; + + $self->loop->later( sub { $self->done( @result ) } ); + + return $self; +} + +=head2 $future->fail_later( $exception, @details ) + +A shortcut to calling the C method in a C idle watch on the +underlying Loop object. Ensures that a returned Future object is not ready +immediately, but will wait for the next IO round. + +Like C, returns C<$future> itself to allow easy chaining. + +=cut + +sub fail_later +{ + my $self = shift; + my ( $exception, @details ) = @_; + + $exception or croak "Expected a true exception"; + + $self->loop->later( sub { $self->fail( $exception, @details ) } ); + + return $self; +} + +=head1 AUTHOR + +Paul Evans + +=cut + +0x55AA; -- cgit v1.2.1