File Coverage

blib/lib/IO/Async/Loop/EV.pm
Criterion Covered Total %
statement 21 87 24.1
branch 0 26 0.0
condition n/a
subroutine 7 22 31.8
pod n/a
total 28 135 20.7


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, 2012 -- leonerd@leonerd.org.uk
5              
6             package IO::Async::Loop::EV;
7              
8 5     5   149274 use strict;
  5         14  
  5         209  
9 5     5   28 use warnings;
  5         12  
  5         262  
10              
11             our $VERSION = '0.02';
12 5     5   37 use constant API_VERSION => '0.49';
  5         9  
  5         579  
13              
14 5     5   44 use base qw( IO::Async::Loop );
  5         11  
  5         3254  
15             IO::Async::Loop->VERSION( '0.49' );
16              
17 5     5   31 use constant _CAN_SUBSECOND_ACCURATELY => 0;
  5         9  
  5         248  
18              
19 5     5   27 use Carp;
  5         9  
  5         529  
20              
21 5     5   1011 use EV;
  5         3927  
  5         5401  
22              
23             =head1 NAME
24              
25             C - use C with C
26              
27             =head1 SYNOPSIS
28              
29             use IO::Async::Loop::EV;
30              
31             my $loop = IO::Async::Loop::EV->new();
32              
33             $loop->add( ... );
34              
35             $loop->add( IO::Async::Signal->new(
36             name => 'HUP',
37             on_receipt => sub { ... },
38             ) );
39              
40             $loop->loop_forever();
41              
42             =head1 DESCRIPTION
43              
44             This subclass of L uses L to perform its work.
45              
46             =cut
47              
48             sub new
49             {
50 0     0     my $class = shift;
51 0           my $self = $class->SUPER::__new( @_ );
52              
53 0           $self->{$_} = {} for qw( watch_r watch_w watch_time watch_signal watch_idle watch_child );
54              
55 0           return $self;
56             }
57              
58             sub loop_once
59             {
60 0     0     my $self = shift;
61 0           my ( $timeout ) = @_;
62              
63 0           my $timeout_w;
64 0 0         if( defined $timeout ) {
65 0     0     $timeout_w = EV::timer $timeout, 0, sub {}; # simply to wake up RUN_ONCE
  0            
66             }
67              
68 0           EV::run( EV::RUN_ONCE );
69             }
70              
71             sub watch_io
72             {
73 0     0     my $self = shift;
74 0           my %params = @_;
75              
76 0 0         my $handle = $params{handle} or die "Need a handle";
77              
78 0 0         if( my $on_read_ready = $params{on_read_ready} ) {
79 0           $self->{watch_r}{$handle} = EV::io( $handle, EV::READ, $on_read_ready );
80             }
81              
82 0 0         if( my $on_write_ready = $params{on_write_ready} ) {
83 0           $self->{watch_w}{$handle} = EV::io( $handle, EV::WRITE, $on_write_ready );
84             }
85             }
86              
87             sub unwatch_io
88             {
89 0     0     my $self = shift;
90 0           my %params = @_;
91              
92 0 0         my $handle = $params{handle} or die "Need a handle";
93              
94 0 0         if( $params{on_read_ready} ) {
95 0           delete $self->{watch_r}{$handle};
96             }
97              
98 0 0         if( $params{on_write_ready} ) {
99 0           delete $self->{watch_w}{$handle};
100             }
101             }
102              
103             sub watch_time
104             {
105 0     0     my $self = shift;
106 0           my %params = @_;
107              
108 0 0         my $code = $params{code} or croak "Expected 'code' as CODE ref";
109              
110 0           my $w;
111 0 0         if( defined $params{after} ) {
112 0           $w = EV::timer $params{after}, 0, $code;
113             }
114             else {
115 0           $w = EV::periodic $params{at}, 0, 0, $code;
116             }
117              
118 0           return $self->{watch_time}{$w} = $w;
119             }
120              
121             sub unwatch_time
122             {
123 0     0     my $self = shift;
124 0           my ( $id ) = @_;
125              
126 0           delete $self->{watch_time}{$id};
127             }
128              
129             sub watch_signal
130             {
131 0     0     my $self = shift;
132 0           my ( $signal, $code ) = @_;
133              
134 0 0         defined $self->signame2num( $signal ) or croak "No such signal '$signal'";
135              
136 0           $self->{watch_signal}{$signal} = EV::signal $signal, $code;
137             }
138              
139             sub unwatch_signal
140             {
141 0     0     my $self = shift;
142 0           my ( $signal ) = @_;
143              
144 0           delete $self->{watch_signal}{$signal};
145             }
146              
147             sub watch_idle
148             {
149 0     0     my $self = shift;
150 0           my %params = @_;
151              
152 0 0         my $when = delete $params{when} or croak "Expected 'when'";
153              
154 0 0         my $code = delete $params{code} or croak "Expected 'code' as a CODE ref";
155              
156 0 0         $when eq "later" or croak "Expected 'when' to be 'later'";
157              
158 0           my $key;
159             my $w = EV::idle sub {
160 0     0     delete $self->{watch_idle}{$key};
161 0           goto &$code;
162 0           };
163              
164 0           $key = "$w";
165 0           $self->{watch_idle}{$key} = $w;
166 0           return $key;
167             }
168              
169             sub unwatch_idle
170             {
171 0     0     my $self = shift;
172 0           my ( $id ) = @_;
173              
174 0           delete $self->{watch_idle}{$id};
175             }
176              
177             sub watch_child
178             {
179 0     0     my $self = shift;
180 0           my ( $pid, $code ) = @_;
181              
182             $self->{watch_child}{$pid} = EV::child $pid, 0, sub {
183 0     0     my $w = shift;
184 0           $code->( $w->rpid, $w->rstatus );
185 0           };
186             }
187              
188             sub unwatch_child
189             {
190 0     0     my $self = shift;
191 0           my ( $pid ) = @_;
192              
193 0           delete $self->{watch_child}{$pid};
194             }
195              
196             =head1 AUTHOR
197              
198             Paul Evans
199              
200             =cut
201              
202             0x55AA;