AnyEvent-Delay-Simple
view release on metacpan or search on metacpan
lib/AnyEvent/Delay/Simple.pm view on Meta::CPAN
my $sub = shift(@$subs);
unless (defined($args)) {
$args = [];
}
unless ($sub) {
$cv->send(@$args);
return;
}
$cv->begin();
AE::postpone {
my @res;
if ($err) {
eval {
@res = $sub->($obj ? $obj : (), @$args);
};
if ($@) {
my $msg = $@;
AE::log error => $msg;
$cv->cb(sub {
$err->($obj ? $obj : (), $msg);
});
$cv->send(@$args);
}
else {
_easy_delay_step($obj, $subs, $err, \@res, $cv);
}
}
else {
@res = $sub->($obj ? $obj : (), @$args);
_easy_delay_step($obj, $subs, $err, \@res, $cv);
}
$cv->end();
};
return;
}
1;
__END__
=head1 NAME
AnyEvent::Delay::Simple - Manage callbacks and control the flow of events by AnyEvent
=head1 SYNOPSIS
use AnyEvent::Delay::Simple;
my $cv = AE::cv;
delay(
sub {
say('1st step');
pop->send('1st result'); # send data to 2nd step
},
sub {
say(@_); # receive data from 1st step
say('2nd step');
die();
},
sub { # never calls because 2nd step failed
say('3rd step');
},
sub { # calls on error, at this time
say('Fail: ' . $_[1]);
$cv->send();
},
sub { # calls on success, not at this time
say('Ok');
$cv->send();
}
);
$cv->recv();
=head1 DESCRIPTION
AnyEvent::Delay::Simple manages callbacks and controls the flow of events for
AnyEvent. This module inspired by L<Mojo::IOLoop::Delay>.
=head1 FUNCTIONS
Both functions runs the chain of callbacks, the first callback will run right
away, and the next one once the previous callback finishes. This chain will
continue until there are no more callbacks, or an error occurs in a callback.
If an error occurs in one of the steps, the chain will be break, and error
handler will call, if it's defined. Unless error handler defined, error is
fatal. If last callback finishes and no error occurs, finish handler will call.
You may import these functions into L<AE> namespace instead of current one.
Just prefix function name with C<AE::> when using module.
use AnyEvent::Delay::Simple qw(AE::delay);
AE::delay(...);
=head2 delay
delay(\&cb_1, ..., \&cb_n, \&err, \&fin);
delay([\&cb_1, ..., \&cb_n], \&fin);
delay([\&cb_1, ..., \&cb_n], \&err, \&fin);
delay($obj, \&cb_1, ..., \&cb_n, \&err, \&fin);
delay($obj, [\&cb_1, ..., \&cb_n], \&fin);
delay($obj, [\&cb_1, ..., \&cb_n], \&err, \&fin);
If the first argument is blessed reference then all callbacks will be calls as
the methods of this object.
Condvar and data from previous step passed as arguments to each callback or
finish handler. If an error occurs then condvar and error message passed to
the error handler. The data sends to the next step by using condvar's C<send()>
method.
sub {
my $cv = pop();
$cv->send('foo', 'bar');
},
sub {
my $cv = pop();
my @args = @_;
# now @args is ('foo', 'bar')
},
Condvar can be used to control the flow of events within step.
sub {
my $cv = pop();
$cv->begin();
$cv->begin();
my $w1; $w1 = AE::timer 1.0, 0, sub { $cv->end(); undef($w1); };
( run in 1.406 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )