Skip to content

Commit

Permalink
added return values for render_later and finish
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 12, 2012
1 parent cf7d8c3 commit 254ef1c
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.29 2012-08-11
3.29 2012-08-12
- Improved documentation.
- Improved tests.

Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -14,11 +14,13 @@ use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;
# ...Where are we going?"
sub emit {
my ($self, $name) = (shift, shift);

if (my $s = $self->{events}{$name}) {
warn "-- Emit $name in @{[blessed($self)]} (@{[scalar(@$s)]})\n" if DEBUG;
for my $cb (@$s) { $self->$cb(@_) }
}
elsif (DEBUG) { warn "-- Emit $name in @{[blessed($self)]} (0)\n" }

return $self;
}

Expand Down
46 changes: 23 additions & 23 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -135,15 +135,15 @@ sub server {
if defined $self->{accepts}
&& ($self->{accepts} -= int(rand 2) + 1) <= 0;

# Stop listening
$self->_not_listening;
# Stop accepting
$self->_not_accepting;
}
);
$server->listen(@_);
$self->{accepts} = $self->max_accepts if $self->max_accepts;

# Stop listening
$self->_not_listening;
# Stop accepting
$self->_not_accepting;

return $id;
}
Expand Down Expand Up @@ -180,19 +180,11 @@ sub timer {
return $self->reactor->timer($after => sub { $self->$cb });
}

sub _id {
my $self = shift;
my $id;
do { $id = md5_sum('c' . time . rand 999) }
while $self->{connections}{$id} || $self->{servers}{$id};
return $id;
}

sub _listening {
sub _accepting {
my $self = shift;

# Check connection limit
return if $self->{listening};
return if $self->{accepting};
my $servers = $self->{servers} ||= {};
return unless keys %$servers;
my $i = keys %{$self->{connections}};
Expand All @@ -202,16 +194,24 @@ sub _listening {
# Acquire accept mutex
if (my $cb = $self->lock) { return unless $self->$cb(!$i) }

# Check if multi-accept is desirable and start listening
# Check if multi-accept is desirable and start accepting
$_->accepts($max > 1 ? 10 : 1)->start for values %$servers;
$self->{listening} = 1;
$self->{accepting}++;
}

sub _id {
my $self = shift;
my $id;
do { $id = md5_sum('c' . time . rand 999) }
while $self->{connections}{$id} || $self->{servers}{$id};
return $id;
}

sub _manage {
my $self = shift;

# Start listening if possible
$self->_listening;
# Start accepting if possible
$self->_accepting;

# Close connections gracefully
my $connections = $self->{connections} ||= {};
Expand All @@ -231,15 +231,15 @@ sub _manager {
$self->{manager} ||= $self->recurring($self->accept_interval => \&_manage);
}

sub _not_listening {
sub _not_accepting {
my $self = shift;

# Release accept mutex
return unless delete $self->{listening};
return unless delete $self->{accepting};
return unless my $cb = $self->unlock;
$self->$cb();

# Stop listening
# Stop accepting
$_->stop for values %{$self->{servers} || {}};
}

Expand All @@ -251,7 +251,7 @@ sub _remove {
return if $reactor->remove($id);

# Listen socket
if (delete $self->{servers}{$id}) { delete $self->{listening} }
if (delete $self->{servers}{$id}) { delete $self->{accepting} }

# Connection (stream needs to be deleted first)
else {
Expand Down Expand Up @@ -369,7 +369,7 @@ this callback are not captured.
$loop->lock(sub {
my ($loop, $blocking) = @_;
# Got the accept mutex, start listening
# Got the accept mutex, start accepting new connections
return 1;
});
Expand Down
37 changes: 18 additions & 19 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -70,13 +70,28 @@ sub _connect {

# Wait for handle to become writable
weaken $self;
$reactor->io($handle => sub { $self->_connecting($args) })
->watch($handle, 0, 1);
$reactor->io($handle => sub { $self->_try($args) })->watch($handle, 0, 1);
}

sub _tls {
my $self = shift;

# Switch between reading and writing
my $handle = $self->{handle};
if ($self->{tls} && !$handle->connect_SSL) {
my $err = $IO::Socket::SSL::SSL_ERROR;
if ($err == TLS_READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == TLS_WRITE) { $self->reactor->watch($handle, 1, 1) }
return;
}

# Connected
$self->_cleanup->emit_safe(connect => $handle);
}

# "Have you ever seen that Blue Man Group? Total ripoff of the Smurfs.
# And the Smurfs, well, they SUCK."
sub _connecting {
sub _try {
my ($self, $args) = @_;

# Retry or handle exceptions
Expand Down Expand Up @@ -123,22 +138,6 @@ sub _connecting {
$self->_cleanup->emit_safe(connect => $handle);
}

sub _tls {
my $self = shift;

# Switch between reading and writing
my $handle = $self->{handle};
if ($self->{tls} && !$handle->connect_SSL) {
my $err = $IO::Socket::SSL::SSL_ERROR;
if ($err == TLS_READ) { $self->reactor->watch($handle, 1, 0) }
elsif ($err == TLS_WRITE) { $self->reactor->watch($handle, 1, 1) }
return;
}

# Connected
$self->_cleanup->emit_safe(connect => $handle);
}

1;

=head1 NAME
Expand Down
5 changes: 1 addition & 4 deletions lib/Mojo/Loader.pm
Expand Up @@ -23,11 +23,8 @@ sub load {
# Check module name
return 1 if !$module || $module !~ /^\w(?:[\w:']*\w)?$/;

# Already loaded
return if $module->can('new');

# Load
return if eval "require $module; 1";
return if $module->can('new') || eval "require $module; 1";

# Exists
my $path = class_to_path $module;
Expand Down
13 changes: 6 additions & 7 deletions lib/Mojo/Template.pm
Expand Up @@ -123,13 +123,11 @@ sub compile {
# Compile
return unless my $code = $self->code;
my $compiled = eval $code;
$self->compiled($compiled) and return unless $@;

# Use local stacktrace for compile exceptions
return Mojo::Exception->new($@, [$self->template, $code], $self->name)
->trace->verbose(1)
if $@;

$self->compiled($compiled) and return;
->trace->verbose(1);
}

sub interpret {
Expand All @@ -144,9 +142,10 @@ sub interpret {
# Interpret
return unless my $compiled = $self->compiled;
my $output = eval { $compiled->(@_) };
return $@
? Mojo::Exception->new($@, [$self->template], $self->name)->verbose(1)
: $output;
return $output unless $@;

# Exception
return Mojo::Exception->new($@, [$self->template], $self->name)->verbose(1);
}

# "I am so smart! I am so smart! S-M-R-T! I mean S-M-A-R-T..."
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -24,9 +24,9 @@ sub endpoint {
my $port = $url->port || ($scheme eq 'https' ? 443 : 80);

# Proxy for normal HTTP requests
return $scheme eq 'http' && lc($req->headers->upgrade || '') ne 'websocket'
? $self->_proxy($tx, $scheme, $host, $port)
: ($scheme, $host, $port);
return $self->_proxy($tx, $scheme, $host, $port)
if $scheme eq 'http' && lc($req->headers->upgrade || '') ne 'websocket';
return $scheme, $host, $port;
}

sub form {
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious.pm
Expand Up @@ -148,8 +148,7 @@ sub handler {
@{$stash}{keys %$defaults} = values %$defaults;
my $c
= $self->controller_class->new(app => $self, stash => $stash, tx => $tx);
weaken $c->{app};
weaken $c->{tx};
weaken $c->{$_} for qw(app tx);

# Dispatcher
++$self->{dispatch} and $self->hook(around_dispatch => \&_dispatch)
Expand Down
26 changes: 11 additions & 15 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -77,7 +77,7 @@ sub finish {

# WebSocket
my $tx = $self->tx;
return $tx->finish if $tx->is_websocket;
$tx->finish and return $self if $tx->is_websocket;

# Chunked stream
if ($tx->res->is_chunked) {
Expand All @@ -87,7 +87,7 @@ sub finish {

# Normal stream
$self->write($chunk) if defined $chunk;
$self->write('');
return $self->write('');
}

# "You two make me ashamed to call myself an idiot."
Expand All @@ -111,7 +111,7 @@ sub on {
my ($self, $name, $cb) = @_;
my $tx = $self->tx;
$self->rendered(101) if $tx->is_websocket;
$tx->on($name => sub { shift and $self->$cb(@_) });
return $tx->on($name => sub { shift and $self->$cb(@_) });
}

# "Just make a simple cake. And this time, if someone's going to jump out of
Expand Down Expand Up @@ -266,7 +266,7 @@ sub render_exception {
# please support Proposition Infinity."
sub render_json { shift->render(json => @_) }

sub render_later { shift->stash->{'mojo.rendered'}++ }
sub render_later { shift->stash('mojo.rendered' => 1) }

# "Excuse me, sir, you're snowboarding off the trail.
# Lick my frozen metal ass."
Expand Down Expand Up @@ -308,16 +308,12 @@ sub render_text { shift->render(text => @_) }
sub rendered {
my ($self, $status) = @_;

# Disable auto rendering
$self->render_later;

# Make sure we have a status
my $res = $self->res;
$res->code($status) if $status;
# Disable auto rendering and make sure we have a status
my $res = $self->render_later->res;
$res->code($status || 200) if $status || !$res->code;

# Finish transaction
unless ($self->stash->{'mojo.finished'}++) {
$res->code(200) unless $res->code;
my $app = $self->app;
$app->plugins->emit_hook_reverse(after_dispatch => $self);
$app->sessions->store($self);
Expand Down Expand Up @@ -611,8 +607,8 @@ Access request cookie values and create new response cookies.
=head2 C<finish>
$c->finish;
$c->finish('Bye!');
$c = $c->finish;
$c = $c->finish('Bye!');
Gracefully end WebSocket connection or long poll stream.
Expand Down Expand Up @@ -758,7 +754,7 @@ C<stash>.
=head2 C<render_later>
$c->render_later;
$c = $c->render_later;
Disable automatic rendering, especially for long polling this can be quite
useful.
Expand Down Expand Up @@ -798,7 +794,7 @@ C<public> directory or C<DATA> section of your application.
=head2 C<render_text>
$c->render_text('Hello World!');
$c->render_text('Hello World', layout => 'green');
$c->render_text('Hello World!', layout => 'green');
Render the given content as Perl characters, which will be encoded to bytes.
All additional values get merged into the C<stash>. See C<render_data> for an
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Static.pm
Expand Up @@ -15,8 +15,8 @@ has paths => sub { [] };
my $MTIME = time;

# Bundled files
my $PUBLIC
= Mojo::Home->new(Mojo::Home->mojo_lib_dir)->rel_dir('Mojolicious/public');
my $PUBLIC = Mojo::Home->new(Mojo::Home->new->mojo_lib_dir)
->rel_dir('Mojolicious/public');

# "Valentine's Day's coming? Aw crap! I forgot to get a girlfriend again!"
sub dispatch {
Expand Down

0 comments on commit 254ef1c

Please sign in to comment.