| 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, 2009 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package CPS::Governor::IOAsync; | ||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 3 | ▶ | use base qw( CPS::Governor::Deferred ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 3 | our $VERSION = '0.02'; | |||||||||
| 16 | |||||||||||
| 17 | =head1 NAME | ||||||||||
| 18 | |||||||||||
| 19 | C<CPS::Governor::IOAsync> - use L<IO::Async> with L<CPS> | ||||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | use CPS qw( gkforeach ); | ||||||||||
| 24 | use CPS::Governor::IOAsync; | ||||||||||
| 25 | |||||||||||
| 26 | use IO::Async::Loop; | ||||||||||
| 27 | |||||||||||
| 28 | my $loop = IO::Async::Loop->new; | ||||||||||
| 29 | |||||||||||
| 30 | my $gov = CPS::Governor::IOAsync->new( loop => $loop ); | ||||||||||
| 31 | |||||||||||
| 32 | gkforeach( $gov, [ 1 .. 10 ], | ||||||||||
| 33 | sub { | ||||||||||
| 34 | my ( $item, $knext ) = @_; | ||||||||||
| 35 | |||||||||||
| 36 | $loop->do_something( on_done => $knext ); | ||||||||||
| 37 | }, | ||||||||||
| 38 | sub { $loop->loop_stop }, | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | $loop->loop_forever; | ||||||||||
| 42 | |||||||||||
| 43 | =head1 DESCRIPTION | ||||||||||
| 44 | |||||||||||
| 45 | This L<CPS::Governor> allows functions using it to defer their re-execution | ||||||||||
| 46 | by using the L<IO::Async::Loop> C<later> method, meaning it will interleave | ||||||||||
| 47 | with other IO operations performed by C<IO::Async>. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | =head1 CONSTRUCTOR | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | =head2 $gov = CPS::Governor::IOAsync->new( %args ) | ||||||||||
| 56 | |||||||||||
| 57 | Returns a new instance of a C<CPS::Governor::IOAsync> object. Requires the | ||||||||||
| 58 | following argument: | ||||||||||
| 59 | |||||||||||
| 60 | =over 8 | ||||||||||
| 61 | |||||||||||
| 62 | =item loop => IO::Async::Loop | ||||||||||
| 63 | |||||||||||
| 64 | Reference to the C<IO::Async::Loop> object. | ||||||||||
| 65 | |||||||||||
| 66 | =back | ||||||||||
| 67 | |||||||||||
| 68 | Additionally may take any other arguments defined by the | ||||||||||
| 69 | L<CPS::Governor::Deferred> class. | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | sub new | ||||||||||
| 74 | 3 | ▶ | { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 75 | 3 | my $class = shift; | |||||||||
| 76 | 3 | my %args = @_; | |||||||||
| 77 | |||||||||||
| 78 | 3 | ▶ | my $loop = delete $args{loop} or croak "Expected a 'loop'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | |||||||||||
| 80 | 3 | my $self = $class->SUPER::new( %args ); | |||||||||
| 81 | |||||||||||
| 82 | 3 | $self->{loop} = $loop; | |||||||||
| 83 | |||||||||||
| 84 | 3 | return $self; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub later | ||||||||||
| 88 | 10 | ▶ | { | ||||||||
|
Subroutine
later: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 89 | 10 | my $self = shift; | |||||||||
| 90 | 10 | $self->SUPER::later( @_ ); | |||||||||
| 91 | |||||||||||
| 92 | 10 | ▶ | return if $self->{later_queued}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | |||||||||||
| 94 | 3 | ▶ | $self->{loop}->later( sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 3 | undef $self->{later_queued}; | |||||||||
| 96 | 3 | $self->prod; | |||||||||
| 97 | 5 | } ); | |||||||||
| 98 | |||||||||||
| 99 | 5 | $self->{later_queued} = 1; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | # Keep perl happy; keep Britain tidy | ||||||||||
| 103 | 3 | 1; | |||||||||
| 104 | |||||||||||
| 105 | __END__ | ||||||||||