line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Promise::Role::Futurify; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1340
|
use Future::Mojo; |
|
1
|
|
|
|
|
13981
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
9
|
use Scalar::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
8
|
use Role::Tiny; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.001'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires qw(ioloop then); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub futurify { |
12
|
5
|
|
|
5
|
1
|
8926
|
my $self = shift; |
13
|
5
|
|
|
|
|
27
|
my $f = Future::Mojo->new($self->ioloop); |
14
|
5
|
|
|
|
|
229
|
Scalar::Util::weaken(my $weak_f = $f); |
15
|
3
|
50
|
|
3
|
|
203270
|
$self->then(sub { $weak_f->done(@_) if $weak_f; 1 }, |
|
3
|
|
|
|
|
221
|
|
16
|
5
|
50
|
|
2
|
|
53
|
sub { $weak_f->fail(@_) if $weak_f; 1 }); |
|
2
|
|
|
|
|
101309
|
|
|
2
|
|
|
|
|
178
|
|
17
|
5
|
|
|
|
|
309
|
return $f; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
1; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Mojo::Promise::Role::Futurify - Chain a Future from a Mojo::Promise |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Mojo::Promise; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $promise = Mojo::Promise->with_roles('+Futurify')->new; |
31
|
|
|
|
|
|
|
my $future = $promise->futurify->on_ready(sub { |
32
|
|
|
|
|
|
|
my $f = shift; |
33
|
|
|
|
|
|
|
say $f->is_done ? 'Done' : 'Failed'; |
34
|
|
|
|
|
|
|
}); |
35
|
|
|
|
|
|
|
$promise->ioloop->timer(5 => sub { $promise->resolve }); |
36
|
|
|
|
|
|
|
$future->await; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use Mojo::UserAgent; |
39
|
|
|
|
|
|
|
my $ua = Mojo::UserAgent->new; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# complicated way of doing $ua->get('https://example.com') |
42
|
|
|
|
|
|
|
my $tx = $ua->get_p('https://example.com')->with_roles('+Futurify')->futurify->get; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# using Future composition methods |
45
|
|
|
|
|
|
|
my @futures; |
46
|
|
|
|
|
|
|
foreach my $url (@urls) { |
47
|
|
|
|
|
|
|
push @futures, $ua->get_p($url)->with_roles('+Futurify')->futurify; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use Future; |
51
|
|
|
|
|
|
|
Future->wait_all(@futures)->then(sub { |
52
|
|
|
|
|
|
|
foreach my $f (@_) { |
53
|
|
|
|
|
|
|
if ($f->is_done) { |
54
|
|
|
|
|
|
|
my $tx = $f->get; |
55
|
|
|
|
|
|
|
} elsif ($f->is_failed) { |
56
|
|
|
|
|
|
|
my $err = $f->failure; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
})->await; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 DESCRIPTION |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
L provides an interface to chain L |
64
|
|
|
|
|
|
|
objects from L objects. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 METHODS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
L composes the following methods. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 futurify |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $future = $promise->futurify; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Returns a L object that will become ready with success or failure |
75
|
|
|
|
|
|
|
when the L resolves or rejects. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 BUGS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Report any issues on the public bugtracker. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 AUTHOR |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Dan Book |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This software is Copyright (c) 2017 by Dan Book. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This is free software, licensed under: |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 SEE ALSO |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
L, L, L |