Commit | Line | Data |
31a246e4 |
1 | package Tak::Client; |
2 | |
3 | use Tak; |
4 | use Tak::Request; |
5 | use Moo; |
6 | |
7 | has service => (is => 'ro', required => 1); |
8 | |
9 | has curried => (is => 'ro', default => sub { [] }); |
10 | |
11 | sub curry { |
12 | my ($self, @curry) = @_; |
13 | (ref $self)->new(%$self, curried => [ @{$self->curried}, @curry ]); |
14 | } |
15 | |
16 | sub send { |
17 | my ($self, @message) = @_; |
18 | $self->service->receive(@{$self->curried}, @message); |
19 | } |
20 | |
21 | sub start { |
22 | my ($self, $register, @payload) = @_; |
23 | my $req = $self->_new_request($register); |
24 | $self->service->start_request($req, @{$self->curried}, @payload); |
25 | return $req; |
26 | } |
27 | |
28 | sub request_class { 'Tak::Request' } |
29 | |
30 | sub _new_request { |
31 | my ($self, $args) = @_; |
32 | $self->request_class->new($args); |
33 | } |
34 | |
35 | sub do { |
77bf1d9b |
36 | shift->result_of(@_)->get; |
37 | } |
38 | |
39 | sub result_of { |
31a246e4 |
40 | my ($self, @payload) = @_; |
41 | my $done; |
42 | my $result; |
43 | my $req = $self->start({ |
44 | on_result => sub { $result = shift }, |
45 | }, @payload); |
46 | Tak->loop_until($result); |
77bf1d9b |
47 | return $result; |
31a246e4 |
48 | } |
49 | |
50 | 1; |