Merge branch 'master' of git.shadowcat.co.uk:Catalyst-Runtime into SicilianButtercup
John Napiorkowski [Fri, 17 May 2013 16:58:30 +0000 (12:58 -0400)]
Changes
lib/Catalyst/Engine.pm
lib/Catalyst/Response.pm
lib/Catalyst/Upgrading.pod

diff --git a/Changes b/Changes
index 115949c..1eaf0a9 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,7 +1,11 @@
 # This file documents the revision history for Perl extension Catalyst.
 
+TBA
   - Add Devel::InnerPackage to dependencies, fixing tests on perl 5.17.11
     as it's been removed from core. RT#84787
+  - New support for closing over the PSGI $writer object, useful for working
+    with event loops.
+
 
 5.90030 - 2013-04-12
   ! POSSIBLE BREAKING CHANGE: Removed Regexp dispatch type from core, and put
index 2367139..e63da83 100644 (file)
@@ -54,12 +54,20 @@ See L<Catalyst>.
 
 =head2 $self->finalize_body($c)
 
-Finalize body.  Prints the response output.
+Finalize body.  Prints the response output as blocking stream if it looks like
+a filehandle, otherwise write it out all in one go.  If there is no body in
+the response, we assume you are handling it 'manually', such as for nonblocking
+style or asynchronous streaming responses.  You do this by calling L<\write>
+several times (which sends HTTP headers if needed) or you close over C<$response->write_fh>.
+
+See L<Catalyst::Response\write> and L<Catalyst::Response\write_fh> for more.
 
 =cut
 
 sub finalize_body {
     my ( $self, $c ) = @_;
+    return if $c->response->has_write_fh;
+
     my $body = $c->response->body;
     no warnings 'uninitialized';
     if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) {
index 6dc661e..8559f36 100644 (file)
@@ -26,7 +26,25 @@ has _writer => (
     predicate => '_has_writer',
 );
 
-sub DEMOLISH { $_[0]->_writer->close if $_[0]->_has_writer }
+has write_fh => (
+  is=>'ro',
+  predicate=>'has_write_fh',
+  lazy_build=>1);
+
+  sub _build_write_fh {
+    my $self = shift;
+    $self->_context->finalize_headers unless
+      $self->finalized_headers;
+    $self->_writer;
+  };
+
+sub DEMOLISH {
+  my $self = shift;
+  return if $self->has_write_fh;
+  if($self->_has_writer) {
+    $self->_writer->close
+  }
+}
 
 has cookies   => (is => 'rw', default => sub { {} });
 has body      => (is => 'rw', default => undef);
@@ -246,6 +264,40 @@ $res->code is an alias for this, to match HTTP::Response->code.
 
 Writes $data to the output stream.
 
+=head2 $res->write_fh
+
+Returns a PSGI $writer object that has two methods, write and close.  You can
+close over this object for asynchronous and nonblocking applications.  For
+example (assuming you are using a supporting server, like L<Twiggy>
+
+    package AsyncExample::Controller::Root;
+
+    use Moose;
+
+    BEGIN { extends 'Catalyst::Controller' }
+
+    sub prepare_cb {
+      my $write_fh = pop;
+      return sub {
+        my $message = shift;
+        $write_fh->write("Finishing: $message\n");
+        $write_fh->close;
+      };
+    }
+
+    sub anyevent :Local :Args(0) {
+      my ($self, $c) = @_;
+      my $cb = $self->prepare_cb($c->res->write_fh);
+
+      my $watcher;
+      $watcher = AnyEvent->timer(
+        after => 5,
+        cb => sub {
+          $cb->(scalar localtime);
+          undef $watcher; # cancel circular-ref
+        });
+    }
+
 =head2 $res->print( @data )
 
 Prints @data to the output stream, separated by $,.  This lets you pass
index b6d3c8b..4526695 100644 (file)
@@ -2,6 +2,16 @@
 
 Catalyst::Upgrading - Instructions for upgrading to the latest Catalyst
 
+=head1 Upgrading to Catalyst TBA
+
+This version of L<Catalyst> offers some support for using L<AnyEvent> and
+L<IO::Async> event loops in your application.  These changes should work
+fine for most applications however if you are already trying to perform
+some streaming, minor changes in this area of the code might affect your
+functionality.  Please see L<Catalyst::Response\write_fh> for more and for a
+basic example.
+
 =head1 Upgrading to Catalyst 5.9
 
 The major change is that L<Plack>, a toolkit for using the L<PSGI>