line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ZMQ::Raw::Loop::Promise; |
2
|
|
|
|
|
|
|
$ZMQ::Raw::Loop::Promise::VERSION = '0.39'; |
3
|
14
|
|
|
14
|
|
92
|
use strict; |
|
14
|
|
|
|
|
29
|
|
|
14
|
|
|
|
|
396
|
|
4
|
14
|
|
|
14
|
|
72
|
use warnings; |
|
14
|
|
|
|
|
26
|
|
|
14
|
|
|
|
|
309
|
|
5
|
14
|
|
|
14
|
|
68
|
use Carp; |
|
14
|
|
|
|
|
24
|
|
|
14
|
|
|
|
|
1258
|
|
6
|
|
|
|
|
|
|
|
7
|
0
|
|
|
0
|
|
0
|
sub CLONE_SKIP { 1 } |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my @attributes; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
BEGIN |
12
|
|
|
|
|
|
|
{ |
13
|
14
|
|
|
14
|
|
75
|
@attributes = qw/ |
14
|
|
|
|
|
|
|
loop |
15
|
|
|
|
|
|
|
status |
16
|
|
|
|
|
|
|
/; |
17
|
|
|
|
|
|
|
|
18
|
14
|
|
|
14
|
|
107
|
no strict 'refs'; |
|
14
|
|
|
|
|
45
|
|
|
14
|
|
|
|
|
1281
|
|
19
|
14
|
|
|
|
|
34
|
foreach my $accessor (@attributes) |
20
|
|
|
|
|
|
|
{ |
21
|
28
|
|
|
|
|
557
|
*{$accessor} = sub |
22
|
|
|
|
|
|
|
{ |
23
|
74
|
100
|
|
74
|
|
1852
|
@_ > 1 ? $_[0]->{$accessor} = $_[1] : $_[0]->{$accessor} |
|
|
|
|
0
|
|
|
|
24
|
28
|
|
|
|
|
106
|
}; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
14
|
|
|
14
|
|
102
|
use ZMQ::Raw; |
|
14
|
|
|
|
|
57
|
|
|
14
|
|
|
|
|
481
|
|
29
|
|
|
|
|
|
|
|
30
|
14
|
|
|
14
|
|
115
|
use constant PLANNED => 0; |
|
14
|
|
|
|
|
48
|
|
|
14
|
|
|
|
|
1539
|
|
31
|
14
|
|
|
14
|
|
97
|
use constant KEPT => 1; |
|
14
|
|
|
|
|
49
|
|
|
14
|
|
|
|
|
947
|
|
32
|
14
|
|
|
14
|
|
93
|
use constant BROKEN => 2; |
|
14
|
|
|
|
|
30
|
|
|
14
|
|
|
|
|
8853
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 NAME |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
ZMQ::Raw::Loop::Promise - Promise class |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 VERSION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
version 0.39 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
A L represents a promise |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
B: The API of this module is unstable and may change without warning |
47
|
|
|
|
|
|
|
(any change will be appropriately documented in the changelog). |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 SYNOPSIS |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
use ZMQ::Raw; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $context = ZMQ::Raw::Context->new; |
54
|
|
|
|
|
|
|
my $loop = ZMQ::Raw::Loop->new ($context); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $promise = ZMQ::Raw::Loop::Promise->new ($loop); |
57
|
|
|
|
|
|
|
$promise->then (sub |
58
|
|
|
|
|
|
|
{ |
59
|
|
|
|
|
|
|
my $promise = shift; |
60
|
|
|
|
|
|
|
print "Promise kept/broken: ", $promise->result, "\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $timer = ZMQ::Raw::Loop::Timer->new ( |
65
|
|
|
|
|
|
|
timer => ZMQ::Raw::Timer->new ($context, after => 100), |
66
|
|
|
|
|
|
|
on_timeout => sub |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
$promise->keep ('done'); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
$loop->add ($timer); |
73
|
|
|
|
|
|
|
$loop->run(); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 METHODS |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 new( $loop ) |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Create a new promise. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 status( ) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Get the status of the promise. One of C, C or C. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 await( ) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Wait for the promise to be kept or broken. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 result( ) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Wait for the promise to be kept or broken, if its kept the result will be |
92
|
|
|
|
|
|
|
returned, otherwise throws the cause. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 cause( ) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Get the reason why the promise was broken. This method will croak if the promise |
97
|
|
|
|
|
|
|
is still planned or has been kept. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 break( $result ) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Break the promise, setting the cause to C<$result>. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 keep( $result ) |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Keep the promise, setting its result to C<$result>. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 then( \&callback ) |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Schedule C<\&callback> to be fired when the promise is either kept or broken. Returns |
110
|
|
|
|
|
|
|
a new C. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub new |
115
|
|
|
|
|
|
|
{ |
116
|
9
|
|
|
9
|
1
|
27
|
my ($this, $loop) = @_; |
117
|
|
|
|
|
|
|
|
118
|
9
|
|
33
|
|
|
32
|
my $class = ref ($this) || $this; |
119
|
9
|
|
|
|
|
28
|
my $self = |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
loop => $loop, |
122
|
|
|
|
|
|
|
status => &PLANNED, |
123
|
|
|
|
|
|
|
}; |
124
|
|
|
|
|
|
|
|
125
|
9
|
|
|
|
|
21
|
return bless $self, $class; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub await |
129
|
|
|
|
|
|
|
{ |
130
|
4
|
|
|
4
|
1
|
11
|
my ($this) = @_; |
131
|
|
|
|
|
|
|
|
132
|
4
|
|
|
|
|
10
|
while ($this->status == &PLANNED) |
133
|
|
|
|
|
|
|
{ |
134
|
10
|
50
|
|
|
|
44
|
if ($this->loop->terminated) |
135
|
|
|
|
|
|
|
{ |
136
|
0
|
|
|
|
|
0
|
$this->break(); |
137
|
0
|
|
|
|
|
0
|
return; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
10
|
|
|
|
|
31
|
$this->loop->run_one; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub keep |
145
|
|
|
|
|
|
|
{ |
146
|
7
|
|
|
7
|
1
|
40
|
my ($this, $result) = @_; |
147
|
|
|
|
|
|
|
|
148
|
7
|
|
|
|
|
18
|
$this->{result} = $result; |
149
|
7
|
|
|
|
|
53
|
$this->status (&KEPT); |
150
|
|
|
|
|
|
|
|
151
|
7
|
100
|
|
|
|
22
|
if ($this->{then}) |
152
|
|
|
|
|
|
|
{ |
153
|
5
|
|
|
|
|
7
|
&{$this->{then}}(); |
|
5
|
|
|
|
|
26
|
|
154
|
5
|
|
|
|
|
57
|
$this->{then} = undef; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub break |
159
|
|
|
|
|
|
|
{ |
160
|
2
|
|
|
2
|
1
|
10
|
my ($this, $cause) = @_; |
161
|
|
|
|
|
|
|
|
162
|
2
|
|
|
|
|
6
|
$this->{cause} = $cause; |
163
|
2
|
|
|
|
|
9
|
$this->status (&BROKEN); |
164
|
|
|
|
|
|
|
|
165
|
2
|
100
|
|
|
|
7
|
if ($this->{then}) |
166
|
|
|
|
|
|
|
{ |
167
|
1
|
|
|
|
|
2
|
&{$this->{then}}(); |
|
1
|
|
|
|
|
4
|
|
168
|
1
|
|
|
|
|
5
|
$this->{then} = undef; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub then |
173
|
|
|
|
|
|
|
{ |
174
|
6
|
|
|
6
|
1
|
16
|
my ($this, $then) = @_; |
175
|
|
|
|
|
|
|
|
176
|
6
|
50
|
|
|
|
14
|
if ($this->status != &PLANNED) |
177
|
|
|
|
|
|
|
{ |
178
|
0
|
|
|
|
|
0
|
croak "promise not planned"; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
6
|
|
|
|
|
17
|
my $promise = ZMQ::Raw::Loop::Promise->new ($this->loop); |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
$this->{then} = sub |
184
|
|
|
|
|
|
|
{ |
185
|
6
|
|
|
6
|
|
10
|
my $result = eval { &{$then} ($this) }; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
20
|
|
186
|
6
|
100
|
|
|
|
43
|
if ($@) |
187
|
|
|
|
|
|
|
{ |
188
|
1
|
|
|
|
|
6
|
$promise->break ($@); |
189
|
1
|
|
|
|
|
2
|
return; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
5
|
|
|
|
|
27
|
$promise->keep ($result); |
193
|
6
|
|
|
|
|
25
|
}; |
194
|
|
|
|
|
|
|
|
195
|
6
|
|
|
|
|
21
|
return $promise; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub result |
199
|
|
|
|
|
|
|
{ |
200
|
3
|
|
|
3
|
1
|
1352
|
my ($this) = @_; |
201
|
|
|
|
|
|
|
|
202
|
3
|
|
|
|
|
10
|
$this->await(); |
203
|
3
|
50
|
|
|
|
26
|
if ($this->status == &KEPT) |
204
|
|
|
|
|
|
|
{ |
205
|
3
|
|
|
|
|
10
|
return $this->{result}; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
0
|
die $this->{cause}; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub cause |
212
|
|
|
|
|
|
|
{ |
213
|
2
|
|
|
2
|
1
|
6
|
my ($this) = @_; |
214
|
|
|
|
|
|
|
|
215
|
2
|
50
|
|
|
|
6
|
if ($this->status != &BROKEN) |
216
|
|
|
|
|
|
|
{ |
217
|
0
|
|
|
|
|
0
|
croak "promise not broken"; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
2
|
|
|
|
|
16
|
return $this->{cause}; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 CONSTANTS |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 PLANNED |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
The promise is still planned. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 KEPT |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
The promise has been kept. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 BROKEN |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
The promise was broken. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=for Pod::Coverage loop |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 AUTHOR |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Jacques Germishuys |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Copyright 2017 Jacques Germishuys. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
248
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
249
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
1; # End of ZMQ::Raw::Loop::Promise |