File Coverage

blib/lib/IO/Async/Future.pm
Criterion Covered Total %
statement 33 33 100.0
branch 3 4 75.0
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 52 53 98.1


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2013 -- leonerd@leonerd.org.uk
5              
6             package IO::Async::Future;
7              
8 47     47   12949 use strict;
  47         91  
  47         1838  
9 47     47   288 use warnings;
  47         158  
  47         2936  
10              
11             our $VERSION = '0.79';
12              
13 47     47   361 use base qw( Future );
  47         175  
  47         15496  
14             Future->VERSION( '0.05' ); # to respect subclassing
15              
16 47     47   155335 use Carp;
  47         108  
  47         17220  
17              
18             =head1 NAME
19              
20             C - use L with L
21              
22             =head1 SYNOPSIS
23              
24             use IO::Async::Loop;
25              
26             my $loop = IO::Async::Loop->new;
27              
28             my $future = $loop->new_future;
29              
30             $loop->watch_time( after => 3, code => sub { $future->done( "Done" ) } );
31              
32             print $future->get, "\n";
33              
34             =head1 DESCRIPTION
35              
36             This subclass of L stores a reference to the L
37             instance that created it, allowing the C method to block until the
38             Future is ready. These objects should not be constructed directly; instead
39             the C method on the containing Loop should be used.
40              
41             For a full description on how to use Futures, see the L documentation.
42              
43             =cut
44              
45             =head1 CONSTRUCTORS
46              
47             New C objects should be constructed by using the following
48             methods on the C. For more detail see the L
49             documentation.
50              
51             $future = $loop->new_future
52              
53             Returns a new pending Future.
54              
55             $future = $loop->delay_future( %args )
56              
57             Returns a new Future that will become done at a given time.
58              
59             $future = $loop->timeout_future( %args )
60              
61             Returns a new Future that will become failed at a given time.
62              
63             =cut
64              
65             sub new
66             {
67 1499     1499 1 38803 my $proto = shift;
68 1499         11582 my $self = $proto->SUPER::new;
69              
70 1499 100       19502 if( ref $proto ) {
71 476         1263 $self->{loop} = $proto->{loop};
72             }
73             else {
74 1023         4147 $self->{loop} = shift;
75             }
76              
77 1499         5496 return $self;
78             }
79              
80             =head1 METHODS
81              
82             =cut
83              
84             =head2 loop
85              
86             $loop = $future->loop
87              
88             Returns the underlying L object.
89              
90             =cut
91              
92             sub loop
93             {
94 5     5 1 15 my $self = shift;
95 5         37 return $self->{loop};
96             }
97              
98             sub await
99             {
100 40     40 1 1219 my $self = shift;
101 40         301 $self->{loop}->await( $self );
102             }
103              
104             =head2 done_later
105              
106             $future->done_later( @result )
107              
108             A shortcut to calling the C method in a C idle watch on the
109             underlying Loop object. Ensures that a returned Future object is not ready
110             immediately, but will wait for the next IO round.
111              
112             Like C, returns C<$future> itself to allow easy chaining.
113              
114             =cut
115              
116             sub done_later
117             {
118 1     1 1 5 my $self = shift;
119 1         4 my @result = @_;
120              
121 1     1   5 $self->loop->later( sub { $self->done( @result ) } );
  1         6  
122              
123 1         7 return $self;
124             }
125              
126             =head2 fail_later
127              
128             $future->fail_later( $exception, @details )
129              
130             A shortcut to calling the C method in a C idle watch on the
131             underlying Loop object. Ensures that a returned Future object is not ready
132             immediately, but will wait for the next IO round.
133              
134             Like C, returns C<$future> itself to allow easy chaining.
135              
136             =cut
137              
138             sub fail_later
139             {
140 1     1 1 7 my $self = shift;
141 1         4 my ( $exception, @details ) = @_;
142              
143 1 50       5 $exception or croak "Expected a true exception";
144              
145 1     1   4 $self->loop->later( sub { $self->fail( $exception, @details ) } );
  1         14  
146              
147 1         6 return $self;
148             }
149              
150             =head1 AUTHOR
151              
152             Paul Evans
153              
154             =cut
155              
156             0x55AA;