AnyEvent-Retry

 view release on metacpan or  search on metacpan

lib/AnyEvent/Retry/Interval/Multi.pm  view on Meta::CPAN

package AnyEvent::Retry::Interval::Multi;
BEGIN {
  $AnyEvent::Retry::Interval::Multi::VERSION = '0.03';
}
# ABSTRACT: combine multiple interval objects into one interval
use Moose;
use AnyEvent::Retry::Types qw(Interval);

use true;
use namespace::autoclean;

with 'AnyEvent::Retry::Interval';

has [qw/first then/] => (
    is       => 'ro',
    isa      => Interval,
    required => 1,
    coerce   => 1,
);

has '_source' => (
    accessor => '_source',
    isa      => 'Str',
    lazy     => 1,
    default  => 'first',
    clearer  => '_reset_source',
);

has 'after' => (
    is       => 'ro',
    isa      => 'Num|CodeRef',
    required => 1,
);

sub reset {
    my $self = shift;
    $self->_reset_source;
    $self->first->reset;
    $self->then->reset;
}

sub next {
    my ($self, $i) = @_;
    my $source = $self->_source;
    return $self->then->next if $self->_source eq 'then';

    my $next = $self->first->next;
    my $switch = $self->check_condition($i, $next);
    return $next if !$switch;

    $self->_source('then');
    return $self->then->next;
}

sub check_condition {
    my ($self, $i, $next) = @_;
    my $cond = $self->after;
    my $switch = 0;
    if(ref $cond){
        $switch = $cond->($i, $next) ? 1 : 0;
    }
    elsif($cond < 0){
        $switch = 1 if $i > abs($cond);
    }
    else {
        $switch = 1 if $next > $cond;
    }

    return $switch;
}



( run in 2.039 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )