Skip to content

Commit

Permalink
Mojo::EventEmitter->unsubscribe has won
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 14, 2012
1 parent 35e8fa4 commit f4cdc97
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 61 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,8 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.62 2012-03-15 00:00:00
- Deprecated Mojo::EventEmitter->unsubscribe in favor of
Mojo::EventEmitter->off.
- Improved Mojo::IOWatcher exception handling a little.
- Improved documentation.
- Improved tests.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content.pm
Expand Up @@ -445,7 +445,7 @@ Emitted once all headers have been parsed and the body starts.
Emitted when a new chunk of content arrives.
$content->off('read');
$content->unsubscribe('read');
$content->on(read => sub {
my ($content, $chunk) = @_;
say "Streaming: $chunk";
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/Single.pm
Expand Up @@ -54,7 +54,7 @@ sub parse {

# Content needs to be upgraded to multipart
if ($self->auto_upgrade && defined($self->boundary)) {
$self->off(read => $self->{read});
$self->unsubscribe(read => $self->{read});
$self->emit(upgrade => my $multi = Mojo::Content::MultiPart->new($self));
return $multi->parse;
}
Expand Down
57 changes: 24 additions & 33 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -42,24 +42,6 @@ sub emit_safe {

sub has_subscribers { scalar @{shift->subscribers(shift)} }

# "Back you robots!
# Nobody ruins my family vacation but me!
# And maybe the boy."
sub off {
my ($self, $name, $cb) = @_;

# All
unless ($cb) {
delete $self->{events}->{$name};
return $self;
}

# One
$self->{events}->{$name} = [grep { $cb ne $_ } @{$self->{events}->{$name}}];

return $self;
}

sub on {
my ($self, $name, $cb) = @_;
push @{$self->{events}->{$name} ||= []}, $cb;
Expand All @@ -72,7 +54,7 @@ sub once {
weaken $self;
my $wrapper;
$wrapper = sub {
$self->off($name => $wrapper);
$self->unsubscribe($name => $wrapper);
$cb->(@_);
};
$self->on($name => $wrapper);
Expand All @@ -83,13 +65,22 @@ sub once {

sub subscribers { shift->{events}->{shift()} || [] }

# DEPRECATED in Leaf Fluttering In Wind!
# "Back you robots!
# Nobody ruins my family vacation but me!
# And maybe the boy."
sub unsubscribe {
warn <<EOF;
Mojo::EventEmitter->unsubscribe is DEPRECATED in favor of
Mojo::EventEmitter->off!
EOF
shift->off(@_);
my ($self, $name, $cb) = @_;

# All
unless ($cb) {
delete $self->{events}->{$name};
return $self;
}

# One
$self->{events}->{$name} = [grep { $cb ne $_ } @{$self->{events}->{$name}}];

return $self;
}

1;
Expand Down Expand Up @@ -150,13 +141,6 @@ is EXPERIMENTAL and might change without warning!
Check if event has subscribers.
=head2 C<off>
$e = $e->off('foo');
$e = $e->off(foo => $cb);
Unsubscribe from event.
=head2 C<on>
my $cb = $e->on(foo => sub {...});
Expand Down Expand Up @@ -186,7 +170,14 @@ Subscribe to event and unsubscribe again after it has been emitted once.
All subscribers for event.
# Unsubscribe last subscriber
$e->off(foo => $e->subscribers('foo')->[-1]);
$e->unsubscribe(foo => $e->subscribers('foo')->[-1]);
=head2 C<unsubscribe>
$e = $e->unsubscribe('foo');
$e = $e->unsubscribe(foo => $cb);
Unsubscribe from event.
=head1 DEBUGGING
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Log.pm
Expand Up @@ -127,7 +127,7 @@ L<Mojo::Log> can emit the following events.
Emitted when a new message gets logged.
$log->off('message');
$log->unsubscribe('message');
$log->on(message => sub {
my ($log, $level, @messages) = @_;
say "$level: ", @messages;
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Message.pm
Expand Up @@ -53,7 +53,8 @@ sub body {
# Callback
if (ref $new eq 'CODE') {
weaken $self;
return $content->off('read')->on(read => sub { $self->$new(pop) });
return $content->unsubscribe('read')
->on(read => sub { $self->$new(pop) });
}

# Set text content
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server.pm
Expand Up @@ -99,7 +99,7 @@ L<Mojo::Server> can emit the following events.
Emitted when a request is ready and needs to be handled.
$server->off('request');
$server->unsubscribe('request');
$server->on(request => sub {
my ($server, $tx) = @_;
$tx->res->code(200);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/CGI.pm
Expand Up @@ -109,7 +109,7 @@ Mojo::Server::CGI - CGI server
use Mojo::Server::CGI;
my $cgi = Mojo::Server::CGI->new;
$cgi->off('request')
$cgi->unsubscribe('request')
$cgi->on(request => sub {
my ($cgi, $tx) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Daemon.pm
Expand Up @@ -338,7 +338,7 @@ Mojo::Server::Daemon - Non-blocking I/O HTTP 1.1 and WebSocket server
use Mojo::Server::Daemon;
my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
$daemon->off('request');
$daemon->unsubscribe('request');
$daemon->on(request => sub {
my ($daemon, $tx) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/PSGI.pm
Expand Up @@ -92,7 +92,7 @@ Mojo::Server::PSGI - PSGI server
use Mojo::Server::PSGI;
my $psgi = Mojo::Server::PSGI->new;
$psgi->off('request');
$psgi->unsubscribe('request');
$psgi->on(request => sub {
my ($psgi, $tx) = @_;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -510,7 +510,7 @@ HTTP protocol for transport.
# Unsubscribe from "message" event again once we are done
$self->on(finish => sub {
my $self = shift;
$self->app->log->off(message => $cb);
$self->app->log->unsubscribe(message => $cb);
});
};

Expand Down Expand Up @@ -570,7 +570,7 @@ which can be combined to solve some of hardest problems in web development.

# Make sure we have the right part and replace "read" event
return unless $single->headers->content_disposition =~ /example/;
$single->off('read')->on(read => sub {
$single->unsubscribe('read')->on(read => sub {
my ($single, $chunk) = @_;

# Log size of every chunk we receive
Expand Down Expand Up @@ -753,7 +753,7 @@ L<Mojo::UserAgent> makes it actually easy.
my $tx = $ua->build_tx(GET => 'http://mojolicio.us');

# Replace "read" events to disable default content parser
$tx->res->content->off('read')->on(read => sub {
$tx->res->content->unsubscribe('read')->on(read => sub {
my ($content, $chunk) = @_;
say "Streaming: $chunk";
});
Expand Down
10 changes: 5 additions & 5 deletions t/mojo/eventemitter.t
Expand Up @@ -34,7 +34,7 @@ is $echo, 'echo: works!echo2: works!', 'right echo';
is $err, qq/Event "test2" failed: test2: works!\n/, 'right error';
$echo = $err = undef;
is scalar @{$e->subscribers('test2')}, 3, 'three subscribers';
$e->off(test2 => $cb);
$e->unsubscribe(test2 => $cb);
is scalar @{$e->subscribers('test2')}, 2, 'two subscribers';
$e->emit_safe('test2', 'works!');
is $echo, 'echo: works!', 'right echo';
Expand All @@ -45,7 +45,7 @@ $e->emit('test1');
is $called, 2, 'event was emitted twice';
is scalar @{$e->subscribers('test1')}, 1, 'one subscriber';
$e->emit('test1');
$e->off(test1 => $e->subscribers('test1')->[0]);
$e->unsubscribe(test1 => $e->subscribers('test1')->[0]);
is $called, 3, 'event was emitted three times';
is scalar @{$e->subscribers('test1')}, 0, 'no subscribers';
$e->emit('test1');
Expand Down Expand Up @@ -115,15 +115,15 @@ my $counter = 0;
$cb = $e->on(foo => sub { $counter++ });
$e->on(foo => sub { $counter++ });
$e->on(foo => sub { $counter++ });
$e->off(foo => $e->once(foo => sub { $counter++ }));
$e->unsubscribe(foo => $e->once(foo => sub { $counter++ }));
is scalar @{$e->subscribers('foo')}, 3, 'three subscribers';
$e->emit('foo')->off(foo => $cb);
$e->emit('foo')->unsubscribe(foo => $cb);
is $counter, 3, 'event was emitted three times';
is scalar @{$e->subscribers('foo')}, 2, 'two subscribers';
$e->emit('foo');
is $counter, 5, 'event was emitted two times';
ok $e->has_subscribers('foo'), 'has subscribers';
ok !$e->off('foo')->has_subscribers('foo'), 'no subscribers';
ok !$e->unsubscribe('foo')->has_subscribers('foo'), 'no subscribers';
is scalar @{$e->subscribers('foo')}, 0, 'no subscribers';
$e->emit('foo');
is $counter, 5, 'event was not emitted again';
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/response.t
Expand Up @@ -622,7 +622,7 @@ is $res->body, 'hi there!', 'right content';
ok $res->content->has_subscribers('read'), 'has subscribers';
$cb = $res->body(sub { });
ok $res->content->has_subscribers('read'), 'has subscribers';
$res->content->off(read => $cb);
$res->content->unsubscribe(read => $cb);
ok !$res->content->has_subscribers('read'), 'no subscribers';
$res->body('');
is $res->body, '', 'right content';
Expand All @@ -631,7 +631,7 @@ is $res->body, 0, 'right content';
ok !$res->content->has_subscribers('read'), 'no subscribers';
$cb = $res->body(sub { });
ok $res->content->has_subscribers('read'), 'has subscribers';
$res->content->off(read => $cb);
$res->content->unsubscribe(read => $cb);
$res->body('hello!');
ok !$res->content->has_subscribers('read'), 'no subscribers';
is $res->body, 'hello!', 'right content';
Expand Down
10 changes: 5 additions & 5 deletions t/mojo/user_agent.t
Expand Up @@ -119,7 +119,7 @@ is $body, 'works!', 'right content';

# Error in callback is logged
my $message = app->log->subscribers('message')->[0];
app->log->off(message => $message);
app->log->unsubscribe(message => $message);
app->log->level('error');
app->ua->once(error => sub { Mojo::IOLoop->stop });
ok app->ua->has_subscribers('error'), 'has subscribers';
Expand Down Expand Up @@ -184,7 +184,7 @@ is $tx->res->body, 'works!', 'right content';
# GET /timeout (built-in web server times out)
my $log = '';
$message = app->log->subscribers('message')->[0];
app->log->off(message => $message);
app->log->unsubscribe(message => $message);
app->log->level('error');
app->log->on(message => sub { $log .= pop });
$tx = $ua->get('/timeout?timeout=0.5');
Expand Down Expand Up @@ -234,8 +234,8 @@ my $start = $ua->on(
);
$tx->on(
finish => sub {
$stream->off(read => $read);
$stream->off(write => $write);
$stream->unsubscribe(read => $read);
$stream->unsubscribe(write => $write);
}
);
}
Expand All @@ -252,7 +252,7 @@ is scalar @{Mojo::IOLoop->stream($tx->connection)->subscribers('read')}, 1,
'unsubscribed successfully';
like $req, qr#^GET / .*whatever$#s, 'right request';
like $res, qr#^HTTP/.*200 OK.*works!$#s, 'right response';
$ua->off(start => $start);
$ua->unsubscribe(start => $start);
ok !$ua->has_subscribers('start'), 'unsubscribed successfully';

# GET /echo (stream with drain callback)
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket.t
Expand Up @@ -504,7 +504,7 @@ is $result, 'hi' x 200000, 'right result';
# WebSocket /timeout
my $log = '';
$message = app->log->subscribers('message')->[0];
app->log->off(message => $message);
app->log->unsubscribe(message => $message);
app->log->level('error');
app->log->on(message => sub { $log .= pop });
$ua->websocket(
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket_proxy_tls.t
Expand Up @@ -178,7 +178,7 @@ Mojo::IOLoop->start;
is $result, "Hello World! / https://localhost:$port/", 'right content';
is $works, 'it does!', 'right header';
is $start, 2, 'redirected once';
$ua->off('start');
$ua->unsubscribe('start');

# WebSocket /test (normal websocket)
$result = undef;
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/upload_stream_lite_app.t
Expand Up @@ -40,7 +40,7 @@ post '/upload/:id' => sub {
body => sub {
my $single = shift;
return unless $single->headers->content_disposition =~ /my_file/;
$single->off('read');
$single->unsubscribe('read');
$single->on(read => sub { $cache->{$id} .= pop });
}
);
Expand Down

0 comments on commit f4cdc97

Please sign in to comment.