File Coverage

blib/lib/RxPerl/BehaviorSubject.pm
Criterion Covered Total %
statement 29 29 100.0
branch 3 6 50.0
condition n/a
subroutine 9 9 100.0
pod 0 2 0.0
total 41 46 89.1


line stmt bran cond sub pod time code
1             package RxPerl::BehaviorSubject;
2              
3 5     5   33 use strict;
  5         10  
  5         146  
4 5     5   26 use warnings;
  5         9  
  5         135  
5              
6 5     5   22 use base 'RxPerl::Subject';
  5         17  
  5         371  
7              
8 5     5   31 use Carp 'croak';
  5         11  
  5         245  
9 5     5   30 use Scalar::Util 'weaken';
  5         21  
  5         1405  
10              
11             our $VERSION = "v6.27.0";
12              
13             sub _on_subscribe {
14 2     2   4 my ($self, $subscriber) = @_;
15              
16 2 50       7 $subscriber->{next}->($self->{_last_value}) if defined $subscriber->{next};
17             }
18              
19             sub new {
20 3     3 0 9 my ($class, $initial_value) = @_;
21              
22 3 50       11 @_ == 2 or croak 'missing initial value for behavior subject';
23              
24 3         23 my $self = $class->SUPER::new();
25              
26 3         8 $self->{_last_value} = $initial_value;
27              
28 3         11 weaken(my $w_self = $self);
29 3         6 my $next_orig = $self->{next};
30             $self->{next} = sub {
31 5 50   5   14 $w_self->{_last_value} = $_[0] unless $w_self->{_closed};
32 5         12 $next_orig->(@_);
33 3         9 };
34              
35 3         11 bless $self, $class;
36             }
37              
38             sub get_value {
39 3     3 0 8 my ($self) = shift;
40              
41 3         124 return $self->{_last_value};
42             }
43              
44             1;