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 6     6   53679 use 5.010;
  6         19  
4 6     6   34 use strict;
  6         11  
  6         118  
5 6     6   26 use warnings;
  6         11  
  6         185  
6              
7 6     6   40 use Carp qw(croak);
  6         11  
  6         524  
8              
9             use constant {
10 6         2411 VALUE => 0,
11             NEXT => 1,
12             QUEUE => 2,
13 6     6   36 };
  6         33  
14              
15             =head1 NAME
16              
17             Item for Async::Stream
18              
19             =head1 VERSION
20              
21             Version 0.11
22              
23             =cut
24              
25             our $VERSION = '0.12';
26              
27              
28             =head1 SYNOPSIS
29              
30             Creating and managing item for Async::Stream
31              
32             use Async::Stream::Item;
33              
34             my $stream_item = Async::Stream::Item->new($value, $next_item_cb);
35              
36             =head1 SUBROUTINES/METHODS
37              
38             =head2 new($val,$generator)
39              
40             Constructor creates instance of class.
41             Class method gets 2 arguments item's value and generator subroutine references to generate next item.
42              
43             my $i = 0;
44             my $stream_item = Async::Stream::Item->new($i++, sub {
45             my $return_cb = shift;
46             if($i < 100){
47             $return_cb->($i++)
48             } else {
49             $return_cb->()
50             }
51             });
52              
53             =cut
54              
55             sub new {
56 255     255 1 2170 my ($class, $val, $next) = @_;
57              
58 255 100 66     550 if (ref $next ne "CODE" and ref $next ne $class) {
59 1         186 croak "Second argument can be only subroutine reference or instance of class $class ";
60             }
61              
62 254         717 return bless [ $val, $next, []], $class;
63             }
64              
65             =head2 val()
66              
67             Method returns item's value.
68              
69             my $value = $stream_item->val;
70              
71             =cut
72              
73             sub val {
74 198     198 1 891 return $_[0]->[VALUE];
75             }
76              
77             =head2 next($next_callback);
78            
79             Method returns next item in stream. Method gets callback to return next item.
80              
81             $stream_item->next(sub {
82             my $next_stream_item = shift;
83             });
84              
85             =cut
86              
87             sub next {
88 257     257 1 363 my $self = shift;
89 257         323 my $next_cb = shift;
90              
91 257 100       492 if (ref $next_cb ne "CODE") {
92 1         80 croak "First argument can be only subroutine reference";
93             }
94              
95 256 100       530 if (ref $self->[NEXT] eq "CODE") {
96 244         304 push @{$self->[QUEUE]}, $next_cb;
  244         409  
97 244 50       299 if (@{$self->[QUEUE]} == 1) {
  244         465  
98             $self->[NEXT](sub {
99 244     244   336 my @response;
100 244 100       377 if (@_) {
101 189         446 $self->[NEXT] = ref($self)->new($_[0], $self->[NEXT]);
102 189         374 @response = ($self->[NEXT]);
103             } else {
104 55         81 $self->[NEXT] = undef;
105             }
106              
107 244         307 for my $next_cb (@{$self->[QUEUE]}) {
  244         397  
108 244         457 $next_cb->(@response);
109             }
110 244         1029 });
111             }
112             } else {
113 12         26 $next_cb->($self->[NEXT]);
114             }
115            
116 256         1655 return;
117             }
118              
119             =head1 AUTHOR
120              
121             Kirill Sysoev, C<< >>
122              
123             =head1 BUGS
124              
125             Please report any bugs or feature requests to L.
126              
127             =head1 SUPPORT
128              
129             You can find documentation for this module with the perldoc command.
130              
131             perldoc Async::Stream::Item
132              
133              
134             =head1 LICENSE AND COPYRIGHT
135              
136             Copyright 2017 Kirill Sysoev.
137              
138             This program is free software; you can redistribute it and/or modify it
139             under the terms of the the Artistic License (2.0). You may obtain a
140             copy of the full license at:
141              
142             L
143              
144             =cut
145              
146             1; # End of Async::Stream::Item