blib/lib/Mojolicious/Plugin/Future.pm

filter: Statementstmt 100% 27 / 27 Branchbran 100% 2 / 2 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 95.2% 40 / 42 CC 3 SCAR 5.1
CC
3
Cov
97%
__ANON__
3.0
__ANON__
1.0
register
1.0
CRAP 1.7
↑ Summary
1 package Mojolicious::Plugin::Future;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Future::Mojo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.002';
8  
9 1 my %futures;
10  
11 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
12 1 my ($self, $app, $options) = @_;
13
14 1 ▶ $app->helper(future => sub { shift; Future::Mojo->new(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
15
16 2 ▶ $app->helper(adopt_future => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 2 my ($c, $f) = @_;
18 2 my $tx = $c->render_later->tx;
19 2 my $fkey = "$f";
20 2 $futures{$fkey} = $f;
21 2 weaken(my $weak_c = $c);
22 2 ▶ return $f->on_ready(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
23 2 my $f = shift;
24 2 delete $futures{$fkey};
25 2 ▶ $weak_c->helpers->reply->exception(scalar $f->failure)
Branch: 1 branch
BranchTrueFalseTotal
if defined $weak_c and $f->is_failed 1 1 2
Condition66%2/3
defined $weak_c and $f->is_failed
!ll&&!rl&&r
011
MC/DC50%1/2
defined $weak_c and $f->is_failed
defined $weak_c $f->is_failed
Truth table66%2/3
A and B
A= defined $weak_cB= $f->is_failed
ABresult
0X0
100
111
26 if defined $weak_c and $f->is_failed;
27 2 undef $tx;
28 2 });
29 1 });
30 }
31  
32 1 1;
33  
34 =head1 NAME
35  
36 Mojolicious::Plugin::Future - use Future in Mojolicious applications
37  
38 =head1 SYNOPSIS
39  
40 use Mojolicious::Lite;
41
42 plugin 'Future';
43
44 use Scalar::Util 'weaken';
45 use Mojo::UserAgent;
46 my $ua = Mojo::UserAgent->new;
47 get '/async_callback' => sub {
48 my $c = shift;
49 my $f = $c->future;
50 weaken(my $weak_f = $f); # only close over weakened Future
51 $ua->get('http://example.com', sub {
52 my ($ua, $tx) = @_;
53 $tx->success ? $weak_f->done($tx->result) : $weak_f->fail($tx->error->{message});
54 });
55 $c->adopt_future($f->on_done(sub {
56 my ($result) = @_;
57 $c->render(json => {result => $result->text});
58 }));
59 };
60
61 get '/future_returning' => sub {
62 my $c = shift;
63 $c->adopt_future(returns_a_future()->then(sub {
64 my @result = @_;
65 return returns_another_future(@result);
66 })->on_done(sub {
67 my @result = @_;
68 $c->render(json => {result => \@result});
69 }));
70 };
71  
72 =head1 DESCRIPTION
73  
74 L<Mojolicious::Plugin::Future> is a convenient way to use L<Future> in a
75 L<Mojolicious> application. The final future in a sequence or convergence is
76 passed to the L</"adopt_future"> helper, which takes care of the details of
77 asynchronous rendering in a similar fashion to
78 L<Mojolicious::Plugin::DefaultHelpers/"delay">.
79  
80 =head1 HELPERS
81  
82 =head2 adopt_future
83  
84 $f = $c->adopt_future($f);
85  
86 Disables automatic rendering, stores the Future instance, keeps a reference to
87 L<Mojolicious::Controller/"tx"> in case the underlying connection gets closed
88 early, and calls L<< Mojolicious::Plugin::DefaultHelpers/"reply->exception" >>
89 if the Future fails.
90  
91 =head2 future
92  
93 my $f = $c->future;
94 my $f = $c->future($loop);
95  
96 Convenience method to return a new L<Future::Mojo> object.
97  
98 =head1 METHODS
99  
100 L<Mojolicious::Plugin::Future> inherits all methods from L<Mojolicious::Plugin>
101 and implements the following new ones.
102  
103 =head2 register
104  
105 $plugin->register(Mojolicious->new);
106  
107 Register helper in L<Mojolicious> application.
108  
109 =head1 BUGS
110  
111 Report any issues on the public bugtracker.
112  
113 =head1 AUTHOR
114  
115 Dan Book <dbook@cpan.org>
116  
117 =head1 COPYRIGHT AND LICENSE
118  
119 This software is Copyright (c) 2017 by Dan Book.
120  
121 This is free software, licensed under:
122  
123 The Artistic License 2.0 (GPL Compatible)
124  
125 =head1 SEE ALSO
126  
127 L<Future::Mojo>, L<Future>
↑ Summary