AnyEvent-Delay-Simple

 view release on metacpan or  search on metacpan

lib/AnyEvent/Delay/Simple.pm  view on Meta::CPAN

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

=head2 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')
    },

=head1 SEE ALSO

L<AnyEvent>, L<AnyEvent::Delay>, L<Mojo::IOLoop::Delay>.

=head1 SUPPORT

=over 4

=item * Repository

L<http://github.com/AdCampRu/anyevent-delay-simple>

=item * Bug tracker

L<http://github.com/AdCampRu/anyevent-delay-simple/issues>

=back

=head1 AUTHOR

Denis Ibaev C<dionys@cpan.org> for AdCamp.ru.

=head1 LICENSE

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

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

=cut



( run in 0.645 second using v1.01-cache-2.11-cpan-df04353d9ac )