The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AnyEvent::Delay - Manage AnyEvent callbacks and control the flow of events

SYNOPSIS

    # Synchronize multiple events
    my $cv = AE::cv;
    my $delay = AnyEvent::Delay->new();
    $delay->on_finish(sub { say 'BOOM!'; $cv->send });
    for my $i (1 .. 10) {
      my $end = $delay->begin;
      Mojo::IOLoop->timer($i => sub {
        say 10 - $i;
        $end->();
      });
    }
    $cv->recv;

    # Sequentialize multiple events
    my $cv = AE::cv;
    my $delay = AnyEvent::Delay->new();
    $delay->steps(
    
        # First step (parallel events)
        sub {
          my $delay = shift;
          Mojo::IOLoop->timer(2 => $delay->begin);
          http_get( 'http://www.yinyuetai.com' => $delay->begin );
          say 'Second step in 2 seconds.';
        },
    
        # Second step (parallel timers)
        sub {
          my ($delay, @args) = @_;
          say "This http response is $args[1]->[1]{Status}";
          Mojo::IOLoop->timer(1 => $delay->begin);
          Mojo::IOLoop->timer(3 => $delay->begin);
          say 'Third step in 3 seconds.';
        },
    
        # Third step (the end)
        sub {
          my ($delay, @args) = @_;
          say 'And done after 5 seconds total.';
          $cv->send;
        }
    );
    $cv->recv;

DESCRIPTION

AnyEvent::Delay manages callbacks and controls the flow of events for AnyEvent. This module is Mojo::IOLoop::Delay version of AnyEvent.

EVENTS

AnyEvent::Delay have method the following.

on_error

  $delay->on_error(sub {
    my ($delay, $err) = @_;
    ...
  });

if an error occurs in one of the steps, breaking the chain.

on_finish

  $delay->on_finish(sub {
    my ($delay, @args) = @_;
    ...
  });

the active event _ae_counter reaches zero and there are no more steps.

METHODS

begin

  my $without_first_arg_arrayref = $delay->begin;
  my $with_first_arg_arrayref    = $delay->begin(0);

Increment active event _ae_counter, the returned callback can be used to decrement the active event _ae_counter again. Arguments passed to the callback are queued in the right order for the next step or on_finish event method, the argument will be array references for each begin callback.

steps

  $delay = $delay->steps(sub {...}, sub {...});

Sequentialize multiple events, the first callback will run right away, and the next one once the active event _ae_counter reaches zero. This chain will continue until there are no more callbacks, a callback does not increment the active event _ae_counter or an error occurs in a callback.

SEE ALSO

Mojo::IOLoop::Delay.