Commit | Line | Data |
dc28afe8 |
1 | package Object::Remote::Future; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw(Exporter); |
6 | |
c3d5ef8a |
7 | use Object::Remote::Logging qw( :log router ); |
21988035 |
8 | |
c3d5ef8a |
9 | BEGIN { router()->exclude_forwarding } |
23591f5f |
10 | |
dc28afe8 |
11 | use CPS::Future; |
12 | |
3f1f1e66 |
13 | our @EXPORT = qw(future await_future await_all); |
dc28afe8 |
14 | |
fbd3b8ec |
15 | sub future (&;$) { |
dc28afe8 |
16 | my $f = $_[0]->(CPS::Future->new); |
fbd3b8ec |
17 | return $f if ((caller(1+($_[1]||0))||'') eq 'start'); |
dc28afe8 |
18 | await_future($f); |
19 | } |
20 | |
fbd3b8ec |
21 | our @await; |
22 | |
dc28afe8 |
23 | sub await_future { |
24 | my $f = shift; |
2d81cf18 |
25 | log_trace { my $ir = $f->is_ready; "await_future() invoked; is_ready: $ir" }; |
dc28afe8 |
26 | return $f if $f->is_ready; |
27 | require Object::Remote; |
28 | my $loop = Object::Remote->current_loop; |
fbd3b8ec |
29 | { |
30 | local @await = (@await, $f); |
31 | $f->on_ready(sub { |
2d81cf18 |
32 | log_trace { my $l = @await; "future has become ready, length of \@await: '$l'" }; |
33 | if ($f == $await[-1]) { |
5953edf6 |
34 | log_trace { "This future is not waiting on anything so calling stop on the run loop" }; |
2d81cf18 |
35 | $loop->stop; |
36 | } |
fbd3b8ec |
37 | }); |
5953edf6 |
38 | log_trace { "Starting run loop for newly created future" }; |
fbd3b8ec |
39 | $loop->run; |
40 | } |
41 | if (@await and $await[-1]->is_ready) { |
5953edf6 |
42 | log_trace { "Last future in await list was ready, stopping run loop" }; |
fbd3b8ec |
43 | $loop->stop; |
44 | } |
2d81cf18 |
45 | log_trace { "await_future() returning" }; |
dc28afe8 |
46 | return wantarray ? $f->get : ($f->get)[0]; |
47 | } |
48 | |
3f1f1e66 |
49 | sub await_all { |
2d81cf18 |
50 | log_trace { my $l = @_; "await_all() invoked with '$l' futures to wait on" }; |
f6888810 |
51 | await_future(CPS::Future->wait_all(@_)); |
3f1f1e66 |
52 | map $_->get, @_; |
53 | } |
54 | |
dc28afe8 |
55 | package start; |
56 | |
fbd3b8ec |
57 | our $start = sub { my ($obj, $call) = (shift, shift); $obj->$call(@_); }; |
58 | |
dc28afe8 |
59 | sub AUTOLOAD { |
60 | my $invocant = shift; |
8ba4f2e3 |
61 | my ($method) = our $AUTOLOAD =~ /^start::(.+)$/; |
dc28afe8 |
62 | my $res; |
63 | unless (eval { $res = $invocant->$method(@_); 1 }) { |
64 | my $f = CPS::Future->new; |
65 | $f->fail($@); |
66 | return $f; |
67 | } |
68 | unless (Scalar::Util::blessed($res) and $res->isa('CPS::Future')) { |
69 | my $f = CPS::Future->new; |
70 | $f->done($res); |
71 | return $f; |
72 | } |
73 | return $res; |
74 | } |
75 | |
fbd3b8ec |
76 | package maybe; |
77 | |
78 | sub start { |
79 | my ($obj, $call) = (shift, shift); |
80 | if ((caller(1)||'') eq 'start') { |
81 | $obj->$start::start($call => @_); |
82 | } else { |
83 | $obj->$call(@_); |
84 | } |
85 | } |
86 | |
9822fc76 |
87 | package maybe::start; |
88 | |
89 | sub AUTOLOAD { |
90 | my $invocant = shift; |
91 | my ($method) = our $AUTOLOAD =~ /^maybe::start::(.+)$/; |
92 | $method = "start::${method}" if ((caller(1)||'') eq 'start'); |
93 | $invocant->$method(@_); |
94 | } |
95 | |
11e7c8a5 |
96 | package then; |
97 | |
98 | sub AUTOLOAD { |
99 | my $invocant = shift; |
100 | my ($method) = our $AUTOLOAD =~ /^then::(.+)$/; |
101 | my @args = @_; |
102 | # Need two copies since if we're called on an already complete future |
103 | # $f will be freed immediately |
104 | my $ret = my $f = CPS::Future->new; |
105 | $invocant->on_fail(sub { $f->fail(@_); undef($f); }); |
106 | $invocant->on_done(sub { |
107 | my ($obj) = @_; |
108 | my $next = $obj->${\"start::${method}"}(@args); |
109 | $next->on_done(sub { $f->done(@_); undef($f); }); |
110 | $next->on_fail(sub { $f->fail(@_); undef($f); }); |
111 | }); |
112 | return $ret; |
113 | } |
114 | |
dc28afe8 |
115 | 1; |
b9a9982d |
116 | |
117 | =head1 NAME |
118 | |
119 | Object::Remote::Future - Asynchronous calling for L<Object::Remote> |
120 | |
121 | =head1 LAME |
122 | |
123 | Shipping prioritised over writing this part up. Blame mst. |
124 | |
125 | =cut |