Skip to content

Commit

Permalink
Simplify the AnyEvent::HTTP callback using on_body per typester++
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Jul 17, 2009
1 parent d1857aa commit 2de341b
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions lib/AnyEvent/Atom/Stream/UserAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,24 @@ sub get {
my($self, $url, %args) = @_;

my $content_cb = delete $args{":content_cb"};
http_get $url, want_body_handle => 1, sub {
my($handle, $headers) = @_;
Scalar::Util::weaken($self);

my $final_cb = $self->{on_disconnect} || sub {};

if ($handle) {
my $disconn_cb = sub {
undef $_[0];
$final_cb->();
};
$handle->timeout($self->{timeout}) if $self;
$handle->on_timeout($disconn_cb);
$handle->on_eof($disconn_cb);
$handle->on_read(sub {
my $h = shift;
local $XML::Atom::ForceUnicode = 1;
$content_cb->(delete $h->{rbuf});
});

$self->{guard} = AnyEvent::Util::guard {
undef $content_cb; # refs AnyEvent::Atom::Stream
undef $handle;
};
}
}
my $disconn_cb = $args{on_disconnect} || sub { };

my %opts;
$opts{timeout} = $self->{timeout} if $self->{timeout};

http_get $url, %opts, on_body => sub {
my($body, $headers) = @_;
local $XML::Atom::ForceUnicode = 1;
$content_cb->($body);
return 1;
}, sub {
my($body, $headers) = @_;
$disconn_cb->($body);
};

$self->{guard} = AnyEvent::Util::guard {
undef $content_cb; # refs AnyEvent::Atom::Stream
};
}

1;
Expand Down

0 comments on commit 2de341b

Please sign in to comment.