| 1 | package Net::Curl::Promiser::Mojo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Mojo::IOLoop; | ||
| 6 | |||
| 7 | =encoding utf-8 | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Net::Curl::Promiser::Mojo - support for L<Mojolicious> | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | # Defaults to Mojo’s default loop but can accept a custom loop: | ||
| 16 | my $promiser = Net::Curl::Promiser::Mojo->new(); | ||
| 17 | |||
| 18 | my $handle = Net::Curl::Easy->new(); | ||
| 19 | $handle->setopt( CURLOPT_URL() => $url ); | ||
| 20 | |||
| 21 | $promiser->add_handle_p($handle)->then( | ||
| 22 | sub { print "$url completed.$/" }, | ||
| 23 | sub { warn "$url failed: " . shift }, | ||
| 24 | )->wait(); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This module provides a L<Mojolicious>-compatible subclass of | ||
| 29 | L<Net::Curl::Promiser>. | ||
| 30 | |||
| 31 | See F</examples> in the distribution, as well as the tests, | ||
| 32 | for fleshed-out demonstrations. | ||
| 33 | |||
| 34 | =head1 MOJOLICIOUS SPECIALTIES | ||
| 35 | |||
| 36 | This module implements the following tweaks to make it | ||
| 37 | more Mojo-friendly: | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item * This module uses L<Mojo::Promise> rather than L<Promise::ES6> | ||
| 42 | as its promise implementation. | ||
| 43 | |||
| 44 | =item * C<add_handle_p()> is an alias for the base class’s C<add_handle()>. | ||
| 45 | This alias conforms to Mojo’s convention of postfixing C<_p> onto the end | ||
| 46 | of promise-returning functions. | ||
| 47 | |||
| 48 | =item * The constructor accepts an optional parameter specifying a particular | ||
| 49 | Mojo::IOLoop object to be targeted by the promiser. | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | #---------------------------------------------------------------------- | ||
| 56 | |||
| 57 | use parent 'Net::Curl::Promiser::LoopBase'; | ||
| 58 | |||
| 59 | use Net::Curl::Promiser::Backend::Mojo; | ||
| 60 | |||
| 61 | #---------------------------------------------------------------------- | ||
| 62 | |||
| 63 | *add_handle_p = __PACKAGE__->can('add_handle'); | ||
| 64 | |||
| 65 | #---------------------------------------------------------------------- | ||
| 66 | |||
| 67 | sub _INIT { | ||
| 68 | my ($self, $args) = @_; | ||
| 69 | return Net::Curl::Promiser::Backend::Mojo->new(@$args); | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; |