line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Promise::XS::Promise; |
2
|
|
|
|
|
|
|
|
3
|
26
|
|
|
26
|
|
154
|
use strict; |
|
26
|
|
|
|
|
37
|
|
|
26
|
|
|
|
|
610
|
|
4
|
26
|
|
|
26
|
|
112
|
use warnings; |
|
26
|
|
|
|
|
39
|
|
|
26
|
|
|
|
|
9727
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=encoding utf-8 |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Promise::XS::Promise - promise object |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
See L. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DESCRIPTION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
This is L’s actual promise object class. It implements |
19
|
|
|
|
|
|
|
these methods: |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=over |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=item * C |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=item * C |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=item * C |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=back |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
… which behave as they normally do in promise implementations. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Additionally, C and C may be used, thus: |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $p3 = Promise::XS::Promise->all( $p1, $p2, .. ); |
36
|
|
|
|
|
|
|
my $p3 = Promise::XS::Promise->race( $p1, $p2, .. ); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
… or, just: |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $p3 = ref($p1)->all( $p1, $p2, .. ); |
41
|
|
|
|
|
|
|
my $p3 = ref($p1)->race( $p1, $p2, .. ); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
… or even: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $p3 = $p1->all( $p1, $p2, .. ); |
46
|
|
|
|
|
|
|
my $p3 = $p1->race( $p1, $p2, .. ); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
(Note the repetition of $p1 in these last examples!) |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NOTES |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Subclassing this class won’t work because the above-named methods always |
53
|
|
|
|
|
|
|
return instances of (exactly) this class. That may change eventually, |
54
|
|
|
|
|
|
|
but for now this is what’s what. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Lifted from Promises::collect |
59
|
|
|
|
|
|
|
sub all { |
60
|
3
|
|
|
3
|
0
|
102
|
my $all_done = Promise::XS::resolved(); |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
9
|
for my $p (@_[1 .. $#_]) { |
63
|
6
|
|
|
|
|
9
|
my @results; |
64
|
|
|
|
|
|
|
$all_done = $all_done->then( sub { |
65
|
6
|
|
|
6
|
|
12
|
@results = @_; |
66
|
6
|
|
|
|
|
46
|
return $p; |
67
|
6
|
|
|
6
|
|
31
|
} )->then(sub{ ( @results, [ @_ ] ) } ); |
|
6
|
|
|
|
|
40
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
3
|
|
|
|
|
20
|
return $all_done; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Lifted from Promise::ES6 |
74
|
|
|
|
|
|
|
sub race { |
75
|
2
|
|
|
2
|
0
|
8
|
my $deferred = Promise::XS::Deferred::create(); |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
3
|
my $is_done; |
78
|
|
|
|
|
|
|
|
79
|
2
|
|
|
|
|
5
|
my $promise = $deferred->promise(); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $on_resolve_cr = sub { |
82
|
4
|
100
|
|
4
|
|
12
|
return if $is_done; |
83
|
2
|
|
|
|
|
3
|
$is_done = 1; |
84
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
5
|
$deferred->resolve(@_); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Proactively eliminate references: |
88
|
2
|
|
|
|
|
10
|
undef $deferred; |
89
|
2
|
|
|
|
|
9
|
}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $on_reject_cr = sub { |
92
|
0
|
0
|
|
0
|
|
0
|
return if $is_done; |
93
|
0
|
|
|
|
|
0
|
$is_done = 1; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
0
|
$deferred->reject(@_); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Proactively eliminate references: |
98
|
0
|
|
|
|
|
0
|
undef $deferred; |
99
|
2
|
|
|
|
|
6
|
}; |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
5
|
for my $given_promise (@_[1 .. $#_]) { |
102
|
4
|
|
|
|
|
11
|
$given_promise->then($on_resolve_cr, $on_reject_cr); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
2
|
|
|
|
|
16
|
return $promise; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _warn_unhandled { |
109
|
10
|
|
|
10
|
|
34490
|
my (@reasons) = @_; |
110
|
|
|
|
|
|
|
|
111
|
10
|
|
|
|
|
17
|
my $class = __PACKAGE__; |
112
|
|
|
|
|
|
|
|
113
|
10
|
50
|
|
|
|
24
|
if (1 == @reasons) { |
114
|
10
|
|
|
|
|
76
|
warn "$class: Unhandled rejection: $reasons[0]\n"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else { |
117
|
0
|
|
|
|
|
|
my $total = 0 + @reasons; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
for my $i ( 0 .. $#reasons ) { |
120
|
0
|
|
|
|
|
|
my $num = 1 + $i; |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
warn "$class: Unhandled rejection ($num of $total): $reasons[$i]\n"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
128
|
|
|
|
|
|
|
# Future::AsyncAwait interface |
129
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
#sub AWAIT_ON_READY { |
132
|
|
|
|
|
|
|
# $_[0]->finally($_[1])->catch(\&AWAIT_CHAIN_CANCEL); |
133
|
|
|
|
|
|
|
#} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |