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   36 use strict;
  5         10  
  5         153  
4 5     5   27 use warnings;
  5         9  
  5         130  
5              
6 5     5   27 use base 'RxPerl::Subject';
  5         10  
  5         328  
7              
8 5     5   28 use Carp 'croak';
  5         23  
  5         276  
9 5     5   29 use Scalar::Util 'weaken';
  5         22  
  5         1505  
10              
11             our $VERSION = "v6.28.0";
12              
13             sub _on_subscribe {
14 2     2   6 my ($self, $subscriber) = @_;
15              
16 2 50       8 $subscriber->{next}->($self->{_last_value}) if defined $subscriber->{next};
17             }
18              
19             sub new {
20 3     3 0 13 my ($class, $initial_value) = @_;
21              
22 3 50       12 @_ == 2 or croak 'missing initial value for behavior subject';
23              
24 3         22 my $self = $class->SUPER::new();
25              
26 3         8 $self->{_last_value} = $initial_value;
27              
28 3         23 weaken(my $w_self = $self);
29 3         8 my $next_orig = $self->{next};
30             $self->{next} = sub {
31 5 50   5   18 $w_self->{_last_value} = $_[0] unless $w_self->{_closed};
32 5         11 $next_orig->(@_);
33 3         13 };
34              
35 3         10 bless $self, $class;
36             }
37              
38             sub get_value {
39 3     3 0 12 my ($self) = shift;
40              
41 3         20 return $self->{_last_value};
42             }
43              
44             1;