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

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 131 CC 12 SCAR 36.4
CC
12
Cov
0%
_ft_arm
90.0
_ft_untimer
12.0
_ft_timer
6.0
CRAP 38.1
1 package Fetch::Loop::IOAsync;
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, $loop) = @_;
13 $loop ||= do { require IO::Async::Loop; IO::Async::Loop->new };
14 return bless { loop => $loop, fd => {} }, $class;
15 }
16  
17 sub loop { $_[0]{loop} }
18  
19 sub _ft_arm {
20 my ($self, $fd, $mask, $cv) = @_;
21 my $loop = $self->{loop};
22 my $st = $self->{fd}{$fd};
23  
24 if (!$mask) {
25 if ($st) {
26 $loop->unwatch_io(handle => $st->{fh},
27 on_read_ready => 1, on_write_ready => 1);
28 delete $self->{fd}{$fd};
29 }
30 return;
31 }
32  
33 $st ||= $self->{fd}{$fd} = { fh => $self->_fh_for_fd($fd), mask => 0 };
34 my $cur = $st->{mask};
35  
36 my %add;
37 $add{on_read_ready} = $cv if ($mask & Fetch::Loop::FT_READ) && !($cur & Fetch::Loop::FT_READ);
38 $add{on_write_ready} = $cv if ($mask & Fetch::Loop::FT_WRITE) && !($cur & Fetch::Loop::FT_WRITE);
39 $loop->watch_io(handle => $st->{fh}, %add) if %add;
40  
41 my %del;
42 $del{on_read_ready} = 1 if ($cur & Fetch::Loop::FT_READ) && !($mask & Fetch::Loop::FT_READ);
43 $del{on_write_ready} = 1 if ($cur & Fetch::Loop::FT_WRITE) && !($mask & Fetch::Loop::FT_WRITE);
44 $loop->unwatch_io(handle => $st->{fh}, %del) if %del;
45  
46 $st->{mask} = $mask;
47 return;
48 }
49  
50 sub _ft_timer {
51 my ($self, $secs, $cv) = @_;
52 my $g = { cancelled => 0 };
53 $g->{id} = $self->{loop}->watch_time(after => $secs,
54 code => sub { $cv->() unless $g->{cancelled} });
55 return $g;
56 }
57  
58 sub _ft_untimer {
59 my ($self, $g) = @_;
60 return unless ref $g;
61 $g->{cancelled} = 1;
62 $self->{loop}->unwatch_time($g->{id}) if defined $g->{id};
63 return;
64 }
65  
66 sub _ft_await {
67 my ($self, $f) = @_;
68 my $loop = $self->{loop};
69 $loop->loop_once until $f->is_ready;
70 return;
71 }
72  
73 sub install_await {
74 my ($self) = @_;
75 $Fetch::Future::AWAIT = sub { $self->_ft_await($_[0]) };
76 return $self;
77 }
78  
79 1;
80  
81 __END__