| 1 | package Async::Stream::Pushable; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use base qw(Async::Stream); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Use that class for creating streams which you can use to push item to them. | ||||||||||
| 14 | |||||||||||
| 15 | =head1 VERSION | ||||||||||
| 16 | |||||||||||
| 17 | Version 0.11 | ||||||||||
| 18 | |||||||||||
| 19 | =cut | ||||||||||
| 20 | |||||||||||
| 21 | 2 | our $VERSION = '0.12'; | |||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||
| 25 | |||||||||||
| 26 | Creating pushable streams, | ||||||||||
| 27 | you can use this type of streams for example for observer pattern | ||||||||||
| 28 | |||||||||||
| 29 | use Async::Stream::Pushable; | ||||||||||
| 30 | |||||||||||
| 31 | my $stream = Async::Stream::Pushable->new; | ||||||||||
| 32 | |||||||||||
| 33 | $stream->push(1,2,3)->finalize; | ||||||||||
| 34 | |||||||||||
| 35 | # Or for example | ||||||||||
| 36 | |||||||||||
| 37 | my $stream = Async::Stream::Pushable->new; | ||||||||||
| 38 | |||||||||||
| 39 | $some_object->subscribe(sub { | ||||||||||
| 40 | my $new_item = shift; | ||||||||||
| 41 | $stream->push($new_item); | ||||||||||
| 42 | }); | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | =head1 SUBROUTINES/METHODS | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | =head2 new(@array_of_items) | ||||||||||
| 49 | |||||||||||
| 50 | Constructor creates instance of class. | ||||||||||
| 51 | Class method create stream which you can use that to push items to that. | ||||||||||
| 52 | |||||||||||
| 53 | my $stream = Async::Stream::Pushable->new(@urls) | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | 1 | my $class = shift; | |||||||||
| 59 | |||||||||||
| 60 | 1 | my $self; | |||||||||
| 61 | $self = $class->SUPER::new( | ||||||||||
| 62 | 4 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||
| 63 | 4 | my ($return_cb) = @_; | |||||||||
| 64 | |||||||||||
| 65 | 4 | ▶ | if (@{$self->{_items}}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 3 | $return_cb->(shift @{ $self->{_items} }); | |||||||||
| 67 | } else { | ||||||||||
| 68 | 1 | ▶ | if ($self->{_is_finalized}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | $return_cb->(); | |||||||||
| 70 | } | ||||||||||
| 71 | else { | ||||||||||
| 72 | 0 | push @{ $self->{_requests} }, $return_cb; | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | }, | ||||||||||
| 76 | 1 | @_, | |||||||||
| 77 | ); | ||||||||||
| 78 | |||||||||||
| 79 | 1 | $self->{_items} = []; | |||||||||
| 80 | 1 | $self->{_requests} = []; | |||||||||
| 81 | 1 | $self->{_is_finalized} = 0; | |||||||||
| 82 | |||||||||||
| 83 | 1 | return $self; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | =head2 push(@new_items) | ||||||||||
| 87 | |||||||||||
| 88 | Push new items to stream | ||||||||||
| 89 | |||||||||||
| 90 | my $stream->push(@new_items); | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | 2 | ▶ | sub push { | ||||||||
|
Subroutine
push: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||
| 94 | 2 | my ($self, @new_items) = @_; | |||||||||
| 95 | |||||||||||
| 96 | 2 | ▶ | croak q{The stream is finalized} if ($self->{_is_finalized}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | 1 | ▶ | while (@{ $self->{_requests} }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | my $return_cb = shift @{ $self->{_requests} }; | |||||||||
| 100 | 0 | $return_cb->( shift @new_items ); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 1 | ▶ | if (@new_items) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 1 | push @{ $self->{_items} }, @new_items; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | return $self; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =head2 finalize() | ||||||||||
| 111 | |||||||||||
| 112 | Finalize stream | ||||||||||
| 113 | |||||||||||
| 114 | my $stream->finalize; | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | 2 | ▶ | sub finalize { | ||||||||
|
Subroutine
finalize: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 118 | 2 | my ($self) = @_; | |||||||||
| 119 | |||||||||||
| 120 | 2 | ▶ | croak q{The stream has already been finalized} if ($self->{_is_finalized}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | 1 | $self->{_is_finalized} = 1; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | =head1 AUTHOR | ||||||||||
| 126 | |||||||||||
| 127 | Kirill Sysoev, C<< <k.sysoev at me.com> >> | ||||||||||
| 128 | |||||||||||
| 129 | =head1 BUGS AND LIMITATIONS | ||||||||||
| 130 | |||||||||||
| 131 | Please report any bugs or feature requests to | ||||||||||
| 132 | L<https://github.com/pestkam/p5-Async-Stream/issues>. | ||||||||||
| 133 | |||||||||||
| 134 | =head1 SUPPORT | ||||||||||
| 135 | |||||||||||
| 136 | You can find documentation for this module with the perldoc command. | ||||||||||
| 137 | |||||||||||
| 138 | perldoc Async::Stream::Item | ||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 142 | |||||||||||
| 143 | Copyright 2017 Kirill Sysoev. | ||||||||||
| 144 | |||||||||||
| 145 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 146 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 147 | copy of the full license at: | ||||||||||
| 148 | |||||||||||
| 149 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 2 | 1; # End of Async::Stream::Pushable | |||||||||