| 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 | 50 |  |  | 50 |  | 12429 | use strict; | 
|  | 50 |  |  |  |  | 89 |  | 
|  | 50 |  |  |  |  | 1916 |  | 
| 9 | 50 |  |  | 50 |  | 300 | use warnings; | 
|  | 50 |  |  |  |  | 261 |  | 
|  | 50 |  |  |  |  | 2661 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.801'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 50 |  |  | 50 |  | 352 | use base qw( Future ); | 
|  | 50 |  |  |  |  | 206 |  | 
|  | 50 |  |  |  |  | 14791 |  | 
| 14 |  |  |  |  |  |  | Future->VERSION( '0.05' ); # to respect subclassing | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 50 |  |  | 50 |  | 152314 | use Carp; | 
|  | 50 |  |  |  |  | 165 |  | 
|  | 50 |  |  |  |  | 17447 |  | 
| 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 | 1589 |  |  | 1589 | 1 | 40458 | my $proto = shift; | 
| 68 | 1589 |  |  |  |  | 9561 | my $self = $proto->SUPER::new; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 1589 | 100 |  |  |  | 18806 | if( ref $proto ) { | 
| 71 | 509 |  |  |  |  | 1281 | $self->{loop} = $proto->{loop}; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | else { | 
| 74 | 1080 |  |  |  |  | 3537 | $self->{loop} = shift; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1589 |  |  |  |  | 5138 | 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 | 8 |  |  | 8 | 1 | 22 | my $self = shift; | 
| 95 | 8 |  |  |  |  | 51 | return $self->{loop}; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub await | 
| 99 |  |  |  |  |  |  | { | 
| 100 | 76 |  |  | 76 | 1 | 3902 | my $self = shift; | 
| 101 | 76 |  |  |  |  | 722 | $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 | 13 | my $self = shift; | 
| 119 | 1 |  |  |  |  | 4 | my @result = @_; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 1 |  |  | 1 |  | 4 | $self->loop->later( sub { $self->done( @result ) } ); | 
|  | 1 |  |  |  |  | 4 |  | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 1 |  |  |  |  | 5 | 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 | 5 | my $self = shift; | 
| 141 | 1 |  |  |  |  | 4 | my ( $exception, @details ) = @_; | 
| 142 |  |  |  |  |  |  |  | 
| 143 | 1 | 50 |  |  |  | 3 | $exception or croak "Expected a true exception"; | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 1 |  |  | 1 |  | 3 | $self->loop->later( sub { $self->fail( $exception, @details ) } ); | 
|  | 1 |  |  |  |  | 9 |  | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 1 |  |  |  |  | 5 | return $self; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 AUTHOR | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Paul Evans | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =cut | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | 0x55AA; |