Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr committed Aug 4, 2011
0 parents commit 04ba59b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
@@ -0,0 +1,49 @@
Mojolicious::Plugin::Parallol
=============================

Because parallel request should be as fun as parallololololol!

## Synopsis

Simply wrap your callbacks in `$self->parallol()` and the response will
only render when *all* your callbacks are done:

```perl
use Mojolicious::Lite;

plugin 'parallol';

get '/' => sub {
my $self = shift;

$ua->get('http://bbc.co.uk/', $self->parallol(sub {
$self->stash('bbc', pop->res->dom);
}));

$ua->get('http://twitter.com/, $self->parallol(sub {
$self->stash('twitter, pop->res->dom);
}));
};
```

Or, if you need to do more advanced stuff:

```perl
use Mojolicious::Lite;

plugin 'parallol';

get '/' => sub {
my $self = shift;
my $foo;
my $bar;

$db->select('foo', $self->parallol(sub { $foo = pop }));
$db->select('bar', $self->parallol(sub { $bar = pop }));

$self->parallol_done(sub {
$self->render(json => { foo => $foo, bar => $bar });
});
};
```

27 changes: 27 additions & 0 deletions example.pl
@@ -0,0 +1,27 @@
use lib 'lib';
use Mojolicious::Lite;

plugin 'parallol';

get '/' => sub {
my $self = shift;
my $ua = $self->ua;

$self->stash(template => 'index');

$ua->get('http://judofyr.net/', $self->parallol(sub {
$self->stash(judo => pop->res->dom('title')->[0]->text);
}));

$ua->get('http://mojolicio.us/', $self->parallol(sub {
$self->stash(mojo => pop->res->dom('title')->[0]->text);
}));
};

app->start;

__DATA__
@@ index.html.ep
<%= $judo %> loves <%= $mojo %>
34 changes: 34 additions & 0 deletions lib/Mojolicious/Plugin/Parallol.pm
@@ -0,0 +1,34 @@
package Mojolicious::Plugin::Parallol;

use Mojo::Base 'Mojolicious::Plugin';

sub register {
my ($plugin, $app) = @_;

$app->hook(before_dispatch => sub {
my $self = shift;
$self->attr(paralloling => 0);
$self->attr('parallol_done' => sub {
sub { $self->render }
});
});

$app->helper(
parallol => sub {
my ($self, $callback) = @_;

$self->render_later;
$self->paralloling($self->paralloling + 1);

sub {
&$callback(@_);
$self->paralloling($self->paralloling - 1);

&{$self->parallol_done} if ($self->paralloling == 0);
}
}
);
}

1;

0 comments on commit 04ba59b

Please sign in to comment.