Skip to content

Commit

Permalink
renamed Mojo::IOLoop::Trigger to Mojo::IOLoop::Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 28, 2011
1 parent 17d597a commit 6d31997
Show file tree
Hide file tree
Showing 45 changed files with 253 additions and 258 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,7 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.14 2011-10-28 00:00:00
2.14 2011-10-29 00:00:00
- Deprecated Mojo::DOM->new with arguments.
- Renamed Mojo::IOLoop::Trigger to Mojo::IOLoop::Delay.
- Replaced cancel method in Mojo::IOWatcher with drop_timer method.
- Replaced remove method in Mojo::IOWatcher with drop_handle method.
- Added EXPERIMENTAL --verbose flag to test command.

2.13 2011-10-28 00:00:00
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Content.pm
Expand Up @@ -17,10 +17,10 @@ sub body_contains {
sub body_size { croak 'Method "body_size" not implemented by subclass' }

sub boundary {
return
unless (shift->headers->content_type || '')
=~ /multipart.*boundary=\"*([a-zA-Z0-9\'\(\)\,\.\:\?\-\_\+\/]+)/i;
return $1;
return $1
if (shift->headers->content_type || '')
=~ m#multipart.*boundary=\"*([a-zA-Z0-9\'\(\)\,\.\:\?\-\_\+/]+)#i;
return;
}

# "Operator! Give me the number for 911!"
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/MultiPart.pm
Expand Up @@ -65,7 +65,7 @@ sub build_boundary {
}

# Add boundary to Content-Type header
$type =~ /^(.*multipart\/[^;]+)(.*)$/;
$type =~ m#^(.*multipart/[^;]+)(.*)$#;
my $before = $1 || 'multipart/mixed';
my $after = $2 || '';
$headers->content_type("$before; boundary=$boundary$after");
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -21,8 +21,8 @@ my $ATTR_RE = qr/
)?
\s*
/x;
my $END_RE = qr/^\s*\/\s*(?<tag>.+)\s*/;
my $START_RE = qr/(?<tag>[^\s\/]+)(?<attrs>[\s\S]*)/;
my $END_RE = qr#^\s*/\s*(?<tag>.+)\s*#;
my $START_RE = qr#(?<tag>[^\s/]+)(?<attrs>[\s\S]*)#;
my $TOKEN_RE = qr/
(?<text>[^<]*) # Text
(?:
Expand Down Expand Up @@ -154,11 +154,11 @@ sub parse {

# Empty element
$self->_end($start, \$current)
if (!$self->xml && $VOID{$start}) || $attr =~ /\/\s*$/;
if (!$self->xml && $VOID{$start}) || $attr =~ m#/\s*$#;

# Relaxed "script" or "style"
if ($start ~~ [qw/script style/]) {
if ($html =~ /\G(.*?)<\s*\/\s*$start\s*>/gcsi) {
if ($html =~ m#\G(.*?)<\s*/\s*$start\s*>#gcsi) {
$self->_raw($1, \$current);
$self->_end($start, \$current);
}
Expand Down
72 changes: 36 additions & 36 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -3,10 +3,10 @@ use Mojo::Base -base;

use Carp 'croak';
use Mojo::IOLoop::Client;
use Mojo::IOLoop::Delay;
use Mojo::IOLoop::Resolver;
use Mojo::IOLoop::Server;
use Mojo::IOLoop::Stream;
use Mojo::IOLoop::Trigger;
use Mojo::IOWatcher;
use Mojo::Util 'md5_sum';
use Scalar::Util qw/blessed weaken/;
Expand Down Expand Up @@ -92,6 +92,18 @@ sub connection_timeout {

sub defer { shift->timer(0 => @_) }

sub delay {
my ($self, $cb) = @_;
$self = $self->singleton unless ref $self;

my $delay = Mojo::IOLoop::Delay->new;
$delay->ioloop($self);
weaken $delay->{ioloop};
$delay->once(finish => $cb) if $cb;

return $delay;
}

sub drop {
my ($self, $id) = @_;
$self = $self->singleton unless ref $self;
Expand Down Expand Up @@ -287,18 +299,6 @@ sub timer {
return $self->iowatcher->timer($after => sub { $self->$cb(pop) });
}

sub trigger {
my ($self, $cb) = @_;
$self = $self->singleton unless ref $self;

my $t = Mojo::IOLoop::Trigger->new;
$t->ioloop($self);
weaken $t->{ioloop};
$t->once(finish => $cb) if $cb;

return $t;
}

sub write {
my ($self, $id, $chunk, $cb) = @_;

Expand Down Expand Up @@ -344,7 +344,7 @@ sub _drop {

# Timer
return $self unless my $watcher = $self->iowatcher;
return $self if $watcher->cancel($id);
return $self if $watcher->drop_timer($id);

# Listen socket
if (delete $self->{servers}->{$id}) { delete $self->{listening} }
Expand Down Expand Up @@ -667,6 +667,28 @@ dropped, defaults to C<15>.
Invoke callback on next reactor tick.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<delay>
my $delay = Mojo::IOLoop->delay;
my $delay = $loop->delay;
my $delay = $loop->delay(sub {...});
Get L<Mojo::IOLoop::Delay> event synchronization helper.
Note that this method is EXPERIMENTAL and might change without warning!
# Synchronize multiple events
my $delay = Mojo::IOLoop->delay(sub { say 'BOOM!' });
for my $i (1 .. 10) {
$delay->begin;
Mojo::IOLoop->timer($i => sub {
say 10 - $i;
$delay->end;
});
}
# Wait for events
$delay->wait;
=head2 C<drop>
$loop = Mojo::IOLoop->drop($id)
Expand Down Expand Up @@ -964,28 +986,6 @@ Note that this method is EXPERIMENTAL and might change without warning!
Create a new timer, invoking the callback after a given amount of seconds.
=head2 C<trigger>
my $t = Mojo::IOLoop->trigger;
my $t = $loop->trigger;
my $t = $loop->trigger(sub {...});
Get L<Mojo::IOLoop::Trigger> remote control for the loop.
Note that this method is EXPERIMENTAL and might change without warning!
# Synchronize multiple events
my $t = Mojo::IOLoop->trigger(sub { say 'BOOM!' });
for my $i (1 .. 10) {
$t->begin;
Mojo::IOLoop->timer($i => sub {
say 10 - $i;
$t->end;
});
}
# Stop automatically when finished
$t->start;
=head2 C<write>
$loop->write($id => 'Hello!');
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -54,8 +54,8 @@ sub _cleanup {
return unless my $resolver = $self->{resolver};
return unless my $loop = $resolver->ioloop;
return unless my $watcher = $loop->iowatcher;
$watcher->cancel($self->{timer}) if $self->{timer};
$watcher->remove($self->{handle}) if $self->{handle};
$watcher->drop_timer($self->{timer}) if $self->{timer};
$watcher->drop_handle($self->{handle}) if $self->{handle};
}

sub _connect {
Expand Down
53 changes: 27 additions & 26 deletions lib/Mojo/IOLoop/Trigger.pm → lib/Mojo/IOLoop/Delay.pm
@@ -1,4 +1,4 @@
package Mojo::IOLoop::Trigger;
package Mojo::IOLoop::Delay;
use Mojo::Base 'Mojo::EventEmitter';

use Mojo::IOLoop;
Expand All @@ -18,7 +18,7 @@ sub end {
$self->emit_safe('finish', @{$self->{args}}) if --$self->{counter} <= 0;
}

sub start {
sub wait {
my $self = shift;
$self->once(finish => sub { shift->ioloop->stop });
$self->ioloop->start;
Expand All @@ -30,80 +30,81 @@ __END__
=head1 NAME
Mojo::IOLoop::Trigger - IOLoop trigger
Mojo::IOLoop::Delay - IOLoop delay
=head1 SYNOPSIS
use Mojo::IOLoop::Trigger;
use Mojo::IOLoop::Delay;
# Synchronize multiple events
my $t = Mojo::IOLoop::Trigger->new;
$t->on(finish => sub { say 'BOOM!' });
my $delay = Mojo::IOLoop::Delay->new;
$delay->on(finish => sub { say 'BOOM!' });
for my $i (1 .. 10) {
$t->begin;
$delay->begin;
Mojo::IOLoop->timer($i => sub {
say 10 - $i;
$t->end;
$delay->end;
});
}
# Stop automatically when finished
$t->start;
# Wait for events
$delay->wait;
=head1 DESCRIPTION
L<Mojo::IOLoop::Trigger> is a remote control for L<Mojo::IOLoop>.
L<Mojo::IOLoop::Delay> is an event synchronization helper for
L<Mojo::IOLoop>.
Note that this module is EXPERIMENTAL and might change without warning!
=head1 EVENTS
L<Mojo::IOLoop::Trigger> can emit the following events.
L<Mojo::IOLoop::Delay> can emit the following events.
=head2 C<finish>
$trigger->on(finish => sub {
my $trigger = shift;
$delay->on(finish => sub {
my $delay = shift;
});
Emitted once the active event counter reaches zero.
=head1 ATTRIBUTES
L<Mojo::IOLoop::Trigger> implements the following attributes.
L<Mojo::IOLoop::Delay> implements the following attributes.
=head2 C<ioloop>
my $ioloop = $t->ioloop;
$t = $t->ioloop(Mojo::IOLoop->new);
my $ioloop = $delay->ioloop;
$delay = $delay->ioloop(Mojo::IOLoop->new);
Loop object to control, defaults to a L<Mojo::IOLoop> object.
=head1 METHODS
L<Mojo::IOLoop::Trigger> inherits all methods from L<Mojo::EventEmitter> and
L<Mojo::IOLoop::Delay> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 C<begin>
my $cb = $t->begin;
my $cb = $delay->begin;
Increment active event counter, the returned callback can be used instead of
C<end>.
my $t = Mojo::IOLoop->trigger;
Mojo::IOLoop->resolver->lookup('mojolicio.us' => $t->begin);
my $address = $t->start;
my $delay = Mojo::IOLoop->delay;
Mojo::IOLoop->resolver->lookup('mojolicio.us' => $delay->begin);
my $address = $delay->start;
=head2 C<end>
$t->end;
$t->end(@args);
$delay->end;
$delay->end(@args);
Decrement active event counter.
=head2 C<start>
=head2 C<wait>
my @args = $t->start;
my @args = $delay->wait;
Start C<ioloop> and register C<finish> event that stops it again once the
active event counter reaches zero.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -79,7 +79,7 @@ sub DESTROY {
if (my $key = $self->{key}) { unlink $key if -w $key }
return unless my $watcher = $self->{iowatcher};
$self->pause if $self->{handle};
$watcher->remove($_) for values %{$self->{handles}};
$watcher->drop_handle($_) for values %{$self->{handles}};
}

# "And I gave that man directions, even though I didn't know the way,
Expand Down Expand Up @@ -166,7 +166,7 @@ sub generate_port {

sub pause {
my $self = shift;
$self->iowatcher->remove($self->{handle});
$self->iowatcher->drop_handle($self->{handle});
}

sub resume {
Expand All @@ -191,7 +191,7 @@ sub _accept {
weaken $self;
$tls->{SSL_error_trap} = sub {
return unless my $handle = delete $self->{handles}->{shift()};
$self->iowatcher->remove($handle);
$self->iowatcher->drop_handle($handle);
close $handle;
};
$handle = IO::Socket::SSL->start_SSL($handle, %$tls);
Expand Down Expand Up @@ -244,7 +244,7 @@ sub _tls {

# Accepted
if ($handle->accept_SSL) {
$self->iowatcher->remove($handle);
$self->iowatcher->drop_handle($handle);
delete $self->{handles}->{$handle};
return $self->emit_safe(accept => $handle);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -19,7 +19,7 @@ sub DESTROY {
my $self = shift;
return unless my $watcher = $self->{iowatcher};
return unless my $handle = $self->{handle};
$watcher->remove($handle);
$watcher->drop_handle($handle);
close $handle;
$self->_close;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ sub resume {
# about it."
sub steal_handle {
my $self = shift;
$self->iowatcher->remove($self->{handle});
$self->iowatcher->drop_handle($self->{handle});
return delete $self->{handle};
}

Expand Down

0 comments on commit 6d31997

Please sign in to comment.