Permalink
Browse files

Merge branch 'delayed-snippet'

  • Loading branch information...
2 parents 7675ce1 + 3b7e5f5 commit b9cd65c296a2d248ff1d678ff13f88c5a27706be @xsawyerx xsawyerx committed Jan 5, 2017
Showing with 35 additions and 0 deletions.
  1. +2 −0 Changes
  2. +33 −0 lib/Dancer2/Manual.pod
View
@@ -17,6 +17,8 @@
* GH #1286: Clarify hook behavior when disabling layout (biafra)
* GH #1280: Update documentation to use specific parameter
keywords (Hunter McMillen)
+ * Add another example of the delayed asynchronous mechanism
+ (Ed @mohawk2 J., Sawyer X)
0.204001 2016-10-17 08:29:00-05:00 America/Chicago
@@ -410,6 +410,39 @@ You can handle the error yourself by providing an C<on_error> handler:
};
};
+Here is an example that asynchronously streams the contents of a CSV file:
+
+ use Dancer2;
+ use Text::CSV_XS qw< csv >;
+ use Path::Tiny qw< path >;
+ use JSON::MaybeXS qw< encode_json >;
+ # Create CSV parser
+ my $csv = Text::CSV_XS->new({
+ binary => 1,
+ auto_diag => 1,
+ });
+ get '/' => sub {
+ # delayed response:
+ delayed {
+ # streaming content
+ flush;
+ # Read each row and stream it in JSON
+ my $fh = path('filename.csv')->openr_utf8;
+ while ( my $row = $csv->getline($fh) ) {
+ content encode_json $row;
+ }
+ # close user connection
+ done;
+ } on_error => sub {
+ my ($error) = @_;
+ warning 'Failed to stream to user: ' . request->remote_address;
+ };
+ };
+
+B<NOTE:> If you just want to send a file's contents asynchronously,
+use C<send_file($filename)> instead of C<delayed>, as it will
+automatically take advantage of any asynchronous capability.
+
=head2 Action Skipping
An action can choose not to serve the current request and ask Dancer2 to

0 comments on commit b9cd65c

Please sign in to comment.