1package AnyEvent::Subprocess::Job::Delegate::MonitorHandle;
2BEGIN {
3  $AnyEvent::Subprocess::Job::Delegate::MonitorHandle::VERSION = '1.102912';
4}
5# ABSTRACT: monitor a handle for input and invoke callbacks with that input
6use AnyEvent::Subprocess::Running::Delegate::MonitorHandle;
7use AnyEvent::Subprocess::Types qw(CodeList WhenToCallBack);
8use MooseX::Types::Moose qw(Str);
9
10use Moose;
11use namespace::autoclean;
12
13with 'AnyEvent::Subprocess::Job::Delegate';
14
15has 'handle' => (
16    is       => 'ro',
17    isa      => Str,
18    required => 1,
19);
20
21has 'callbacks' => (
22    traits     => ['Array'],
23    is         => 'ro',
24    isa        => CodeList,
25    required   => 1,
26    coerce     => 1,
27    auto_deref => 1,
28    handles    => {
29        add_callback => 'push',
30    },
31);
32
33has 'when' => (
34    is       => 'ro',
35    isa      => WhenToCallBack,
36    required => 1,
37    default  => sub { 'Line' },
38);
39
40sub parent_setup_hook {
41    my ($self, $job, $run) = @_;
42
43    my $handle = $run->delegate($self->handle)->handle;
44
45    if($self->when eq 'Line'){
46        my $reader; $reader = sub {
47            my ($h, $l, $eol) = @_;
48            $self->_run_callbacks($l, $eol);
49            $h->push_read(line => $reader);
50        };
51        $handle->push_read(line => $reader);
52    }
53    else {
54        $handle->on_read( sub {
55            my $h = shift;
56            $self->_run_callbacks( delete $h->{rbuf} );
57            return;
58        });
59    }
60
61    return;
62}
63
64sub _run_callbacks {
65    my $self = shift;
66
67    for my $cb ($self->callbacks){
68        $cb->(@_);
69    }
70
71    return;
72}
73
74sub build_run_delegates {
75    my $self = shift;
76    return AnyEvent::Subprocess::Running::Delegate::MonitorHandle->new(
77        name          => $self->name,
78        _job_delegate => $self,
79    );
80}
81
82sub child_setup_hook {}
83sub child_finalize_hook {}
84sub parent_finalize_hook {}
85sub build_code_args {}
86sub receive_child_result {}
87sub receive_child_error {}
88
89__PACKAGE__->meta->make_immutable;
90
911;
92
93
94
95=pod
96
97=head1 NAME
98
99AnyEvent::Subprocess::Job::Delegate::MonitorHandle - monitor a handle for input and invoke callbacks with that input
100
101=head1 VERSION
102
103version 1.102912
104
105=head1 DESCRIPTION
106
107Monitors a handle for input, and calls a list of coderefs when there is input.  The coderefs get the input.
108
109=head1 INITARGS
110
111=head2 name
112
113The name of the delegate that has the filehandle you want to monitor.
114
115=head2 callbacks
116
117ArrayRef[CodeRef]s to call with input.
118
119=head2 when
120
121'Line' to be called with each line, something else to be called
122whenever there is data in the read buffer.
123
124=head1 METHODS
125
126Don't call us, we'll call you.
127
128=head1 AUTHOR
129
130Jonathan Rockway <jrockway@cpan.org>
131
132=head1 COPYRIGHT AND LICENSE
133
134This software is copyright (c) 2011 by Jonathan Rockway.
135
136This is free software; you can redistribute it and/or modify it under
137the same terms as the Perl 5 programming language system itself.
138
139=cut
140
141
142__END__
143
144