Commits

Toby Inkster committed 61edae4

Restore my old assert.pl and split async experimentation into another example script.

  • Participants
  • Parent commits f6fa3e5

Comments (0)

Files changed (3)

File examples/assert-async.pl

+use 5.010;
+#use Coro;
+use AnyEvent;
+#use Coro::AnyEvent;
+use Web::Magic::Async;
+
+my $w; $w = AE::timer 0, 1, sub { say "timer event" };
+
+my $a = Web::Magic::Async
+	->new(GET => 'http://www.google.co.uk/')
+	->assert_response(success => sub {$_->is_success})
+	->do_request;
+
+my $b = Web::Magic::Async
+	->new(GET => 'http://www.google.co.uk/adgawertgwretgwrtgw')
+	->assert_response(success => sub {$_->is_success})
+	->do_request;
+
+warn length($a->content);
+warn length($b->content);

File examples/assert.pl

-use EV;
-use Web::Magic::Async;
+use Web::Magic;
 
-my $a = Web::Magic::Async
+my $a = Web::Magic
 	->new(GET => 'http://www.google.co.uk/')
 	->assert_response(success => sub {$_->is_success})
 	->do_request;
 
-my $b = Web::Magic::Async
+my $b = Web::Magic
 	->new(GET => 'http://www.google.co.uk/adgawertgwretgwrtgw')
 	->assert_response(success => sub {$_->is_success})
 	->do_request;
 
-warn length($a->content);
-warn length($b->content);

File lib/Web/Magic/Async.pm

 			);
 		
 		$self->_async(
-			got_head         => AnyEvent->condvar,
-			got_partial_body => AnyEvent->condvar,
 			got_body         => AnyEvent->condvar,
 			partial_body     => '',
 			);
+		$self->_async->{got_body}->begin;
 		
 		http_request $req->method, $req->uri,
 			headers => { map { (lc $_, $req->header($_)) } $req->header_field_names },
 			body    => $req->content,
 			on_header => sub { $self->__header_callback(@_) },
 			on_body   => sub { $self->__body_callback(@_) },
-			sub { $self->__final_callback(@_) };
+			sub { $self->__final_callback(@_); $self->_async->{got_body}->end; 1 };
 	}
 	
-	return $self;
+	$self;
 }
 
 sub __header_callback
 {
-	my ($self, $hash) = @_;
+	my ($self, $headers) = @_;
+	my %h_clone = %$headers;
 	
 	warn $$self." -> __header_callback";
 	
 	$self->_async(
-		status      => delete $hash->{Status},
-		reason      => delete $hash->{Reason},
-		httpversion => delete $hash->{HTTPVersion},
+		status      => delete $h_clone{Status},
+		reason      => delete $h_clone{Reason},
+		httpversion => delete $h_clone{HTTPVersion},
 		);
 	
 	$self->_async(
-		headers  => HTTP::Headers->new(%$hash),
+		headers  => HTTP::Headers->new(%h_clone),
 		);
 	
-	$self->_async->{got_head}->send;
 	$self;
 }
 
 	warn $$self." -> __body_callback";
 
 	$self->_async->{partial_body} .= $body;
-	$self->_async->{got_partial_body}->send;
+	
 	$self;
 }
 
 		(delete $self->_async->{partial_body}).$body
 		);
 	
-	# XXX: Check assertions!
+	# TODO: Check assertions!
 
-	$self->_async->{got_body}->send;
 	$self;
 }
 
 	$self->_stash->{response};
 }
 
-sub headers
-{
-	my ($self) = @_;
-
-	return $self->_stash->{response}->headers
-		if $self->_stash->{response};
-
-	$self->do_request
-		unless exists $self->_async->{got_head};
-		
-	$self->_async->{got_head}->recv
-		unless $self->_async->{headers};
-	
-	$self->_async->{headers};
-}
+#sub headers
+#{
+#	my ($self) = @_;
+#
+#	return $self->_stash->{response}->headers
+#		if $self->_stash->{response};
+#
+#	$self->do_request
+#		unless exists $self->_async->{got_head};
+#		
+#	$self->_async->{got_head}->recv
+#		unless $self->_async->{headers};
+#	
+#	$self->_async->{headers};
+#}
 
 sub is_requested
 {