line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Promises6; |
2
|
31
|
|
|
31
|
|
659131
|
use Evo::Base -strict; |
|
31
|
|
|
|
|
64
|
|
|
31
|
|
|
|
|
278
|
|
3
|
31
|
|
|
31
|
|
5013
|
use Exporter 'import'; |
|
31
|
|
|
|
|
48
|
|
|
31
|
|
|
|
|
1003
|
|
4
|
31
|
|
|
31
|
|
11069
|
use Promises6::Util ':all'; |
|
31
|
|
|
|
|
49
|
|
|
31
|
|
|
|
|
4213
|
|
5
|
31
|
|
|
31
|
|
11585
|
use Promises6::Evo::Builder; |
|
31
|
|
|
|
|
82
|
|
|
31
|
|
|
|
|
300
|
|
6
|
31
|
|
|
31
|
|
943
|
use Evo::Manager; |
|
31
|
|
|
|
|
47
|
|
|
31
|
|
|
|
|
104
|
|
7
|
31
|
|
|
31
|
|
15710
|
use Evo; |
|
31
|
|
|
|
|
14679
|
|
|
31
|
|
|
|
|
31492
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.007'; # VERSION |
10
|
|
|
|
|
|
|
my @DSL = qw(when_ok when_rejected when_progress promise then); |
11
|
|
|
|
|
|
|
our @EXPORT_OK = (@DSL, qw(deferred resolved rejected all race)); |
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK, dsl => \@DSL); |
13
|
91
|
|
|
91
|
1
|
3932
|
sub deferred { Promises6::Evo::Builder->singleton->deferred() } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# dsl |
16
|
|
|
|
|
|
|
sub then { |
17
|
7
|
|
|
7
|
1
|
94
|
Evo::dsl_stash->{'promise'} = Evo::dsl_stash('promise')->then(@_); |
18
|
|
|
|
|
|
|
} |
19
|
2
|
|
|
2
|
1
|
65
|
sub when_ok : prototype(&) { then(shift, undef, undef) } |
20
|
1
|
|
|
1
|
1
|
31
|
sub when_rejected : prototype(&) { then(undef, shift, undef) } |
21
|
1
|
|
|
1
|
1
|
8
|
sub when_progress : prototype(&) { then(undef, undef, shift) } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# es6 mozilla all |
24
|
|
|
|
|
|
|
# resolve all in the same order or reject immidiately with reason |
25
|
|
|
|
|
|
|
# if one of promises is rejected |
26
|
5
|
|
|
5
|
1
|
2238
|
sub all(@promises) { |
|
5
|
|
|
|
|
9
|
|
27
|
5
|
|
|
|
|
9
|
my $total = @promises; |
28
|
5
|
|
|
|
|
14
|
my $d = deferred; |
29
|
|
|
|
|
|
|
|
30
|
5
|
|
|
|
|
68
|
my ($counter, $i, @results) = (0, 0); |
31
|
5
|
|
|
|
|
8
|
foreach my $cur (@promises) { |
32
|
13
|
|
|
|
|
73
|
my $cur_i = $i; |
33
|
13
|
100
|
|
|
|
32
|
my $p = is_promise($cur) ? $cur : resolved($cur); |
34
|
|
|
|
|
|
|
$p->then( |
35
|
|
|
|
|
|
|
sub { |
36
|
11
|
|
|
11
|
|
23
|
$results[$cur_i] = shift; |
37
|
11
|
100
|
|
|
|
95
|
$d->resolve(\@results) unless ++$counter < $total; |
38
|
|
|
|
|
|
|
}, |
39
|
2
|
|
|
2
|
|
21
|
sub { $d->reject(shift) } |
40
|
13
|
|
|
|
|
208
|
); |
41
|
13
|
|
|
|
|
220
|
$i++; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# already can be rejected |
44
|
13
|
100
|
|
|
|
32
|
last unless $d->state == PENDING; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
5
|
|
|
|
|
53
|
$d->promise; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# es6 mozilla race |
51
|
|
|
|
|
|
|
# first who rejects or resolves a promise gets a prize |
52
|
5
|
|
|
5
|
1
|
612
|
sub race(@promises) { |
|
5
|
|
|
|
|
7
|
|
53
|
5
|
|
|
|
|
7
|
my $total = @promises; |
54
|
5
|
|
|
|
|
9
|
my $d = deferred; |
55
|
|
|
|
|
|
|
|
56
|
5
|
|
|
|
|
72
|
foreach my $cur (@promises) { |
57
|
9
|
100
|
|
|
|
52
|
my $p = is_promise($cur) ? $cur : resolved($cur); |
58
|
9
|
|
|
2
|
|
165
|
$p->then(sub { $d->resolve(shift) }, sub { $d->reject(shift) }); |
|
5
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
9
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# already can be resolved |
61
|
9
|
100
|
|
|
|
131
|
last unless $d->state == PENDING; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
5
|
|
|
|
|
48
|
$d->promise; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub promise : prototype(&) { |
68
|
14
|
|
|
14
|
1
|
2225
|
my $cb = shift; |
69
|
14
|
|
|
|
|
40
|
my $d = deferred; |
70
|
14
|
|
|
3
|
|
231
|
my $resolve = sub { $d->resolve($_[0]) }; |
|
3
|
|
|
|
|
44
|
|
71
|
14
|
|
|
1
|
|
42
|
my $reject = sub { $d->reject($_[0]) }; |
|
1
|
|
|
|
|
10
|
|
72
|
14
|
|
|
2
|
|
38
|
my $notify = sub { $d->notify($_[0]) }; |
|
2
|
|
|
|
|
39
|
|
73
|
|
|
|
|
|
|
|
74
|
14
|
|
|
|
|
94
|
my $promise = $d->promise; |
75
|
|
|
|
|
|
|
Evo::dsl_call( |
76
|
|
|
|
|
|
|
{promise => $d->promise}, |
77
|
|
|
|
|
|
|
$resolve, $reject, $notify, |
78
|
|
|
|
|
|
|
sub { |
79
|
14
|
|
|
14
|
|
864
|
$cb->($resolve, $reject, $notify); |
80
|
14
|
|
|
|
|
85
|
$promise = Evo::dsl_stash('promise'); |
81
|
|
|
|
|
|
|
} |
82
|
14
|
|
|
|
|
192
|
); |
83
|
|
|
|
|
|
|
|
84
|
14
|
|
|
|
|
640
|
return $promise; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub resolved : prototype($) { |
88
|
23
|
|
|
23
|
1
|
4030
|
my $val = shift; |
89
|
23
|
|
|
|
|
61
|
deferred->resolve($val)->promise; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub rejected : prototype($) { |
93
|
5
|
|
|
5
|
1
|
515
|
my $reason = shift; |
94
|
5
|
50
|
|
|
|
20
|
Carp::croak("Provide a reason, not a promise") if is_promise($reason); |
95
|
5
|
|
|
|
|
21
|
deferred->reject($reason)->promise; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# ABSTRACT: Promises6 - Promises/A+ with DSL, ES6 syntax and progress notifications |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |