blib/lib/Fetch/Loop/AnyEvent.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 95 CC 7 SCAR 20.3
CC
7
Cov
0%
_ft_arm
20.0
_ft_timer
6.0
_ft_untimer
6.0
CRAP 7.6
1 package Fetch::Loop::AnyEvent;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.29';
7  
8 use parent -norequire, 'Fetch::Loop';
9 use Fetch::Loop ();
10  
11 sub new {
12 my ($class) = @_;
13 require AnyEvent;
14 return bless { w => {} }, $class;
15 }
16  
17 sub _ft_arm {
18 my ($self, $fd, $mask, $cv) = @_;
19 my $w = $self->{w}{$fd} ||= {};
20  
21 if ($mask & Fetch::Loop::FT_READ) {
22 $w->{r} ||= AE::io($fd, 0, sub { $cv->() });
23 } else {
24 delete $w->{r};
25 }
26 if ($mask & Fetch::Loop::FT_WRITE) {
27 $w->{w} ||= AE::io($fd, 1, sub { $cv->() });
28 } else {
29 delete $w->{w};
30 }
31 delete $self->{w}{$fd} unless %$w;
32 return;
33 }
34  
35 sub _ft_timer {
36 my ($self, $secs, $cv) = @_;
37 my $g = { cancelled => 0 };
38 $g->{w} = AE::timer($secs, 0, sub { $cv->() unless $g->{cancelled} });
39 return $g;
40 }
41  
42 sub _ft_untimer {
43 my ($self, $g) = @_;
44 return unless ref $g;
45 $g->{cancelled} = 1;
46 delete $g->{w}; # dropping the guard cancels the timer
47 return;
48 }
49  
50 sub _ft_await {
51 my ($self, $f) = @_;
52 return if $f->is_ready;
53 my $cv = AE::cv();
54 $f->on_ready(sub { $cv->send });
55 $cv->recv;
56 return;
57 }
58  
59 sub install_await {
60 my ($self) = @_;
61 $Fetch::Future::AWAIT = sub { $self->_ft_await($_[0]) };
62 return $self;
63 }
64  
65 1;
66  
67 __END__