File Coverage

blib/lib/Async/Selector/Watcher.pm
Criterion Covered Total %
statement 36 36 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod 4 8 50.0
total 54 58 93.1


line stmt bran cond sub pod time code
1             package Async::Selector::Watcher;
2 15     15   148 use strict;
  15         25  
  15         472  
3 15     15   73 use warnings;
  15         27  
  15         424  
4 15     15   86 use Scalar::Util qw(weaken);
  15         24  
  15         3041  
5 15     15   77 use Carp;
  15         38  
  15         6694  
6              
7             sub new {
8 241     241 0 1480 my ($class, $selector, $conditions) = @_;
9 241         1277 my $self = bless {
10             selector => $selector,
11             conditions => $conditions,
12             check_all => 0,
13             }, $class;
14 241         1773 weaken($self->{selector});
15 241         855 return $self;
16             }
17              
18             sub detach {
19 354     354 0 455 my ($self) = @_;
20 354         863 $self->{selector} = undef;
21             }
22              
23             sub get_check_all {
24 552     552 0 826 my ($self) = @_;
25 552         1967 return $self->{check_all};
26             }
27              
28             sub set_check_all {
29 52     52 0 74 my ($self, $check_all) = @_;
30 52         121 $self->{check_all} = $check_all;
31             }
32              
33             sub cancel {
34 176     176 1 26346 my ($self) = @_;
35 176 100       593 return $self if not defined($self->{selector});
36 170         360 my $selector = $self->{selector};
37 170         374 $self->detach();
38 170         590 $selector->cancel($self);
39 170         474 return $self;
40             }
41              
42             sub conditions {
43 1413     1413 1 3307 my ($self) = @_;
44 1413         1386 return %{$self->{conditions}};
  1413         6399  
45             }
46              
47             sub resources {
48 407     407 1 8331 my ($self) = @_;
49 407         466 return keys %{$self->{conditions}};
  407         1930  
50             }
51              
52             sub active {
53 460     460 1 65030 my ($self) = @_;
54 460         2130 return defined($self->{selector});
55             }
56              
57             our $VERSION = '1.03';
58              
59             1;
60              
61             =pod
62              
63             =head1 NAME
64              
65             Async::Selector::Watcher - representation of resource watch in Async::Selector
66              
67             =head1 VERSION
68              
69             1.03
70              
71             =head1 SYNOPSIS
72              
73              
74             use Async::Selector;
75            
76             my $s = Async::Selector->new();
77            
78             setup_resources_with($s);
79            
80             ## Obtain a watcher from Selector.
81             my $watcher = $s->watch(a => 1, b => 2, sub {
82             my ($w, %res) = @_;
83             handle_a($res{a}) if exists $res{a};
84             handle_b($res{b}) if exists $res{b};
85             });
86            
87             ## Is the watcher active?
88             $watcher->active; ## => true
89            
90             ## Get the list of watched resources
91             my @resources = sort $watcher->resources; ## => ('a', 'b')
92            
93             ## Get the watcher conditions
94             my %conditions = $watcher->conditions; ## => (a => 1, b => 2)
95            
96             ## Cancel the watcher
97             $watcher->cancel;
98              
99              
100             =head1 DESCRIPTION
101              
102             L is an object that stores information about a resource watch in L module.
103             It also provides its user with a way to cancel the watch.
104              
105              
106             =head1 CLASS METHODS
107              
108             Nothing.
109              
110             L objects are created by C, C and C methods of L.
111              
112              
113             =head1 OBJECT METHODS
114              
115             In the following description, C<$watcher> is an L object.
116              
117             =head2 $is_active = $watcher->active();
118              
119             Returns true if the L is active. Returns false otherwise.
120              
121             Active watchers are the ones in L objects, watching some of the Selector's resources.
122             Callback functions of active watchers can be executed if the watched resources get available.
123              
124             Inactive watchers are the ones that have been removed from L objects.
125             Their callback functions are never executed any more.
126              
127             Note that watchers are automatically canceled and become inactive when their parent L object is destroyed.
128              
129              
130             =head2 $watcher->cancel();
131              
132             Cancels the watch.
133              
134             The C<$watcher> then becomes inactive and is removed from the L object it used to belong to.
135              
136              
137             =head2 @resources = $watcher->resources();
138              
139             Returns the list of resource names that are watched by this L object.
140              
141              
142              
143             =head2 %conditions = $watcher->conditions();
144              
145             Returns a hash whose keys are the resource names that are watched by this L object,
146             and values are the condition inputs for the resources.
147              
148              
149              
150             =head1 SEE ALSO
151              
152             L
153              
154              
155             =head1 AUTHOR
156              
157             Toshio Ito, C<< >>
158              
159             =head1 LICENSE AND COPYRIGHT
160              
161             Copyright 2012-2013 Toshio Ito.
162              
163             This program is free software; you can redistribute it and/or modify it
164             under the terms of either: the GNU General Public License as published
165             by the Free Software Foundation; or the Artistic License.
166              
167             See http://dev.perl.org/licenses/ for more information.
168              
169              
170              
171             =cut
172              
173