my ($hostname, $port, $data, $sendfile_cb) = @_;
my ($addr, $socket); # shared lexical variables
my $steps = define_steps
- cb => \$sendfile_cb;
+ cb_ref => \$sendfile_cb;
step lookup_addr => sub {
return async_gethostbyname(hostname => $hostname,
ghbn_cb => $steps->{'got_addr'});
invocation:
my $steps = define_steps
- cb => $finished_cb,
+ cb_ref => \$finished_cb,
immediate => 1;
To do the same for a single step, add the same keyword to the C<step> invocation:
step immediate => 1,
connect => sub { .. };
+In some case, you want to execute some code when the step finish, it can
+be done by defining a finalize code in define_steps:
+
+ my $steps = define_steps
+ cb_ref => \$finished_cb,
+ finalize => sub { .. };
+
=head2 JOINING ASYNCHRONOUS "THREADS"
With slow operations, it is often useful to perform multiple operations
my $running_commands = 0;
my ($result1, $result2);
my $steps = define_steps
- cb_ref => $finished_cb;
+ cb_ref => \$finished_cb;
step start => sub {
$running_commands++;
run_command($command1,
run_command($command2,
run_cb => $steps->{'command2_done'});
};
- step command1_done {
+ step command1_done => sub {
$result1 = $_[0];
$steps->{'maybe_done'}->();
};
- step command2_done {
+ step command2_done => sub {
$result2 = $_[0];
$steps->{'maybe_done'}->();
};
- step maybe_done {
+ step maybe_done => sub {
return if --$running_commands; # not done yet
$finished_cb->($result1, $result2);
};
sub define_steps (@) {
my (%params) = @_;
my $cb_ref = $params{'cb_ref'};
+ my $finalize = $params{'finalize'};
my %steps;
croak "cb_ref is undefined" unless defined $cb_ref;
$$cb_ref = sub {
%steps = ();
$current_steps = undef;
+ $finalize->() if defined($finalize);
goto $orig_cb;
};