Skip to content

Commit

Permalink
Fixed non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Akron committed Apr 3, 2013
1 parent 6b1e881 commit 1a24f58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@
0.11 2013-04-03
- Improved test suite.
- Fixed non-blocking helper.

0.10 2013-03-04
- Delete mentioning api_test in documentation.
Expand Down
22 changes: 16 additions & 6 deletions lib/Mojolicious/Plugin/Piwik.pm
Expand Up @@ -2,6 +2,7 @@ package Mojolicious::Plugin::Piwik;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::ByteStream 'b';
use Mojo::UserAgent;
use Mojo::IOLoop;

our $VERSION = '0.11';

Expand Down Expand Up @@ -256,19 +257,28 @@ SCRIPTTAG

# Non-Blocking
else {
$ua->get(
$url => sub {
my ($ua, $tx) = @_;

# Create delay object
my $delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
my $tx = shift;

my $json = {};

# Return prepared response
$json = _prepare_response($tx->res) if $tx->success;
my $res = $tx->success;
$json = _prepare_response($res) if $res;

# Release callback
$cb->($json);
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
}
);

$ua->get($url => $delay->begin);

# Start IOLoop if not started already
$delay->wait unless Mojo::IOLoop->is_running;
};
});

Expand Down

0 comments on commit 1a24f58

Please sign in to comment.