Skip to content

Commit

Permalink
removed experimental status from Mojo::EventEmitter and merged unsubs…
Browse files Browse the repository at this point in the history
…cribe_all method into unsubscribe
  • Loading branch information
kraih committed Oct 29, 2011
1 parent d323417 commit 4b790dc
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@
This file documents the revision history for Perl extension Mojolicious.

2.16 2011-10-30 00:00:00
- Removed experimental status from Mojo::EventEmitter.
- Merged unsubscribe and unsubscribe_all methods in
Mojo::EventEmitter.
- Improved documentation.

2.15 2011-10-29 00:00:00
Expand Down
24 changes: 10 additions & 14 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -46,6 +46,13 @@ sub subscribers { shift->{events}->{shift()} || [] }
sub unsubscribe {
my ($self, $name, $cb) = @_;

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

# One
my @callbacks;
for my $subscriber (@{$self->subscribers($name)}) {
next if $cb eq $subscriber;
Expand All @@ -56,12 +63,6 @@ sub unsubscribe {
return $self;
}

sub unsubscribe_all {
my ($self, $name) = @_;
$self->unsubscribe($name => $_) for @{$self->subscribers($name)};
return $self;
}

sub _emit {
my $self = shift;
my $safe = shift;
Expand Down Expand Up @@ -117,7 +118,6 @@ Mojo::EventEmitter - Event emitter base class
=head1 DESCRIPTION
L<Mojo::EventEmitter> is a simple base class for event emitting objects.
Note that this module is EXPERIMENTAL and might change without warning!
=head1 METHODS
Expand All @@ -137,6 +137,7 @@ Emit event.
$e = $e->emit_safe('foo', 123);
Emit event safely and emit C<error> event on failure.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<has_subscribers>
Expand Down Expand Up @@ -164,16 +165,11 @@ All subscribers for event.
=head2 C<unsubscribe>
$e->unsubscribe(foo => $cb);
$e = $e->unsubscribe('foo');
$e = $e->unsubscribe(foo => $cb);
Unsubscribe from event.
=head2 C<unsubscribe_all>
$e->unsubscribe_all('foo');
Remove all subscribers from event.
=head1 DEBUGGING
You can set the C<MOJO_EVENTEMITTER_DEBUG> environment variable to get some
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Log.pm
Expand Up @@ -125,7 +125,7 @@ L<Mojo::Log> can emit the following events.
Emitted when a new message gets logged.
Note that this event is EXPERIMENTAL and might change without warning!
$log->unsubscribe_all('message');
$log->unsubscribe('message');
$log->on(message => sub {
my ($log, $level, @messages) = @_;
say "$level: ", @messages;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server.pm
Expand Up @@ -105,7 +105,7 @@ L<Mojo::Server> can emit the following events.
Emitted when a request is ready and needs to be handled.
$server->unsubscribe_all('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->unsubscribe_all('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 @@ -345,7 +345,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->unsubscribe_all('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 @@ -89,7 +89,7 @@ Mojo::Server::PSGI - PSGI server
use Mojo::Server::PSGI;
my $psgi = Mojo::Server::PSGI->new;
$psgi->unsubscribe_all('request');
$psgi->unsubscribe('request');
$psgi->on(request => sub {
my ($psgi, $tx) = @_;
Expand Down
8 changes: 3 additions & 5 deletions t/mojo/eventemitter.t
Expand Up @@ -110,23 +110,21 @@ is $once, 1, 'event was not emitted again';
$e->emit('one_time');
is $once, 1, 'event was not emitted again';

# Unsubscribe
# Remove subscribers
$e = Mojo::EventEmitter->new;
my $counter = 0;
$cb = $e->on(foo => sub { $counter++ });
$e->on(foo => sub { $counter++ });
$e->on(foo => sub { $counter++ });
$e->unsubscribe(foo => $e->once(foo => sub { $counter++ }));
is scalar @{$e->subscribers('foo')}, 3, 'three subscribers';
$e->emit('foo');
$e->emit('foo')->unsubscribe(foo => $cb);
is $counter, 3, 'event was emitted three times';
$e->unsubscribe(foo => $cb);
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';
$e->unsubscribe_all('foo');
ok !$e->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
2 changes: 1 addition & 1 deletion t/mojo/user_agent.t
Expand Up @@ -148,7 +148,7 @@ $ua->on(
}
);
$tx = $ua->start($tx);
$ua->unsubscribe_all('start');
$ua->unsubscribe('start');
ok $tx->success, 'successful';
is $finished, 1, 'finish event has been emitted';
is $tx->res->code, 200, 'right status';
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket_proxy_tls.t
Expand Up @@ -159,7 +159,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->unsubscribe_all('start');
$ua->unsubscribe('start');

# WebSocket /test (normal websocket)
$result = undef;
Expand Down

0 comments on commit 4b790dc

Please sign in to comment.