__PACKAGE__->APPLY;
my
$loop
;
sub
sleep
{
shift
;
my
(
$secs
) =
@_
;
return
(
$loop
//= IO::Async::Loop->new )->delay_future(
after
=>
$secs
);
}
my
%watching_read_by_fileno
;
sub
ready_for_read
{
shift
;
my
(
$fh
) =
@_
;
my
$watching
=
$watching_read_by_fileno
{
$fh
->
fileno
} //= [];
$loop
//= IO::Async::Loop->new;
my
$f
=
$loop
->new_future;
my
$was
=
scalar
@$watching
;
push
@$watching
,
$f
;
return
$f
if
$was
;
$loop
->watch_io(
handle
=>
$fh
,
on_read_ready
=>
sub
{
$watching
->[0]->done;
shift
@$watching
;
return
if
scalar
@$watching
;
$loop
->unwatch_io(
handle
=>
$fh
,
on_read_ready
=> 1,
);
delete
$watching_read_by_fileno
{
$fh
->
fileno
};
},
);
return
$f
;
}
my
%watching_write_by_fileno
;
sub
ready_for_write
{
shift
;
my
(
$fh
) =
@_
;
my
$watching
=
$watching_write_by_fileno
{
$fh
->
fileno
} //= [];
$loop
//= IO::Async::Loop->new;
my
$f
=
$loop
->new_future;
my
$was
=
scalar
@$watching
;
push
@$watching
,
$f
;
return
$f
if
$was
;
$loop
->watch_io(
handle
=>
$fh
,
on_write_ready
=>
sub
{
$watching
->[0]->done;
shift
@$watching
;
return
if
scalar
@$watching
;
$loop
->unwatch_io(
handle
=>
$fh
,
on_write_ready
=> 1,
);
delete
$watching_write_by_fileno
{
$fh
->
fileno
};
},
);
return
$f
;
}
sub
waitpid
{
shift
;
my
(
$pid
) =
@_
;
my
$f
= (
$loop
//= IO::Async::Loop->new )->new_future;
$loop
->watch_process(
$pid
,
sub
{
my
(
undef
,
$wstatus
) =
@_
;
$f
->done(
$wstatus
);
} );
$f
->on_cancel(
sub
{
$loop
->unwatch_process(
$pid
) } );
return
$f
;
}
0x55AA;