<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Mojo::IOLoop::Delay;
use Mojo::Base 'Mojo::EventEmitter';

use Mojo::IOLoop;

has ioloop =&gt; sub { Mojo::IOLoop-&gt;singleton };

sub begin {
  my ($self, $ignore) = @_;
  $self-&gt;{pending}++;
  my $id = $self-&gt;{counter}++;
  return sub { (defined $ignore ? $ignore : 1) and shift; $self-&gt;_step($id, @_) };
}

sub steps {
  my $self = shift;
  $self-&gt;{steps} = [@_];
  $self-&gt;ioloop-&gt;timer(0 =&gt; $self-&gt;begin);
  return $self;
}

sub wait {
  my $self = shift;

  my @args;
  $self-&gt;once(error =&gt; \&amp;_die);
  $self-&gt;once(finish =&gt; sub { shift-&gt;ioloop-&gt;stop; @args = @_ });
  $self-&gt;ioloop-&gt;start;

  return wantarray ? @args : $args[0];
}

sub _die { $_[0]-&gt;has_subscribers('error') ? $_[0]-&gt;ioloop-&gt;stop : die $_[1] }

sub _step {
  my ($self, $id) = (shift, shift);

  $self-&gt;{args}[$id] = [@_];
  return if $self-&gt;{fail} || --$self-&gt;{pending} || $self-&gt;{lock};
  local $self-&gt;{lock} = 1;
  my @args = map {@$_} @{delete $self-&gt;{args}};

  $self-&gt;{counter} = 0;
  if (my $cb = shift @{$self-&gt;{steps} ||= []}) {
    eval { $self-&gt;$cb(@args); 1 } or return $self-&gt;emit(error =&gt; $@)-&gt;{fail}++;
  }

  return $self-&gt;emit(finish =&gt; @args) unless $self-&gt;{counter};
  $self-&gt;ioloop-&gt;timer(0 =&gt; $self-&gt;begin) unless $self-&gt;{pending};
}

1;

=encoding utf8

=head1 NAME

Mojo::IOLoop::Delay - Manage callbacks and control the flow of events

=head1 SYNOPSIS

  use Mojo::IOLoop::Delay;

  # Synchronize multiple events
  my $delay = Mojo::IOLoop::Delay-&gt;new;
  $delay-&gt;on(finish =&gt; sub { say 'BOOM!' });
  for my $i (1 .. 10) {
    my $end = $delay-&gt;begin;
    Mojo::IOLoop-&gt;timer($i =&gt; sub {
      say 10 - $i;
      $end-&gt;();
    });
  }
  $delay-&gt;wait unless Mojo::IOLoop-&gt;is_running;

  # Sequentialize multiple events
  my $delay = Mojo::IOLoop::Delay-&gt;new;
  $delay-&gt;steps(

    # First step (simple timer)
    sub {
      my $delay = shift;
      Mojo::IOLoop-&gt;timer(2 =&gt; $delay-&gt;begin);
      say 'Second step in 2 seconds.';
    },

    # Second step (parallel timers)
    sub {
      my ($delay, @args) = @_;
      Mojo::IOLoop-&gt;timer(1 =&gt; $delay-&gt;begin);
      Mojo::IOLoop-&gt;timer(3 =&gt; $delay-&gt;begin);
      say 'Third step in 3 seconds.';
    },

    # Third step (the end)
    sub {
      my ($delay, @args) = @_;
      say 'And done after 5 seconds total.';
    }
  );
  $delay-&gt;wait unless Mojo::IOLoop-&gt;is_running;

=head1 DESCRIPTION

L&lt;Mojo::IOLoop::Delay&gt; manages callbacks and controls the flow of events for
L&lt;Mojo::IOLoop&gt;.

=head1 EVENTS

L&lt;Mojo::IOLoop::Delay&gt; inherits all events from L&lt;Mojo::EventEmitter&gt; and can
emit the following new ones.

=head2 error

  $delay-&gt;on(error =&gt; sub {
    my ($delay, $err) = @_;
    ...
  });

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

=head2 finish

  $delay-&gt;on(finish =&gt; sub {
    my ($delay, @args) = @_;
    ...
  });

Emitted once the active event counter reaches zero and there are no more
steps.

=head1 ATTRIBUTES

L&lt;Mojo::IOLoop::Delay&gt; implements the following attributes.

=head2 ioloop

  my $ioloop = $delay-&gt;ioloop;
  $delay     = $delay-&gt;ioloop(Mojo::IOLoop-&gt;new);

Event loop object to control, defaults to the global L&lt;Mojo::IOLoop&gt;
singleton.

=head1 METHODS

L&lt;Mojo::IOLoop::Delay&gt; inherits all methods from L&lt;Mojo::EventEmitter&gt; and
implements the following new ones.

=head2 begin

  my $without_first_arg = $delay-&gt;begin;
  my $with_first_arg    = $delay-&gt;begin(0);

Increment active event counter, the returned callback can be used to decrement
the active event counter again. Arguments passed to the callback are queued in
the right order for the next step or L&lt;/"finish"&gt; event and L&lt;/"wait"&gt; method,
the first argument will be ignored by default.

  # Capture all arguments
  my $delay = Mojo::IOLoop-&gt;delay;
  Mojo::IOLoop-&gt;client({port =&gt; 3000} =&gt; $delay-&gt;begin(0));
  my ($loop, $err, $stream) = $delay-&gt;wait;

=head2 steps

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

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

=head2 wait

  my $arg  = $delay-&gt;wait;
  my @args = $delay-&gt;wait;

Start L&lt;/"ioloop"&gt; and stop it again once an L&lt;/"error"&gt; or L&lt;/"finish"&gt; event
gets emitted, only works when L&lt;/"ioloop"&gt; is not running already.

  # Use the "finish" event to synchronize portably
  $delay-&gt;on(finish =&gt; sub {
    my ($delay, @args) = @_;
    ...
  });
  $delay-&gt;wait unless $delay-&gt;ioloop-&gt;is_running;

=head1 SEE ALSO

L&lt;Mojolicious&gt;, L&lt;Mojolicious::Guides&gt;, L&lt;http://mojolicio.us&gt;.

=cut
</pre></body></html>