| 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, 2014-2026 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package POE::Future 0.06; | ||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use base qw( Future ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | Future->VERSION( '0.49' ); # udata | |||||||||
| 15 | |||||||||||
| 16 | 4 | ▶ | use POE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | C<POE::Future> - use L<Future> with L<POE> | ||||||||||
| 21 | |||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||
| 23 | |||||||||||
| 24 | =for highlighter language=perl | ||||||||||
| 25 | |||||||||||
| 26 | use POE::Future; | ||||||||||
| 27 | |||||||||||
| 28 | my $future = POE::Future->new_delay( 10 ) | ||||||||||
| 29 | ->then_done( "Hello, world!" ); | ||||||||||
| 30 | |||||||||||
| 31 | say $future->get; | ||||||||||
| 32 | |||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||
| 34 | |||||||||||
| 35 | This subclass of L<Future> integrates with L<POE>, allowing the C<await> | ||||||||||
| 36 | method to block until the future is ready. It allows C<POE>-using code to be | ||||||||||
| 37 | written that returns C<Future> instances, so that it can make full use of | ||||||||||
| 38 | C<Future>'s abilities, including L<Future::Utils>, and also that modules using | ||||||||||
| 39 | it can provide a C<Future>-based asynchronous interface of their own. | ||||||||||
| 40 | |||||||||||
| 41 | For a full description on how to use Futures, see the L<Future> documentation. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | =head1 CONSTRUCTORS | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | =head2 new | ||||||||||
| 50 | |||||||||||
| 51 | $f = POE::Future->new; | ||||||||||
| 52 | |||||||||||
| 53 | Returns a new leaf future instance, which will allow waiting for its result to | ||||||||||
| 54 | be made available, using the C<await> method. | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | =head2 new_delay | ||||||||||
| 59 | |||||||||||
| 60 | $f = POE::Future->new_delay( $after ); | ||||||||||
| 61 | |||||||||||
| 62 | Returns a new leaf future instance which will become ready (with an empty | ||||||||||
| 63 | result) after the specified delay time. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | sub new_delay | ||||||||||
| 68 | 3 | ▶ | { | ||||||||
|
Subroutine
new_delay: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 3 | my $self = shift->new; | |||||||||
| 70 | 3 | my ( $after ) = @_; | |||||||||
| 71 | |||||||||||
| 72 | $self->set_udata( session => my $session = POE::Session->create( | ||||||||||
| 73 | inline_states => { | ||||||||||
| 74 | 3 | ▶ | _start => sub { $_[KERNEL]->delay( done => $after ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 1 | ▶ | cancel => sub { $_[KERNEL]->delay( done => ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 2 | ▶ | done => sub { $self->done }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 77 | }, | ||||||||||
| 78 | 3 | ) ); | |||||||||
| 79 | |||||||||||
| 80 | 1 | ▶ | $self->on_cancel( sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 81 | 1 | my ( $self ) = @_; | |||||||||
| 82 | 1 | POE::Kernel->post( $session, cancel => ); | |||||||||
| 83 | 3 | }); | |||||||||
| 84 | |||||||||||
| 85 | 3 | return $self; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | =head2 new_alarm | ||||||||||
| 89 | |||||||||||
| 90 | $f = POE::Future->new_alarm( $at ); | ||||||||||
| 91 | |||||||||||
| 92 | Returns a new leaf future instance which will become ready (with an empty | ||||||||||
| 93 | result) at the specified alarm time. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | sub new_alarm | ||||||||||
| 98 | 1 | ▶ | { | ||||||||
|
Subroutine
new_alarm: called CC=1 SCAR=0.3
Pod
documented
| |||||||||||
| 99 | 1 | my $self = shift->new; | |||||||||
| 100 | 1 | my ( $at ) = @_; | |||||||||
| 101 | |||||||||||
| 102 | $self->set_udata( session => my $session = POE::Session->create( | ||||||||||
| 103 | inline_states => { | ||||||||||
| 104 | 1 | ▶ | _start => sub { $_[KERNEL]->alarm( done => $at ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 105 | 0 | ▶ | cancel => sub { $_[KERNEL]->alarm( done => ) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | 1 | ▶ | done => sub { $self->done }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 107 | }, | ||||||||||
| 108 | 1 | ) ); | |||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | $self->on_cancel( sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | my ( $self ) = @_; | |||||||||
| 112 | 0 | POE::Kernel->post( $session, cancel => ); | |||||||||
| 113 | 1 | }); | |||||||||
| 114 | |||||||||||
| 115 | 1 | return $self; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | =pod | ||||||||||
| 119 | |||||||||||
| 120 | To create a delay or alarm timer that will fail instead of succeed, use the | ||||||||||
| 121 | C<then_fail> method: | ||||||||||
| 122 | |||||||||||
| 123 | my $f = POE::Future->new_delay( 20 ) | ||||||||||
| 124 | ->then_fail( "Timeout" ); | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | sub await | ||||||||||
| 129 | 4 | ▶ | { | ||||||||
|
Subroutine
await: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 130 | 4 | my $self = shift; | |||||||||
| 131 | 4 | ▶ | POE::Kernel::run_one_timeslice until $self->is_ready; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 4 | return $self; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | =head1 AUTHOR | ||||||||||
| 136 | |||||||||||
| 137 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 138 | |||||||||||
| 139 | =cut | ||||||||||
| 140 | |||||||||||
| 141 | 4 | 0x55AA; | |||||||||