line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Promises6; |
2
|
31
|
|
|
31
|
|
464744
|
use Evo -modern; |
|
31
|
|
|
|
|
48
|
|
|
31
|
|
|
|
|
180
|
|
3
|
31
|
|
|
31
|
|
5546
|
use Exporter 'import'; |
|
31
|
|
|
|
|
48
|
|
|
31
|
|
|
|
|
783
|
|
4
|
31
|
|
|
31
|
|
9031
|
use Promises6::Util ':all'; |
|
31
|
|
|
|
|
47
|
|
|
31
|
|
|
|
|
3164
|
|
5
|
31
|
|
|
31
|
|
9199
|
use Promises6::Evo::Builder; |
|
31
|
|
|
|
|
58
|
|
|
31
|
|
|
|
|
23561
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.008'; # VERSION |
8
|
|
|
|
|
|
|
my @DSL = qw(when_ok when_rejected when_progress promise then); |
9
|
|
|
|
|
|
|
our @EXPORT_OK = (@DSL, qw(deferred resolved rejected all race)); |
10
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK, dsl => \@DSL); |
11
|
91
|
|
|
91
|
1
|
3518
|
sub deferred { Promises6::Evo::Builder->singleton->deferred() } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# dsl |
14
|
|
|
|
|
|
|
sub then { |
15
|
7
|
|
|
7
|
1
|
47
|
my $dsl = Evo::dep_single 'Evo::Dsl'; |
16
|
7
|
|
|
|
|
27
|
$dsl->stash->{'promise'} = $dsl->stash->{'promise'}->then(@_); |
17
|
|
|
|
|
|
|
} |
18
|
2
|
|
|
2
|
1
|
30
|
sub when_ok : prototype(&) { then(shift, undef, undef) } |
19
|
1
|
|
|
1
|
1
|
15
|
sub when_rejected : prototype(&) { then(undef, shift, undef) } |
20
|
1
|
|
|
1
|
1
|
7
|
sub when_progress : prototype(&) { then(undef, undef, shift) } |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# es6 mozilla all |
23
|
|
|
|
|
|
|
# resolve all in the same order or reject immidiately with reason |
24
|
|
|
|
|
|
|
# if one of promises is rejected |
25
|
5
|
|
|
5
|
1
|
1008
|
sub all(@promises) { |
|
5
|
|
|
|
|
5
|
|
26
|
5
|
|
|
|
|
5
|
my $total = @promises; |
27
|
5
|
|
|
|
|
7
|
my $d = deferred; |
28
|
|
|
|
|
|
|
|
29
|
5
|
|
|
|
|
55
|
my ($counter, $i, @results) = (0, 0); |
30
|
5
|
|
|
|
|
9
|
foreach my $cur (@promises) { |
31
|
13
|
|
|
|
|
10
|
my $cur_i = $i; |
32
|
13
|
100
|
|
|
|
22
|
my $p = is_promise($cur) ? $cur : resolved($cur); |
33
|
|
|
|
|
|
|
$p->then( |
34
|
|
|
|
|
|
|
sub { |
35
|
11
|
|
|
11
|
|
17
|
$results[$cur_i] = shift; |
36
|
11
|
100
|
|
|
|
33
|
$d->resolve(\@results) unless ++$counter < $total; |
37
|
|
|
|
|
|
|
}, |
38
|
2
|
|
|
2
|
|
4
|
sub { $d->reject(shift) } |
39
|
13
|
|
|
|
|
127
|
); |
40
|
13
|
|
|
|
|
122
|
$i++; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# already can be rejected |
43
|
13
|
100
|
|
|
|
21
|
last unless $d->state == PENDING; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
10
|
$d->promise; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# es6 mozilla race |
50
|
|
|
|
|
|
|
# first who rejects or resolves a promise gets a prize |
51
|
5
|
|
|
5
|
1
|
371
|
sub race(@promises) { |
|
5
|
|
|
|
|
4
|
|
52
|
5
|
|
|
|
|
5
|
my $total = @promises; |
53
|
5
|
|
|
|
|
8
|
my $d = deferred; |
54
|
|
|
|
|
|
|
|
55
|
5
|
|
|
|
|
37
|
foreach my $cur (@promises) { |
56
|
9
|
100
|
|
|
|
18
|
my $p = is_promise($cur) ? $cur : resolved($cur); |
57
|
9
|
|
|
2
|
|
82
|
$p->then(sub { $d->resolve(shift) }, sub { $d->reject(shift) }); |
|
5
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
5
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# already can be resolved |
60
|
9
|
100
|
|
|
|
72
|
last unless $d->state == PENDING; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
5
|
|
|
|
|
9
|
$d->promise; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub promise : prototype(&) { |
67
|
14
|
|
|
14
|
1
|
1488
|
my $cb = shift; |
68
|
14
|
|
|
|
|
31
|
my $d = deferred; |
69
|
14
|
|
|
3
|
|
124
|
my $resolve = sub { $d->resolve($_[0]) }; |
|
3
|
|
|
|
|
24
|
|
70
|
14
|
|
|
1
|
|
28
|
my $reject = sub { $d->reject($_[0]) }; |
|
1
|
|
|
|
|
5
|
|
71
|
14
|
|
|
2
|
|
27
|
my $notify = sub { $d->notify($_[0]) }; |
|
2
|
|
|
|
|
20
|
|
72
|
|
|
|
|
|
|
|
73
|
14
|
|
|
|
|
34
|
my $promise = $d->promise; |
74
|
14
|
|
|
|
|
97
|
my $dsl = Evo::dep_single('Evo::Dsl'); |
75
|
|
|
|
|
|
|
$dsl->call( |
76
|
|
|
|
|
|
|
{promise => $d->promise}, |
77
|
|
|
|
|
|
|
$resolve, $reject, $notify, |
78
|
|
|
|
|
|
|
sub { |
79
|
14
|
|
|
14
|
|
385
|
$cb->($resolve, $reject, $notify); |
80
|
14
|
|
|
|
|
61
|
$promise = $dsl->stash('promise'); |
81
|
|
|
|
|
|
|
} |
82
|
14
|
|
|
|
|
8252
|
); |
83
|
|
|
|
|
|
|
|
84
|
14
|
|
|
|
|
341
|
return $promise; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub resolved : prototype($) { |
88
|
23
|
|
|
23
|
1
|
2895
|
my $val = shift; |
89
|
23
|
|
|
|
|
43
|
deferred->resolve($val)->promise; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub rejected : prototype($) { |
93
|
5
|
|
|
5
|
1
|
705
|
my $reason = shift; |
94
|
5
|
50
|
|
|
|
16
|
Carp::croak("Provide a reason, not a promise") if is_promise($reason); |
95
|
5
|
|
|
|
|
12
|
deferred->reject($reason)->promise; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# ABSTRACT: Promises6 - Promises/A+ with DSL, ES6 syntax and progress notifications |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |