File Coverage

blib/lib/Async/Stream/Iterator.pm
Criterion Covered Total %
statement 29 29 100.0
branch 7 8 87.5
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 45 46 97.8


line stmt bran cond sub pod time code
1             package Async::Stream::Iterator;
2              
3 3     3   40 use 5.010;
  3         8  
4 3     3   15 use strict;
  3         7  
  3         56  
5 3     3   14 use warnings;
  3         7  
  3         69  
6              
7 3     3   23 use Carp;
  3         10  
  3         649  
8              
9             =head1 NAME
10              
11             Iterator for Async stream
12              
13             =head1 VERSION
14              
15             Version 0.03
16              
17             =cut
18              
19             our $VERSION = '0.03';
20              
21              
22             =head1 SYNOPSIS
23              
24             Creating and managing item for Async::Stream
25              
26             use Async::Stream::Iterator;
27              
28             my $iterator = Async::Stream::Iterator->new($stream);
29            
30             =head1 SUBROUTINES/METHODS
31              
32             =head2 new($stream)
33              
34             Constructor creates instance of class.
35             Class method gets 1 arguments stream from which will be created iterator.
36              
37             my $iterator = Async::Stream::Iterator->new($stream);
38             =cut
39             sub new {
40 55     55 1 714 my ($class, $stream) = @_;
41              
42 55 100       218 if (!$stream->isa('Async::Stream')) {
43 1         125 croak "First argument can be only instance of Async::Stream or instance of derived class";
44             }
45              
46 54         147 my $item = $stream->head;
47              
48             return bless sub {
49 226     226   376 my $return_cb = shift;
50 226 50       547 return $return_cb->() if (!defined $item);
51              
52             $item->next(sub {
53 226 100       533 if (defined $_[0]) {
54 173         316 $item = shift;
55 173         467 $return_cb->($item->val)
56             } else {
57 53         131 $return_cb->()
58             }
59 226         1067 });
60 54         292 }, $class;
61             }
62              
63             =head2 next($returning_cb)
64              
65             Method gets returning callback and call that when iterator ready to return next value.
66              
67             $iterator->(sub {
68             my $item_value = shift;
69             });
70             =cut
71              
72             sub next {
73 227     227 1 938 my $self = shift;
74 227         367 my $return_cb = shift;
75              
76 227 100       595 if (ref $return_cb ne 'CODE') {
77 1         100 croak "First argument can be only subroutine reference"
78             }
79              
80 226         722 $self->($return_cb);
81              
82 226         747 return;
83             }
84              
85             =head1 AUTHOR
86              
87             Kirill Sysoev, C<< >>
88              
89             =head1 BUGS
90              
91             Please report any bugs or feature requests to L.
92              
93             =head1 SUPPORT
94              
95             You can find documentation for this module with the perldoc command.
96              
97             perldoc Async::Stream::Item
98              
99              
100             =head1 LICENSE AND COPYRIGHT
101              
102             Copyright 2017 Kirill Sysoev.
103              
104             This program is free software; you can redistribute it and/or modify it
105             under the terms of the the Artistic License (2.0). You may obtain a
106             copy of the full license at:
107              
108             L
109              
110             =cut
111              
112             1; # End of Async::Stream::Item