| 1 | package Async::Stream::Item; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | VALUE => 0, | |||||||||||||||||||||||||||
| 11 | NEXT => 1, | ||||||||||||||||||||||||||||
| 12 | QUEUE => 2, | ||||||||||||||||||||||||||||
| 13 | 6 | }; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Item for Async::Stream | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 VERSION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Version 0.11 | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 6 | 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 | 255 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 255 | my ($class, $val, $next) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 255 | ▶ | if (ref $next ne "CODE" and ref $next ne $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $next ne "CODE" and ref $next ne $class
MC/DC50%1/2
ref $next ne "CODE" and ref $next ne $class
ref $next ne "CODE"
ref $next ne $class
Truth table66%2/3
A and B
A= ref $next ne "CODE"B= ref $next ne $class
| |||||||||||||||||||||||||||||
| 59 | 1 | croak "Second argument can be only subroutine reference or instance of class $class "; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 254 | 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 | 198 | ▶ | sub val { | ||||||||||||||||||||||||||
|
Subroutine
val: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 198 | 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 | 257 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 257 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 257 | my $next_cb = shift; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 257 | ▶ | if (ref $next_cb ne "CODE") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 1 | croak "First argument can be only subroutine reference"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 256 | ▶ | if (ref $self->[NEXT] eq "CODE") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 244 | push @{$self->[QUEUE]}, $next_cb; | |||||||||||||||||||||||||||
| 97 | 244 | ▶ | if (@{$self->[QUEUE]} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 244 | ▶ | $self->[NEXT](sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 99 | 244 | my @response; | |||||||||||||||||||||||||||
| 100 | 244 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 189 | $self->[NEXT] = ref($self)->new($_[0], $self->[NEXT]); | |||||||||||||||||||||||||||
| 102 | 189 | @response = ($self->[NEXT]); | |||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||
| 104 | 55 | $self->[NEXT] = undef; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 244 | for my $next_cb (@{$self->[QUEUE]}) { | |||||||||||||||||||||||||||
| 108 | 244 | $next_cb->(@response); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 244 | }); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||
| 113 | 12 | $next_cb->($self->[NEXT]); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 256 | return; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Kirill Sysoev, C<< <k.sysoev at me.com> >> | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 BUGS | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Please report any bugs or feature requests to L<https://github.com/pestkam/p5-Async-Stream/issues>. | ||||||||||||||||||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 6 | 1; # End of Async::Stream::Item | |||||||||||||||||||||||||||