File Coverage

blib/lib/RxPerl/ReplaySubject.pm
Criterion Covered Total %
statement 37 37 100.0
branch 5 10 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 1 0.0
total 52 60 86.6


line stmt bran cond sub pod time code
1             package RxPerl::ReplaySubject;
2              
3 5     5   32 use strict;
  5         10  
  5         146  
4 5     5   23 use warnings;
  5         9  
  5         124  
5              
6 5     5   24 use base 'RxPerl::Subject';
  5         10  
  5         393  
7              
8 5     5   28 use Scalar::Util 'weaken';
  5         10  
  5         329  
9 5     5   34 use Time::HiRes 'time';
  5         8  
  5         52  
10              
11             our $VERSION = "v6.28.0";
12              
13             sub _on_subscribe {
14 2     2   5 my ($self, $subscriber) = @_;
15              
16 2 50       4 my $now; $now = time if defined $self->{_window_time};
  2         7  
17              
18 2 50       6 if (defined $subscriber->{next}) {
19 2         4 foreach my $replay_value (@{ $self->{_replay_values} }) {
  2         6  
20 4         14 my ($value, $time) = @$replay_value;
21 4 50 33     13 $subscriber->{next}->($value) if ! defined $self->{_window_time} or $time + $self->{_window_time} >= $now;
22             }
23             }
24             }
25              
26             sub _on_subscribe_closed {
27 1     1   4 _on_subscribe(@_);
28             }
29              
30             sub new {
31 1     1 0 4 my ($class, $replay_size, $window_time) = @_;
32              
33 1         7 my $self = $class->SUPER::new();
34              
35 1         7 $self->{_replay_values} = [];
36 1         3 $self->{_window_time} = $window_time;
37              
38 1         6 weaken(my $w_self = $self);
39 1         3 my $next_orig = $self->{next};
40             $self->{next} = sub {
41 5 50   5   24 push @{$w_self->{_replay_values}}, [$_[0], time] unless $w_self->{_closed};
  5         18  
42 5 50       13 splice @{$w_self->{_replay_values}}, 0, -$replay_size if defined $replay_size;
  5         9  
43 5         15 $next_orig->($_[0]);
44 1         4 };
45              
46 1         5 bless $self, $class;
47             }
48              
49             1;