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   31 use strict;
  5         11  
  5         138  
4 5     5   24 use warnings;
  5         11  
  5         152  
5              
6 5     5   30 use base 'RxPerl::Subject';
  5         9  
  5         384  
7              
8 5     5   30 use Scalar::Util 'weaken';
  5         19  
  5         260  
9 5     5   30 use Time::HiRes 'time';
  5         9  
  5         57  
10              
11             our $VERSION = "v6.27.0";
12              
13             sub _on_subscribe {
14 2     2   5 my ($self, $subscriber) = @_;
15              
16 2 50       3 my $now; $now = time if defined $self->{_window_time};
  2         6  
17              
18 2 50       4 if (defined $subscriber->{next}) {
19 2         3 foreach my $replay_value (@{ $self->{_replay_values} }) {
  2         5  
20 4         11 my ($value, $time) = @$replay_value;
21 4 50 33     12 $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   3 _on_subscribe(@_);
28             }
29              
30             sub new {
31 1     1 0 3 my ($class, $replay_size, $window_time) = @_;
32              
33 1         6 my $self = $class->SUPER::new();
34              
35 1         3 $self->{_replay_values} = [];
36 1         3 $self->{_window_time} = $window_time;
37              
38 1         4 weaken(my $w_self = $self);
39 1         2 my $next_orig = $self->{next};
40             $self->{next} = sub {
41 5 50   5   11 push @{$w_self->{_replay_values}}, [$_[0], time] unless $w_self->{_closed};
  5         12  
42 5 50       10 splice @{$w_self->{_replay_values}}, 0, -$replay_size if defined $replay_size;
  5         7  
43 5         12 $next_orig->($_[0]);
44 1         3 };
45              
46 1         3 bless $self, $class;
47             }
48              
49             1;