Skip to content

Commit

Permalink
more reactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 21, 2015
1 parent ae1deb1 commit e9518ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Mojo/Reactor/EV.pm
Expand Up @@ -148,6 +148,11 @@ Construct a new L<Mojo::Reactor::EV> object.
Run reactor until an event occurs or no events are being watched anymore. Note
that this method can recurse back into the reactor, so you need to be careful.
# Don't block longer than 0.5 seconds
my $id = $reactor->timer(0.5 => sub {});
$reactor->one_tick;
$reactor->remove($id);
=head2 recurring
my $id = $reactor->recurring(0.25 => sub {...});
Expand All @@ -162,6 +167,9 @@ amount of time in seconds.
Start watching for I/O and timer events, this will block until L</"stop"> is
called or no events are being watched anymore.
# Start reactor only if it is not running already
$reactor->start unless $reactor->is_running;
=head2 stop
$reactor->stop;
Expand All @@ -182,6 +190,18 @@ seconds.
Change I/O events to watch handle for with true and false values. Note that
this method requires an active I/O watcher.
# Watch only for readable events
$reactor->watch($handle, 1, 0);
# Watch only for writable events
$reactor->watch($handle, 0, 1);
# Watch for readable and writable events
$reactor->watch($handle, 1, 1);
# Pause watching for events
$reactor->watch($handle, 0, 0);
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
26 changes: 26 additions & 0 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -196,6 +196,12 @@ Restart timer. Note that this method requires an active timer.
Watch handle for I/O events, invoking the callback whenever handle becomes
readable or writable.
# Callback will be invoked twice if handle becomes readable and writable
$reactor->io($handle => sub {
my ($reactor, $writable) = @_;
say $writable ? 'Handle is writable' : 'Handle is readable';
});
=head2 is_running
my $bool = $reactor->is_running;
Expand All @@ -209,6 +215,11 @@ Check if reactor is running.
Run reactor until an event occurs or no events are being watched anymore. Note
that this method can recurse back into the reactor, so you need to be careful.
# Don't block longer than 0.5 seconds
my $id = $reactor->timer(0.5 => sub {});
$reactor->one_tick;
$reactor->remove($id);
=head2 recurring
my $id = $reactor->recurring(0.25 => sub {...});
Expand Down Expand Up @@ -236,6 +247,9 @@ Remove all handles and timers.
Start watching for I/O and timer events, this will block until L</"stop"> is
called or no events are being watched anymore.
# Start reactor only if it is not running already
$reactor->start unless $reactor->is_running;
=head2 stop
$reactor->stop;
Expand All @@ -256,6 +270,18 @@ seconds.
Change I/O events to watch handle for with true and false values. Note that
this method requires an active I/O watcher.
# Watch only for readable events
$reactor->watch($handle, 1, 0);
# Watch only for writable events
$reactor->watch($handle, 0, 1);
# Watch for readable and writable events
$reactor->watch($handle, 1, 1);
# Pause watching for events
$reactor->watch($handle, 0, 0);
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down

0 comments on commit e9518ff

Please sign in to comment.