Last active
August 29, 2015 14:08
-
-
Save Logioniz/cf44c3fa258a28656b25 to your computer and use it in GitHub Desktop.
Nice example to understand ioloop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $t = Test::Mojo->new; | |
sub f2 { | |
my $cb = shift; | |
my $d = Mojo::IOLoop->delay( | |
sub { | |
warn 'f2_1', $/; | |
shift->pass; | |
}, | |
sub { | |
warn 'f2_2', $/; | |
$cb->(); | |
} | |
); | |
$d->on(finish => sub { | |
warn 'FINISH f2', $/; | |
}); | |
$d->wait; | |
my $subscribers = $d->subscribers('finish'); | |
warn 'Len2 : ', scalar @$subscribers, $/; | |
} | |
sub f1 { | |
my $cb = shift; | |
my $d = Mojo::IOLoop->delay( | |
sub { | |
warn 'f1_1', $/; | |
f2(shift->begin); | |
}, | |
sub { | |
warn 'f1_2', $/; | |
$cb->(); | |
} | |
); | |
$d->on(finish => sub { | |
warn 'FINISH f1', $/; | |
}); | |
$d->wait; | |
} | |
my $d = Mojo::IOLoop::Delay->new; | |
$d->steps( | |
# my $d = Mojo::IOLoop->delay( | |
sub { | |
warn 11111; | |
f1(shift->begin); | |
}, | |
sub { | |
my ($delay) = @_; | |
warn 2222; | |
$delay->pass; | |
}, | |
sub { | |
warn 3333; | |
return; | |
} | |
); | |
my $subscribers = $d->subscribers('finish'); | |
warn 'Len: ', scalar @$subscribers, $/; | |
$d->on(finish => sub { | |
warn '!!FINISH!!', $/; | |
}); | |
$d->wait; | |
#Mojo::IOLoop->start; | |
done_testing; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment