PrePAN

Sign in to PrePAN

State::Tracker Manage states and execute callbacks for predefined states

Author
Sadrak@github
Date
URL
Status
In Review
Good

Synopsis

# some own flags ; using constants are not necessary
{
    my $progress_flag;
    use constant PROGRESS_DONE     => $progress_flag = 0;
    use constant PROGRESS_FIND     => 2**$progress_flag++;
    use constant PROGRESS_MD6SUM   => 2**$progress_flag++;
    use constant PROGRESS_SIGNAL   => 2**$progress_flag++;
}

# changing & checking states

my $progress = State::Tracker->new;

$progress << PROGRESS_FIND # activate
$progress >> PROGRESS_FIND # deactivate
$progress <> PROGRESS_FIND # toggle

$progress += PROGRESS_FIND # increment counter; activate if > 0
$progress -= PROGRESS_FIND # decrement counter, deactivate if < 0

$progress += {PROGRESS_FIND => 10} # increment counter by 10; activate if > 0
$progress -= {PROGRESS_FIND => 20} # decrement counter by 20, deactivate if < 0

$progress == PROGRESS_DONE # check all flag
$progress != PROGRESS_DONE # check all flag
$progress <= PROGRESS_FIND # check only flag active
$progress >= PROGRESS_FIND # check only flag inactive

# register callbacks

$progress->callback(              
    active   => PROGRESS_FIND,
    sub {
        my ($self, $event) = @_;
        ...
        return 0 if $disable_event;
        return 1; # event stay active
    },
);
$progress->callback(              
    active   => PROGRESS_FIND|PROGRESS_SIGNAL,
    sub { ... },
);
$progress->callback(
    active   => PROGRESS_FIND,
    inactive => PROGRESS_MD6SUM,
    sub { ... },
);
$progress->callback(              
    counter   => {PROGRESS_FIND => 30},  
    sub { ... },
);
$progress->callback(
    counter   => [{PROGRESS_FIND => 30}, {PROGRESS_MD6SUM => -20}],
    sub { ... },
);
$progress->callback(
    counter   => [{PROGRESS_FIND => {'>=' => 30}}, {PROGRESS_MD6SUM => {'!=' => -20}}],
    sub { ... },
);

# using in a loop

while ($progress != PROGRESS_DONE) { ... }

Description

= ROADMAP =

  • first oop, then overload

  • allow non binary states (dispatch internally)

= Description =

This Modul should help to manager many states in an complex environment.

You can define callbacks for all states & combinations at one place and later change only the $progress.

I use a very simple approach in an old project and i want to make a easy implementation for a new project.

The events only get fired if a related flag/counter is changed.

I am absolute unsure if there are problems with such a interface (overloading) or should i better use simple function calls ($progress->activate(PROGRESS_FIND);)?

Also the signes for setting & checks flags are my first thought, perhaps i should stick with the default binary operators (but i am most of the time confused with them).

Also i guess i need some modes:

  • What if someone registered a event with an exact counter like 10 and later increment this counter by 2 from 9 to 11. Should the event fired or not? (mode => 'singlestep')

    • Otherwise if he use exact counters, he should avoid incrementing by more than 1
  • What is someone want to disallow setting states again? Like PROGRESS_FIND is off and he set PROGRESS_FIND again to off? (mode => 'strict')

  • ggf. einen speed-mode, der davon ausgeht, das bereit nur bitfields genutzt werden?

Comments

The overloaded interface (<< >>, +=) is confusing.

It's not clear what a counter is and what incrementing it does.

Please provide a more detailed description of the problem that you are trying to solve and how this module helps solve it.

Please sign up to post a review.