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   28 use strict;
  5         10  
  5         132  
4 5     5   21 use warnings;
  5         8  
  5         110  
5              
6 5     5   23 use base 'RxPerl::Subject';
  5         6  
  5         312  
7              
8 5     5   26 use Carp 'croak';
  5         10  
  5         262  
9 5     5   25 use Scalar::Util 'weaken';
  5         8  
  5         1181  
10              
11             our $VERSION = "v6.27.1";
12              
13             sub _on_subscribe {
14 2     2   4 my ($self, $subscriber) = @_;
15              
16 2 50       16 $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       10 @_ == 2 or croak 'missing initial value for behavior subject';
23              
24 3         20 my $self = $class->SUPER::new();
25              
26 3         7 $self->{_last_value} = $initial_value;
27              
28 3         12 weaken(my $w_self = $self);
29 3         5 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         11 $next_orig->(@_);
33 3         9 };
34              
35 3         8 bless $self, $class;
36             }
37              
38             sub get_value {
39 3     3 0 9 my ($self) = shift;
40              
41 3         30 return $self->{_last_value};
42             }
43              
44             1;