blib/lib/Acme/Parataxis/Driver/IOAsync.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 96 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.40;
2 no warnings qw[experimental::class recursion];
3 use feature 'class';
4 class Acme::Parataxis::Driver::IOAsync v0.1.6 : isa(Acme::Parataxis::Driver) {
5 use Carp qw[croak];
6 use builtin qw[blessed];
7  
8 # Drives readiness through an IO::Async::Loop. IO::Async allows only one watch_io stanza per filehandle, but a
9 # read and a write may be established as two separate watch_io() calls (IO::Async >= 0.805). To keep the one-slot
10 # bookkeeping in the base class simple we always report both directions on unwatch; IO::Async tolerates canceling
11 # a direction that was never requested. watch_time()/unwatch_time() back timer()/cancel_timer(), and loop_once()
12 # (which also fires due timers) backs drive()/poll_ready().
13 field $loop : param; # an IO::Async::Loop (usually IO::Async::Loop::Select on Windows)
14 field %read_cb; # fileno => coderef
15 field %write_cb; # fileno => coderef
16 ADJUST {
17 croak 'Acme::Parataxis::Driver::IOAsync requires an IO::Async::Loop' unless blessed($loop) && $loop->isa('IO::Async::Loop');
18 }
19 method watch_read ( $fh, $cb ) { $self->_watch( $fh, 'r', $cb ) }
20 method watch_write ( $fh, $cb ) { $self->_watch( $fh, 'w', $cb ) }
21  
22 method _watch ( $fh, $dir, $cb ) {
23 my $fd = fileno($fh);
24 croak 'IOAsync driver cannot watch a closed filehandle' unless defined $fd;
25  
26 # As in Driver::Mojo: has_watch() compares handles rather than descriptor numbers, so a false here means this
27 # handle owns no slot yet and any callbacks filed under this number belong to a handle that is gone. Clearing
28 # them first keeps a recycled number from inheriting the previous owner's direction.
29 if ( !$self->has_watch($fh) ) {
30 delete $read_cb{$fd};
31 delete $write_cb{$fd};
32 }
33 if ( $dir eq 'r' ) { $read_cb{$fd} = $cb }
34 else { $write_cb{$fd} = $cb }
35 $self->_track_watch($fh);
36 my %params = ( handle => $fh );
37 $params{on_read_ready} = $read_cb{$fd} if $read_cb{$fd};
38 $params{on_write_ready} = $write_cb{$fd} if $write_cb{$fd};
39 $loop->watch_io(%params);
40 return $fh;
41 }
42  
43 method unwatch ($fh) {
44 my $fd = fileno($fh);
45 return 0 unless defined $fd && $self->has_watch($fh);
46 delete $read_cb{$fd};
47 delete $write_cb{$fd};
48 $self->_untrack_watch($fh);
49 $loop->unwatch_io( handle => $fh, on_read_ready => 1, on_write_ready => 1 );
50 return 1;
51 }
52  
53 method timer ( $ms, $cb ) {
54 croak 'IOAsync driver timer requires a positive delay' unless defined $ms && $ms > 0;
55 my $id;
56 $id = $loop->watch_time(
57 after => $ms / 1000,
58 code => sub {
59 $self->_untrack_timer($id);
60 $cb->();
61 }
62 );
63 $self->_track_timer($id);
64 return $id;
65 }
66  
67 method cancel_timer ($id) {
68 return 0 unless $self->_untrack_timer($id);
69 $loop->unwatch_time($id);
70 return 1;
71 }
72  
73 # loop_once(undef) blocks in select() until an event (IO or the earliest due timer) happens; loop_once(0) is a
74 # non-blocking readiness pass that still fires any already-due timers.
75 method drive () { $loop->loop_once(undef); return 1 }
76 method poll_ready () { $loop->loop_once(0); return 1 }
77 };
78 #
79 1;