File Coverage

blib/lib/Plack/Server/AnyEvent/Writer.pm
Criterion Covered Total %
statement 9 38 23.6
branch 0 4 0.0
condition n/a
subroutine 3 11 27.2
pod 0 4 0.0
total 12 57 21.0


line stmt bran cond sub pod time code
1             package Plack::Server::AnyEvent::Writer;
2              
3 1     1   6 use strict;
  1         2  
  1         31  
4 1     1   5 use warnings;
  1         2  
  1         25  
5              
6 1     1   6 use AnyEvent::Handle;
  1         2  
  1         428  
7              
8             sub new {
9 0     0 0   my ( $class, $socket, $exit ) = @_;
10              
11 0           bless { handle => AnyEvent::Handle->new( fh => $socket ), exit_guard => $exit }, $class;
12             }
13              
14             sub poll_cb {
15 0     0 0   my ( $self, $cb ) = @_;
16              
17 0           my $handle = $self->{handle};
18              
19 0 0         if ( $cb ) {
20             # notifies that now is a good time to ->write
21             $handle->on_drain(sub {
22 0     0     do {
23 0 0         if ( $self->{in_poll_cb} ) {
24 0           $self->{poll_again}++;
25 0           return;
26             } else {
27 0           local $self->{in_poll_cb} = 1;
28 0           $cb->($self);
29             }
30             } while ( delete $self->{poll_again} );
31 0           });
32              
33             # notifies of client close
34             $handle->on_error(sub {
35 0     0     my $err = $_[2];
36 0           $handle->destroy;
37 0           $cb->(undef, $err);
38 0           });
39             } else {
40 0           $handle->on_drain;
41 0           $handle->on_error;
42             }
43             }
44              
45 0     0 0   sub write { $_[0]{handle}->push_write($_[1]) }
46              
47             sub close {
48 0     0 0   my $self = shift;
49              
50 0           $self->{exit_guard}->end;
51              
52 0           my $handle = $self->{handle};
53              
54             # kill poll_cb, but not $handle
55 0           $handle->on_drain;
56 0           $handle->on_error;
57              
58             $handle->on_drain(sub {
59 0     0     shutdown $_[0]->fh, 1;
60 0           $_[0]->destroy;
61 0           undef $handle;
62 0           });
63             }
64              
65 0     0     sub DESTROY { $_[0]->close }
66              
67             # ex: set sw=4 et:
68              
69             1;
70             __END__