Skip to content

Commit

Permalink
added on method to Mojolicious::Controller and message event to Mojo:…
Browse files Browse the repository at this point in the history
…:Log
  • Loading branch information
kraih committed Oct 29, 2011
1 parent 3d1b3e4 commit 9e7dd62
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 127 deletions.
6 changes: 6 additions & 0 deletions Changes
@@ -1,6 +1,12 @@
This file documents the revision history for Perl extension Mojolicious.

2.15 2011-10-29 00:00:00
- Deprecated Mojolicious::Controller->on_finish in favor of
Mojolicious::Controller->on.
- Removed Mojolicious::Controller->on_message, you can now use
Mojolicious::Controller->on instead.
$c->on(message => sub {...})
- Added EXPERIMENTAL message event to Mojo::Log.
- Improved documentation.

2.14 2011-10-29 00:00:00
Expand Down
2 changes: 1 addition & 1 deletion README.pod
Expand Up @@ -115,7 +115,7 @@ Web development for humans, making hard things possible and everything fun.
# WebSocket echo service
websocket '/echo' => sub {
my $self = shift;
$self->on_message(sub {
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
});
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Content.pm
Expand Up @@ -140,9 +140,8 @@ sub leftovers { shift->{buffer} }

# DEPRECATED in Smiling Face With Sunglasses!
sub on_read {
warn <<EOF;
Mojo::Content->on_read is DEPRECATED in favor of using Mojo::Content->on!
EOF
warn
"Mojo::Content->on_read is DEPRECATED in favor of Mojo::Content->on!\n";
shift->on(read => shift);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/HelloWorld.pm
Expand Up @@ -9,7 +9,7 @@ app->log->path(undef);

# "Does whisky count as beer?"
under '/diag' => sub {
shift->on_finish(sub { $ENV{MOJO_HELLO} = 'world' });
shift->on(finish => sub { $ENV{MOJO_HELLO} = 'world' });
};

any '/' => 'diag';
Expand Down Expand Up @@ -98,7 +98,7 @@ any '/upload' => sub {

any '/websocket' => sub {
my $self = shift;
$self->on_message(sub { shift->send_message(shift) })
$self->on(message => sub { shift->send_message(shift) })
if $self->tx->is_websocket;
};

Expand Down
62 changes: 44 additions & 18 deletions lib/Mojo/Log.pm
@@ -1,5 +1,5 @@
package Mojo::Log;
use Mojo::Base -base;
use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
Expand Down Expand Up @@ -28,6 +28,20 @@ has 'path';
# Supported log level
my $LEVEL = {debug => 1, info => 2, warn => 3, error => 4, fatal => 5};

sub new {
my $self = shift->SUPER::new(@_);
$self->on(
message => sub {
my $self = shift;
return unless my $handle = $self->handle;
flock $handle, LOCK_EX;
$handle->syswrite($self->format(@_));
flock $handle, LOCK_UN;
}
);
return $self;
}

# "Yes, I got the most! I win X-Mas!"
sub debug { shift->log('debug', @_) }
sub error { shift->log('error', @_) }
Expand Down Expand Up @@ -59,22 +73,10 @@ sub is_warn { shift->is_level('warn') }
# "If The Flintstones has taught us anything,
# it's that pelicans can be used to mix cement."
sub log {
my ($self, $level, @msgs) = @_;

# Check log level
$level = lc $level;
my $self = shift;
my $level = lc shift;
return $self unless $level && $self->is_level($level);

# Lock
my $handle = $self->handle;
flock $handle, LOCK_EX;

# Format and log messages
$handle->syswrite($self->format($level, @msgs));

# Unlock
flock $handle, LOCK_UN;

$self->emit(message => $level => @_);
return $self;
}

Expand Down Expand Up @@ -110,6 +112,24 @@ Mojo::Log - Simple logger for Mojo
L<Mojo::Log> is a simple logger for L<Mojo> projects.
=head1 EVENTS
L<Mojo::Log> can emit the following events.
=head2 C<message>
$log->on(message => sub {
my ($log, $level, @messages) = @_;
});
Emitted when a new message gets logged.
Note that this event is EXPERIMENTAL and might change without warning!
$log->on(message => sub {
my ($log, $level, @messages) = @_;
say "$level: ", join("\n", @messages);
});
=head1 ATTRIBUTES
L<Mojo::Log> implements the following attributes.
Expand Down Expand Up @@ -137,8 +157,14 @@ Logfile path.
=head1 METHODS
L<Mojo::Log> inherits all methods from L<Mojo::Base> and implements the
following new ones.
L<Mojo::Log> inherits all methods from L<Mojo::EventEmitter> and implements
the following new ones.
=head2 C<new>
my $log = Mojo::Log->new;
Construct a new L<Mojo::Log> object.
=head2 C<debug>
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Message.pm
Expand Up @@ -312,16 +312,15 @@ sub max_line_size { shift->headers->max_line_size(@_) }

# DEPRECATED in Smiling Face With Sunglasses!
sub on_finish {
warn <<EOF;
Mojo::Message->on_finish is DEPRECATED in favor of using Mojo::Message->on!
EOF
warn
"Mojo::Message->on_finish is DEPRECATED in favor of Mojo::Message->on!\n";
shift->on(finish => shift);
}

# DEPRECATED in Smiling Face With Sunglasses!
sub on_progress {
warn <<EOF;
Mojo::Message->on_progress is DEPRECATED in favor of using Mojo::Message->on!
Mojo::Message->on_progress is DEPRECATED in favor of Mojo::Message->on!
EOF
shift->on(progress => shift);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Server.pm
Expand Up @@ -53,9 +53,8 @@ EOF

# DEPRECATED in Smiling Face With Sunglasses!
sub on_request {
warn <<EOF;
Mojo::Server->on_request is DEPRECATED in favor of using Mojo::Server->on!
EOF
warn
"Mojo::Server->on_request is DEPRECATED in favor of Mojo::Server->on!\n";
shift->on(request => shift);
}

Expand Down
6 changes: 2 additions & 4 deletions lib/Mojo/Transaction.pm
Expand Up @@ -41,17 +41,15 @@ sub is_writing {
# DEPRECATED in Smiling Face With Sunglasses!
sub on_finish {
warn <<EOF;
Mojo::Transaction->on_finish is DEPRECATED in favor of using
Mojo::Transaction->on!
Mojo::Transaction->on_finish is DEPRECATED in favor of Mojo::Transaction->on!
EOF
shift->on(finish => shift);
}

# DEPRECATED in Smiling Face With Sunglasses!
sub on_resume {
warn <<EOF;
Mojo::Transaction->on_resume is DEPRECATED in favor of using
Mojo::Transaction->on!
Mojo::Transaction->on_resume is DEPRECATED in favor of Mojo::Transaction->on!
EOF
shift->on(resume => shift);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -143,7 +143,7 @@ sub keep_alive {
# DEPRECATED in Smiling Face With Sunglasses!
sub on_request {
warn <<EOF;
Mojo::Transaction::HTTP->on_request is DEPRECATED in favor of using
Mojo::Transaction::HTTP->on_request is DEPRECATED in favor of
Mojo::Transaction::HTTP->on!
EOF
shift->on(request => shift);
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -85,8 +85,7 @@ sub need_proxy {
# DEPRECATED in Smiling Face With Sunglasses!
sub on_start {
warn <<EOF;
Mojo::UserAgent->on_start is DEPRECATED in favor of using
Mojo::UserAgent->on!
Mojo::UserAgent->on_start is DEPRECATED in favor of Mojo::UserAgent->on!
EOF
shift->on(start => shift);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojolicious.pm
Expand Up @@ -72,12 +72,12 @@ sub new {
$r->namespace(ref $self);

# Hide own controller methods
$r->hide(qw/AUTOLOAD DESTROY client cookie delayed finish finished/);
$r->hide(qw/flash handler helper on_message param redirect_to render/);
$r->hide(qw/render_content render_data render_exception render_json/);
$r->hide(qw/render_not_found render_partial render_static render_text/);
$r->hide(qw/rendered send_message session signed_cookie url_for/);
$r->hide(qw/write write_chunk/);
$r->hide(qw/AUTOLOAD DESTROY app cookie finish flash handler on on_finish/);
$r->hide(qw/param redirect_to render render_content render_data/);
$r->hide(qw/render_exception render_json render_not_found render_partial/);
$r->hide(qw/render_static render_text rendered req res respond_to/);
$r->hide(qw/send_message session signed_cookie stash tx ua url_for write/);
$r->hide('write_chunk');

# Prepare log
my $mode = $self->mode;
Expand Down
48 changes: 22 additions & 26 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -142,20 +142,20 @@ sub flash {
}

# "My parents may be evil, but at least they're stupid."
sub on_finish {
my ($self, $cb) = @_;
return $self->tx->on(finish => sub { shift and $self->$cb(@_) });
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(@_) });
}

# "I like being a women.
# Now when I say something stupid, everyone laughs and buys me things."
sub on_message {
my ($self, $cb) = @_;
my $tx = $self->tx;
Carp::croak('No WebSocket connection to receive messages from')
unless $tx->is_websocket;
$self->rendered(101);
return $tx->on(message => sub { shift and $self->$cb(@_) });
# DEPRECATED in Leaf Fluttering In Wind!
sub on_finish {
warn <<EOF;
Mojolicious::Controller->on_finish is DEPRECATED in favor of
Mojolicious::Controller->on!
EOF
shift->on(finish => @_);
}

# "Just make a simple cake. And this time, if someone's going to jump out of
Expand Down Expand Up @@ -738,27 +738,23 @@ Gracefully end WebSocket connection or long poll stream.
Data storage persistent only for the next request, stored in the session.
=head2 C<on_finish>
=head2 C<on>
my $cb = $c->on_finish(sub {...});
my $cb = $c->on(finish => sub {...});
Register C<finish> event with transaction, which will be emitted when the
transaction has been finished.
Register event with C<tx>, which is usually a L<Mojo::Transaction::HTTP> or
L<Mojo::Transaction::WebSocket> object.
$c->on_finish(sub {
# Emitted when the transaction has been finished
$c->on(finish => sub {
my $c = shift;
say 'We are done!';
});
=head2 C<on_message>
my $cb = $c->on_message(sub {...});
Register C<message> event with transaction, which will be emitted when new
WebSocket messages arrive.
Note that this method is EXPERIMENTAL and might change without warning!
$c->on_message(sub {
# Emitted when new WebSocket messages arrive
$c->on(message => sub {
my ($c, $message) = @_;
say "Message: $message";
});
=head2 C<param>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -764,7 +764,7 @@ You can restrict access to WebSocket handshakes using the C<websocket> method.
# Action
sub echo {
my $self = shift;
$self->on_message(sub {
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
});
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -701,7 +701,7 @@ WebSocket applications have never been this easy before.
websocket '/echo' => sub {
my $self = shift;
$self->on_message(sub {
$self->on(message => sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
});
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -15,7 +15,7 @@ has cache => sub { Mojo::Cache->new };
has [qw/children conditions/] => sub { [] };
has controller_base_class => 'Mojolicious::Controller';
has [qw/dictionary shortcuts/] => sub { {} };
has hidden => sub { [qw/new app attr has render req res stash tx/] };
has hidden => sub { [qw/new attr has/] };
has pattern => sub { Mojolicious::Routes::Pattern->new };

# "Yet thanks to my trusty safety sphere,
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious/Static.pm
Expand Up @@ -125,6 +125,8 @@ sub serve {
return 1;
}

# "I like being a women.
# Now when I say something stupid, everyone laughs and buys me things."
sub _get_data_file {
my ($self, $c, $rel) = @_;

Expand Down
8 changes: 4 additions & 4 deletions t/mojo/psgi.t
Expand Up @@ -40,9 +40,9 @@ is $headers{'Content-Length'}, 43, 'right "Content-Length" value';
is $headers{'Content-Type'}, 'application/json', 'right "Content-Type" value';
my $params = '';
while (defined(my $chunk = $res->[2]->getline)) { $params .= $chunk }
is $ENV{MOJO_HELLO}, undef, 'on_finish not yet called';
is $ENV{MOJO_HELLO}, undef, 'finish event has not been emitted';
$res->[2]->close;
is delete $ENV{MOJO_HELLO}, 'world', 'on_finish called';
is delete $ENV{MOJO_HELLO}, 'world', 'finish event has been emitted';
$params = Mojo::JSON->new->decode($params);
is_deeply $params, {bar => 'baz', hello => 'world', lalala => 23},
'right structure';
Expand Down Expand Up @@ -77,9 +77,9 @@ is $headers{'Content-Length'}, 43, 'right "Content-Length" value';
is $headers{'Content-Type'}, 'application/json', 'right "Content-Type" value';
$params = '';
while (defined(my $chunk = $res->[2]->getline)) { $params .= $chunk }
is $ENV{MOJO_HELLO}, undef, 'on_finish not yet called';
is $ENV{MOJO_HELLO}, undef, 'finish event has not been emitted';
$res->[2]->close;
is delete $ENV{MOJO_HELLO}, 'world', 'on_finish called';
is delete $ENV{MOJO_HELLO}, 'world', 'finish event has been emitted';
$params = Mojo::JSON->new->decode($params);
is_deeply $params, {bar => 'baz', world => 'hello', lalala => 23},
'right structure';
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/user_agent.t
Expand Up @@ -26,7 +26,7 @@ my $timeout = undef;
get '/timeout' => sub {
my $self = shift;
Mojo::IOLoop->connection_timeout($self->tx->connection => '0.5');
$self->on_finish(sub { $timeout = 1 });
$self->on(finish => sub { $timeout = 1 });
$self->render_later;
};

Expand Down Expand Up @@ -250,7 +250,7 @@ is $tx->res->body, 'works', 'right content';
$tx = $ua->get('/timeout');
ok !$tx->success, 'not successful';
is $tx->error, 'Premature connection close.', 'right error';
is $timeout, 1, 'on_finish was called';
is $timeout, 1, 'finish event has been emitted';

# Nested keep alive
my @kept_alive;
Expand Down

0 comments on commit 9e7dd62

Please sign in to comment.