Skip to content

Commit

Permalink
renamed emit_hook_chain to emit_chain and changed event order from LI…
Browse files Browse the repository at this point in the history
…FO to FIFO
  • Loading branch information
kraih committed Nov 19, 2011
1 parent bc9255b commit 0af28df
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -3,7 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.29 2011-11-19 00:00:00
- Deprecated Mojolicious->on_process in favor of around_dispatch
hook.
- Added EXPERIMENTAL emit_hook_chain method to Mojolicious::Plugins.
- Added EXPERIMENTAL emit_chain method to Mojolicious::Plugins.
(Akron, sri)
- Added EXPERIMENTAL around_dispatch hook.
- Fixed small bug in boundary and charset methods of Mojo::Content.
Expand Down
11 changes: 3 additions & 8 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -26,11 +26,11 @@ sub on {
sub once {
my ($self, $name, $cb) = @_;

weaken $self;
my $wrapper;
$wrapper = sub {
my $self = shift;
$self->unsubscribe($name => $wrapper);
$self->$cb(@_);
$cb->(@_);
};
$self->on($name => $wrapper);
weaken $wrapper;
Expand All @@ -53,12 +53,7 @@ sub unsubscribe {
}

# One
my @callbacks;
for my $subscriber (@{$self->subscribers($name)}) {
next if $cb eq $subscriber;
push @callbacks, $subscriber;
}
$self->{events}->{$name} = \@callbacks;
$self->{events}->{$name} = [grep { $cb ne $_ } @{$self->{events}->{$name}}];

return $self;
}
Expand Down
49 changes: 25 additions & 24 deletions lib/Mojolicious.pm
Expand Up @@ -93,19 +93,6 @@ sub new {
# Reduced log output outside of development mode
$self->log->level('info') unless $mode eq 'development';

# Default dispatcher
$self->hook(
around_dispatch => sub {
my ($next, $c) = @_;

# DEPRECATED in Leaf Fluttering In Wind!
my $cb = $self->on_process;
$c->app->$cb($c);

$next->();
}
);

# Run mode
$mode = $mode . '_mode';
$self->$mode(@_) if $self->can($mode);
Expand Down Expand Up @@ -180,14 +167,29 @@ sub handler {
$tx = $tx->tx;
}

# Build default controller and process
# Build default controller
my $defaults = $self->defaults;
@{$stash}{keys %$defaults} = values %$defaults;
my $c =
$self->controller_class->new(app => $self, stash => $stash, tx => $tx);
weaken $c->{app};
weaken $c->{tx};
unless (eval { $self->plugins->emit_hook_chain(around_dispatch => $c) }) {

# Dispatcher
unless ($self->{dispatcher}) {
$self->hook(
around_dispatch => sub {
my ($next, $c) = @_;

# DEPRECATED in Leaf Fluttering In Wind!
$self->on_process->($c->app, $c);
}
);
$self->{dispatcher}++;
}

# Process
unless (eval { $self->plugins->emit_chain(around_dispatch => $c) }) {
$self->log->fatal("Processing request failed: $@");
$tx->res->code(500);
$tx->resume;
Expand Down Expand Up @@ -221,14 +223,13 @@ sub hook { shift->plugins->on(@_) }
# DEPRECATED in Leaf Fluttering In Wind!
sub on_process {
my ($self, $cb) = @_;
if ($cb) {
warn <<EOF;
return $self->{on_process} ||= sub { shift->dispatch(@_) }
unless $cb;
warn <<EOF;
Mojolicious->on_process is DEPRECATED in favor of the around_dispatch hook!
EOF
$self->{on_process} = $cb;
return $self;
}
return $self->{on_process} ||= sub { shift->dispatch(@_) };
$self->{on_process} = $cb;
return $self;
}

sub plugin {
Expand Down Expand Up @@ -519,9 +520,9 @@ Useful for all kinds of postprocessing tasks.
=item around_dispatch
Emitted in reverse order right before the C<before_dispatch> hook and wraps
around the whole dispatch process, so you have to manually forward to the
next hook if you want to continue the chain.
Emitted right before the C<before_dispatch> hook and wraps around the whole
dispatch process, so you have to manually forward to the next hook if you
want to continue the chain.
Note that this hook is EXPERIMENTAL and might change without warning!
$app->hook(around_dispatch => sub {
Expand Down
20 changes: 10 additions & 10 deletions lib/Mojolicious/Plugins.pm
Expand Up @@ -22,12 +22,12 @@ sub emit_hook {
return $self;
}

sub emit_hook_chain {
sub emit_chain {
my ($self, $name, @args) = @_;
my @subscribers = @{$self->subscribers($name)};
my $cb;
$cb = sub {
return unless my $next = pop @subscribers;
return unless my $next = shift @subscribers;
$next->($cb, @args);
undef $cb;
};
Expand Down Expand Up @@ -136,21 +136,21 @@ Namespaces to load plugins from.
L<Mojolicious::Plugins> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 C<emit_chain>
$plugins = $plugins->emit_chain('foo');
$plugins = $plugins->emit_chain(foo => 123);
Emit events as chained hooks.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<emit_hook>
$plugins = $plugins->emit_hook('foo');
$plugins = $plugins->emit_hook(foo => 123);
Emit events as hooks.
=head2 C<emit_hook_chain>
$plugins = $plugins->emit_hook_chain('foo');
$plugins = $plugins->emit_hook_chain(foo => 123);
Emit events as hooks in a chain.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<emit_hook_reverse>
$plugins = $plugins->emit_hook_reverse('foo');
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/eventemitter.t
Expand Up @@ -86,9 +86,9 @@ ok !$once, 'no subscribers';
$once = 0;
$e->once(
one_time => sub {
$e->once(
shift->once(
one_time => sub {
$e->once(one_time => sub { $once++ });
shift->once(one_time => sub { $once++ });
}
);
}
Expand Down
8 changes: 4 additions & 4 deletions t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -17,16 +17,16 @@ use Test::Mojo;
# Wrap whole application
hook around_dispatch => sub {
my ($next, $self) = @_;
return $self->render(text => 'Wrapped!')
if $self->req->url->path->contains('/wrap');
return $self->render(text => 'Wrapped again!')
if $self->req->url->path->contains('/wrap/again');
$next->();
};

# Wrap whole application again
hook around_dispatch => sub {
my ($next, $self) = @_;
return $self->render(text => 'Wrapped again!')
if $self->req->url->path->contains('/wrap/again');
return $self->render(text => 'Wrapped!')
if $self->req->url->path->contains('/wrap');
$next->();
};

Expand Down

0 comments on commit 0af28df

Please sign in to comment.