line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Delayed responses |
2
|
|
|
|
|
|
|
$Dancer2::Core::Response::Delayed::VERSION = '0.400000'; |
3
|
|
|
|
|
|
|
use Moo; |
4
|
136
|
|
|
136
|
|
1352
|
use Dancer2::Core::Types qw<CodeRef InstanceOf>; |
|
136
|
|
|
|
|
254
|
|
|
136
|
|
|
|
|
769
|
|
5
|
136
|
|
|
136
|
|
39635
|
|
|
136
|
|
|
|
|
388
|
|
|
136
|
|
|
|
|
1459
|
|
6
|
|
|
|
|
|
|
has request => ( |
7
|
|
|
|
|
|
|
is => 'ro', |
8
|
|
|
|
|
|
|
isa => InstanceOf['Dancer2::Core::Request'], |
9
|
|
|
|
|
|
|
required => 1, |
10
|
|
|
|
|
|
|
); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has response => ( |
13
|
|
|
|
|
|
|
is => 'ro', |
14
|
|
|
|
|
|
|
isa => InstanceOf['Dancer2::Core::Response'], |
15
|
|
|
|
|
|
|
required => 1, |
16
|
|
|
|
|
|
|
handles => [qw/status headers push_header/], |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has cb => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
isa => CodeRef, |
22
|
|
|
|
|
|
|
required => 1, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has error_cb => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
isa => CodeRef, |
28
|
|
|
|
|
|
|
predicate => '_has_error_cb', |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return sub { |
35
|
|
|
|
|
|
|
my $responder = shift; |
36
|
17
|
|
|
17
|
1
|
4138
|
|
37
|
|
|
|
|
|
|
local $Dancer2::Core::Route::REQUEST = $self->request; |
38
|
|
|
|
|
|
|
local $Dancer2::Core::Route::RESPONSE = $self->response; |
39
|
17
|
|
|
17
|
|
1030
|
local $Dancer2::Core::Route::RESPONDER = $responder; |
40
|
|
|
|
|
|
|
local $Dancer2::Core::Route::WRITER; |
41
|
17
|
|
|
|
|
47
|
|
42
|
17
|
|
|
|
|
36
|
local $Dancer2::Core::Route::ERROR_HANDLER = |
43
|
17
|
|
|
|
|
59
|
$self->_has_error_cb ? $self->error_cb : undef; |
44
|
17
|
|
|
|
|
24
|
|
45
|
|
|
|
|
|
|
$self->cb->(); |
46
|
17
|
100
|
|
|
|
61
|
}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
17
|
|
|
|
|
66
|
1; |
50
|
17
|
|
|
|
|
86
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=pod |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=encoding UTF-8 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 NAME |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Dancer2::Core::Response::Delayed - Delayed responses |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 VERSION |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
version 0.400000 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 SYNOPSIS |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $response = Dancer2::Core::Response::Delayed->new( |
67
|
|
|
|
|
|
|
request => Dancer2::Core::Request->new(...), |
68
|
|
|
|
|
|
|
response => Dancer2::Core::Response->new(...), |
69
|
|
|
|
|
|
|
cb => sub {...}, |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# optional error handling |
72
|
|
|
|
|
|
|
error_cb => sub { |
73
|
|
|
|
|
|
|
my ($error) = @_; |
74
|
|
|
|
|
|
|
... |
75
|
|
|
|
|
|
|
}, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# or in an app |
79
|
|
|
|
|
|
|
get '/' => sub { |
80
|
|
|
|
|
|
|
# delayed response: |
81
|
|
|
|
|
|
|
delayed { |
82
|
|
|
|
|
|
|
# streaming content |
83
|
|
|
|
|
|
|
content "data"; |
84
|
|
|
|
|
|
|
content "more data"; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# close user connection |
87
|
|
|
|
|
|
|
done; |
88
|
|
|
|
|
|
|
} on_error => sub { |
89
|
|
|
|
|
|
|
my ($error) = @_; |
90
|
|
|
|
|
|
|
warning 'Failed to stream to user: ' . request->remote_address; |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
}; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 DESCRIPTION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This object represents a delayed (asynchronous) response for L<Dancer2>. |
97
|
|
|
|
|
|
|
It can be used via the C<delayed> keyword. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
It keeps references to a request and a response in order to avoid |
100
|
|
|
|
|
|
|
keeping a reference to the application. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 request |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Contains a request the delayed response uses. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
In the context of a web request, this will be the request that existed |
109
|
|
|
|
|
|
|
when the delayed response has been created. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 response |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Contains a response the delayed response uses. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
In the context of a web request, this will be the response that existed |
116
|
|
|
|
|
|
|
when the delayed response has been created. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 cb |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
The code that will be run asynchronously. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 error_cb |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
A callback for handling errors. This callback receives the error as its |
125
|
|
|
|
|
|
|
first (and currently only) parameter. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 METHODS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 is_halted |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
A method indicating whether the response has halted. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
This is useless in the context of an asynchronous request so it simply |
134
|
|
|
|
|
|
|
returns no. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This method is likely going away. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 has_passed |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
A method indicating whether the response asked to skip the current |
141
|
|
|
|
|
|
|
response. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This is useless in the context of an asynchronous request so it simply |
144
|
|
|
|
|
|
|
returns no. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This method is likely going away. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 to_psgi |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Create a PSGI response. The way it works is by returning a proper PSGI |
151
|
|
|
|
|
|
|
response subroutine which localizes the request and response (in case |
152
|
|
|
|
|
|
|
the callback wants to edit them without a reference to them), and then |
153
|
|
|
|
|
|
|
calls the callback. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Finally, when the callback is done, it asks the response (whether it |
156
|
|
|
|
|
|
|
was changed or not) to create its own PSGI response (calling C<to_psgi>) |
157
|
|
|
|
|
|
|
and sends that to the callback it receives as a delayed response. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 AUTHOR |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Dancer Core Developers |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
This software is copyright (c) 2022 by Alexis Sukrieh. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
168
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |