line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::PromiseActions; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
1657
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
18
|
|
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
523
|
use Mojo::Promise; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
30
|
|
6
|
3
|
|
|
3
|
|
77
|
use Scalar::Util 'blessed'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
984
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
3
|
|
|
3
|
1
|
115
|
my ($elf, $app, $config) = @_; |
12
|
|
|
|
|
|
|
$app->hook( |
13
|
|
|
|
|
|
|
around_action => sub { |
14
|
10
|
|
|
10
|
|
241085
|
my ($next, $c, $action, $last) = @_; |
15
|
10
|
|
|
|
|
31
|
my $want = wantarray; |
16
|
10
|
|
|
|
|
17
|
my @args; |
17
|
10
|
100
|
|
|
|
25
|
if ($want) { @args = $next->() } |
|
4
|
|
|
|
|
9
|
|
18
|
6
|
|
|
|
|
12
|
else { $args[0] = $next->() } |
19
|
10
|
100
|
66
|
|
|
15852
|
if (blessed($args[0]) && $args[0]->can('then')) { |
20
|
7
|
|
|
|
|
29
|
my $tx = $c->tx; |
21
|
7
|
100
|
|
|
|
53
|
$c->render_later if $last; |
22
|
7
|
|
|
|
|
86
|
my $p = Mojo::Promise->resolve($args[0]); |
23
|
|
|
|
|
|
|
$p->then( |
24
|
2
|
100
|
|
|
|
202682
|
($last ? undef : sub { $c->continue if $_[0] }), |
25
|
3
|
50
|
|
|
|
302792
|
sub { $c->reply->exception($_[0]) and undef $tx }, |
26
|
7
|
100
|
|
|
|
477
|
)->wait; |
27
|
7
|
100
|
|
|
|
407
|
return unless $last; |
28
|
|
|
|
|
|
|
} |
29
|
7
|
100
|
|
|
|
41
|
return $want ? @args : $args[0]; |
30
|
|
|
|
|
|
|
} |
31
|
3
|
|
|
|
|
24
|
); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
1; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 NAME |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Mojolicious::Plugin::PromiseActions - Automatic async and error handling for Promises |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 SYNOPSIS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
plugin 'PromiseActions'; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
get '/' => sub { |
45
|
|
|
|
|
|
|
my $c=shift; |
46
|
|
|
|
|
|
|
app->ua->get_p('ifconfig.me/all.json')->then(sub { |
47
|
|
|
|
|
|
|
$c->render(text=>shift->res->json('/ip_addr')); |
48
|
|
|
|
|
|
|
}); |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 METHODS |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 register |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Sets up a around_dispatch hook to disable automatic rendering and |
56
|
|
|
|
|
|
|
add a default catch callback to render an exception page when |
57
|
|
|
|
|
|
|
actions return a L |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Copyright (C) 2019, Marcus Ramberg. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
64
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 AUTHORS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Joel Berger, C |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Marcus Ramberg, C |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SEE ALSO |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
L, L, |
75
|
|
|
|
|
|
|
L, L |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |