File Coverage

blib/lib/Async/Stream/Item.pm
Criterion Covered Total %
statement 39 39 100.0
branch 9 10 90.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 3 3 100.0
total 62 64 96.8


line stmt bran cond sub pod time code
1             package Async::Stream::Item;
2              
3 4     4   22019 use 5.010;
  4         17  
4 4     4   27 use strict;
  4         11  
  4         110  
5 4     4   27 use warnings;
  4         10  
  4         119  
6              
7 4     4   26 use Carp;
  4         10  
  4         340  
8              
9              
10             use constant {
11 4         1624 VALUE => 0,
12             NEXT => 1,
13             QUEUE => 2,
14 4     4   28 };
  4         8  
15              
16             =head1 NAME
17              
18             Item for Async::Stream
19              
20             =head1 VERSION
21              
22             Version 0.05
23              
24             =cut
25              
26             our $VERSION = '0.05';
27              
28              
29             =head1 SYNOPSIS
30              
31             Creating and managing item for Async::Stream
32              
33             use Async::Stream::Item;
34              
35             my $stream_item = Async::Stream::Item->new($value, $next_item_cb);
36            
37             =head1 SUBROUTINES/METHODS
38              
39             =head2 new($val,$generator)
40              
41             Constructor creates instance of class.
42             Class method gets 2 arguments item's value and generator subroutine references to generate next item.
43              
44             my $i = 0;
45             my $stream_item = Async::Stream::Item->new($i++, sub {
46             my $return_cb = shift;
47             if($i < 100){
48             $return_cb->($i++)
49             } else {
50             $return_cb->()
51             }
52             });
53              
54             =cut
55              
56             sub new {
57 232     232 1 1086 my ($class, $val, $next) = @_;
58              
59 232 100 66     655 if (ref $next ne "CODE" and ref $next ne $class) {
60 1         136 croak "Second argument can be only subroutine reference or instance of class $class ";
61             }
62              
63 231         813 return bless [ $val, $next, []], $class;
64             }
65              
66             =head2 val()
67              
68             Method returns item's value.
69              
70             my $value = $stream_item->val;
71              
72             =cut
73              
74             sub val {
75 180     180 1 760 return $_[0]->[VALUE];
76             }
77              
78             =head2 next($next_callback);
79            
80             Method returns next item in stream. Method gets callback to return next item.
81              
82             $stream_item->next(sub {
83             my $next_stream_item = shift;
84             });
85              
86             =cut
87              
88             sub next {
89 235     235 1 412 my $self = shift;
90 235         626 my $next_cb = shift;
91              
92 235 100       587 if (ref $next_cb ne "CODE") {
93 1         78 croak "First argument can be only subroutine reference";
94             }
95              
96 234 100       566 if (ref $self->[NEXT] eq "CODE") {
97 221         319 push @{$self->[QUEUE]}, $next_cb;
  221         485  
98 221 50       340 if (@{$self->[QUEUE]} == 1) {
  221         526  
99             $self->[NEXT](sub {
100 221     221   385 my @response;
101 221 100       449 if (@_) {
102 171         469 $self->[NEXT] = ref($self)->new($_[0], $self->[NEXT]);
103 171         438 @response = ($self->[NEXT]);
104             } else {
105 50         90 $self->[NEXT] = undef;
106             }
107              
108 221         318 for my $next_cb (@{$self->[QUEUE]}) {
  221         454  
109 221         547 $next_cb->(@response);
110             }
111 221         1123 });
112             }
113             } else {
114 13         35 $next_cb->($self->[NEXT]);
115             }
116            
117 234         23665 return;
118             }
119              
120             =head1 AUTHOR
121              
122             Kirill Sysoev, C<< >>
123              
124             =head1 BUGS
125              
126             Please report any bugs or feature requests to L.
127              
128             =head1 SUPPORT
129              
130             You can find documentation for this module with the perldoc command.
131              
132             perldoc Async::Stream::Item
133              
134              
135             =head1 LICENSE AND COPYRIGHT
136              
137             Copyright 2017 Kirill Sysoev.
138              
139             This program is free software; you can redistribute it and/or modify it
140             under the terms of the the Artistic License (2.0). You may obtain a
141             copy of the full license at:
142              
143             L
144              
145             =cut
146              
147             1; # End of Async::Stream::Item