Skip to content

Commit

Permalink
just use two I/O watchers in the example
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 15, 2015
1 parent 1d72d7a commit 6d843c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
20 changes: 13 additions & 7 deletions lib/Mojo/Reactor/EV.pm
Expand Up @@ -80,22 +80,28 @@ Mojo::Reactor::EV - Low-level event reactor with libev support
use Mojo::Reactor::EV;
# Turn file descriptor into handle and watch if it becomes readable or
# writable
# Watch if handle becomes readable or writable
my $reactor = Mojo::Reactor::EV->new;
my $handle = IO::Handle->new_from_fd($fd, 'r+');
$reactor->io($handle => sub {
$reactor->io($first => sub {
my ($reactor, $writable) = @_;
say $writable ? 'Handle is writable' : 'Handle is readable';
say $writable ? 'First handle is writable' : 'First handle is readable';
});
# Change to watching only if handle becomes writable
$reactor->watch($handle, 0, 1);
$reactor->watch($first, 0, 1);
# Turn file descriptor into handle and watch if it becomes readable
my $second = IO::Handle->new_from_fd($fd, 'r');
$reactor->io($second => sub {
my ($reactor, $writable) = @_;
say $writable ? 'Second handle is writable' : 'Second handle is readable';
})->watch($second, 1, 0);
# Add a timer
$reactor->timer(15 => sub {
my $reactor = shift;
$reactor->remove($handle);
$reactor->remove($first);
$reactor->remove($second);
say 'Timeout!';
});
Expand Down
20 changes: 13 additions & 7 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -141,22 +141,28 @@ Mojo::Reactor::Poll - Low-level event reactor with poll support
use Mojo::Reactor::Poll;
# Turn file descriptor into handle and watch if it becomes readable or
# writable
# Watch if handle becomes readable or writable
my $reactor = Mojo::Reactor::Poll->new;
my $handle = IO::Handle->new_from_fd($fd, 'r+');
$reactor->io($handle => sub {
$reactor->io($first => sub {
my ($reactor, $writable) = @_;
say $writable ? 'Handle is writable' : 'Handle is readable';
say $writable ? 'First handle is writable' : 'First handle is readable';
});
# Change to watching only if handle becomes writable
$reactor->watch($handle, 0, 1);
$reactor->watch($first, 0, 1);
# Turn file descriptor into handle and watch if it becomes readable
my $second = IO::Handle->new_from_fd($fd, 'r');
$reactor->io($second => sub {
my ($reactor, $writable) = @_;
say $writable ? 'Second handle is writable' : 'Second handle is readable';
})->watch($second, 1, 0);
# Add a timer
$reactor->timer(15 => sub {
my $reactor = shift;
$reactor->remove($handle);
$reactor->remove($first);
$reactor->remove($second);
say 'Timeout!';
});
Expand Down

0 comments on commit 6d843c7

Please sign in to comment.