The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

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();

DESCRIPTION

AnyEvent::Delay::Simple manages callbacks and controls the flow of events for AnyEvent. This module inspired by Mojo::IOLoop::Delay.

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 AE namespace instead of current one. Just prefix function name with AE:: when using module.

    use AnyEvent::Delay::Simple qw(AE::delay);
    AE::delay(...);

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 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); };
        my $w2; $w2 = AE::timer 2.0, 0, sub { $cv->end(); undef($w2); };
        $cv->cb(sub { $cv->send('step finished'); });
    }

easy_delay

    easy_delay(\&cb_1, ..., \&cb_n, \&err, \&fin);
    easy_delay([\&cb_1, ..., \&cb_n], \&fin);
    easy_delay([\&cb_1, ..., \&cb_n], \&err, \&fin);

    easy_delay($obj, \&cb_1, ..., \&cb_n, \&err, \&fin);
    easy_delay($obj, [\&cb_1, ..., \&cb_n], \&fin);
    easy_delay($obj, [\&cb_1, ..., \&cb_n], \&err, \&fin);

This function is similar to the previous function. But its arguments contains no condvar. And return values of each callbacks in chain passed as arguments to the next one.

    sub {
        return ('foo', 'bar');
    },
    sub {
        my (@args) = @_;
        # now @args is ('foo', 'bar')
    },

SEE ALSO

AnyEvent, AnyEvent::Delay, Mojo::IOLoop::Delay.

SUPPORT

AUTHOR

Denis Ibaev dionys@cpan.org for AdCamp.ru.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/ for more information.