Skip to content

Commit

Permalink
added catch method to Mojo::EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 24, 2014
1 parent db69f6a commit 76f93f9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,5 @@

5.0 2014-05-21
5.0 2014-05-24
- Code name "Tiger Face", this is a major release.
- Changed heuristics for number detection in Mojo::JSON to better line up
with user expectations.
Expand All @@ -11,6 +11,7 @@
- Removed check_file method from Mojo::Server::Morbo.
- Added format attribute to Mojo::Log.
- Added with_compression method to Mojo::Transaction::WebSocket.
- Added catch method to Mojo::EventEmitter.
- Added append method to Mojo::Log.
- Added check method to Mojo::Server::Morbo.
- Updated jQuery to version 2.1.1.
Expand Down
11 changes: 11 additions & 0 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -5,6 +5,8 @@ use Scalar::Util qw(blessed weaken);

use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;

sub catch { $_[0]->on(error => $_[1]) and return $_[0] }

sub emit {
my ($self, $name) = (shift, shift);

Expand Down Expand Up @@ -135,6 +137,15 @@ Emitted for event errors, fatal if unhandled.
L<Mojo::EventEmitter> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 catch
$e = $e->catch(sub {...});
Subscribe to C<error> event.
# Longer version
$e->on(error => sub {...});
=head2 emit
$e = $e->emit('foo');
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/delay.t
Expand Up @@ -266,7 +266,6 @@ ok !$finished, 'finish event has not been emitted';
# Exception in second step
($failed, $finished, $result) = ();
$delay = Mojo::IOLoop::Delay->new;
$delay->on(error => sub { $failed = pop });
$delay->on(finish => sub { $finished++ });
$delay->steps(
sub {
Expand All @@ -275,7 +274,7 @@ $delay->steps(
},
sub { die 'Second step!' },
sub { $result = 'failed' }
);
)->catch(sub { $failed = pop });
$delay->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Second step!/, 'right error';
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/eventemitter.t
Expand Up @@ -32,8 +32,7 @@ like $@, qr/^Mojo::EventEmitter: Event "error" failed: intentional/,

# Error fallback
my ($echo, $err);
$e->on(error => sub { $err = pop });
$e->on(test2 => sub { $echo .= 'echo: ' . pop });
$e->catch(sub { $err = pop })->on(test2 => sub { $echo .= 'echo: ' . pop });
$e->on(
test2 => sub {
my ($self, $msg) = @_;
Expand Down

0 comments on commit 76f93f9

Please sign in to comment.