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   13207 use 5.010;
  4         11  
4 4     4   19 use strict;
  4         9  
  4         64  
5 4     4   21 use warnings;
  4         7  
  4         77  
6              
7 4     4   17 use Carp;
  4         7  
  4         264  
8              
9              
10             use constant {
11 4         1309 VALUE => 0,
12             NEXT => 1,
13             QUEUE => 2,
14 4     4   23 };
  4         6  
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 225     225 1 1247 my ($class, $val, $next) = @_;
58              
59 225 100 66     699 if (ref $next ne "CODE" and ref $next ne $class) {
60 1         142 croak "Second argument can be only subroutine reference or instance of class $class ";
61             }
62              
63 224         829 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 175     175 1 845 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 228     228 1 443 my $self = shift;
90 228         377 my $next_cb = shift;
91              
92 228 100       587 if (ref $next_cb ne "CODE") {
93 1         74 croak "First argument can be only subroutine reference";
94             }
95              
96 227 100       630 if (ref $self->[NEXT] eq "CODE") {
97 214         340 push @{$self->[QUEUE]}, $next_cb;
  214         497  
98 214 50       400 if (@{$self->[QUEUE]} == 1) {
  214         571  
99             $self->[NEXT](sub {
100 214     214   388 my @response;
101 214 100       453 if (@_) {
102 166         549 $self->[NEXT] = ref($self)->new($_[0], $self->[NEXT]);
103 166         427 @response = ($self->[NEXT]);
104             } else {
105 48         95 $self->[NEXT] = undef;
106             }
107              
108 214         342 for my $next_cb (@{$self->[QUEUE]}) {
  214         494  
109 214         608 $next_cb->(@response);
110             }
111 214         1245 });
112             }
113             } else {
114 13         32 $next_cb->($self->[NEXT]);
115             }
116            
117 227         24767 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