Acme-Sort-Sleep

 view release on metacpan or  search on metacpan

local/lib/perl5/IO/Async/Loop.pm  view on Meta::CPAN

   my ( $future ) = @_;

   $self->loop_once until $future->is_ready;

   return $future;
}

=head2 await_all

   $loop->await_all( @futures )

Blocks until all the given futures are ready, as indicated by the C<is_ready>
method. Equivalent to calling C<await> on a C<< Future->wait_all >> except
that it doesn't create the surrounding future object.

=cut

sub _all_ready { $_->is_ready or return 0 for @_; return 1  }

sub await_all
{
   my $self = shift;
   my @futures = @_;

   $self->loop_once until _all_ready @futures;
}

=head2 delay_future

   $loop->delay_future( %args )->get

Returns a new L<IO::Async::Future> instance which will become done at a given
point in time. The C<%args> should contain an C<at> or C<after> key as per the
C<watch_time> method. The returned future may be cancelled to cancel the
timer. At the alloted time the future will succeed with an empty result list.

=cut

sub delay_future
{
   my $self = shift;
   my %args = @_;

   my $future = $self->new_future;
   my $id = $self->watch_time( %args,
      code => sub { $future->done },
   );

   $future->on_cancel( sub { shift->loop->unwatch_time( $id ) } );

   return $future;
}

=head2 timeout_future

   $loop->timeout_future( %args )->get

Returns a new L<IO::Async::Future> instance which will fail at a given point
in time. The C<%args> should contain an C<at> or C<after> key as per the
C<watch_time> method. The returned future may be cancelled to cancel the
timer. At the alloted time, the future will fail with the string C<"Timeout">.

=cut

sub timeout_future
{
   my $self = shift;
   my %args = @_;

   my $future = $self->new_future;
   my $id = $self->watch_time( %args,
      code => sub { $future->fail( "Timeout" ) },
   );

   $future->on_cancel( sub { shift->loop->unwatch_time( $id ) } );

   return $future;
}

############
# Features #
############

=head1 FEATURES

Most of the following methods are higher-level wrappers around base
functionality provided by the low-level API documented below. They may be
used by L<IO::Async::Notifier> subclasses or called directly by the program.

The following methods documented with a trailing call to C<< ->get >> return
L<Future> instances.

=cut

sub __new_feature
{
   my $self = shift;
   my ( $classname ) = @_;

   ( my $filename = "$classname.pm" ) =~ s{::}{/}g;
   require $filename;

   # These features aren't supposed to be "user visible", so if methods called
   # on it carp or croak, the shortmess line ought to skip IO::Async::Loop and
   # go on report its caller. To make this work, add the feature class to our
   # @CARP_NOT list.
   push our(@CARP_NOT), $classname;

   return $classname->new( loop => $self );
}

=head2 attach_signal

   $id = $loop->attach_signal( $signal, $code )

This method adds a new signal handler to watch the given signal. The same
signal can be attached to multiple times; its callback functions will all be
invoked, in no particular order.

The returned C<$id> value can be used to identify the signal handler in case
it needs to be removed by the C<detach_signal> method. Note that this value
may be an object reference, so if it is stored, it should be released after it
cancelled, so the object itself can be freed.

=over 8

=item $signal

The name of the signal to attach to. This should be a bare name like C<TERM>.

=item $code

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.116 second using v1.00-cache-2.02-grep-82fe00e-cpan-c30982ac1bc3 )