# Simple cron-like component.
# Implements a recurring event generator that
# uses proxy objects and plain perl callbacks.
# Copyright 2004 by Rocco Caputo.  Free software.
# Same terms as Perl itself.  Have fun!

# The ChronicProxy class implements a proxy
# object facade over ChronicCallback.  The latter
# class is a singleton recurring callback service.

package ChronicProxy;

use warnings;
use strict;

use POE;
use ChronicCallback;

# Create a new proxy object.  Start the callback
# timer, and return an object representing it.

sub new {
  my ($class, %param) = @_;

  my $next_ticker_id =
    ChronicCallback->start(%param);

  return bless {
    MyID => $next_ticker_id,
  }, $class;
}

# Ensure that callback timers are stopped when
# their proxies are destroyed.

sub DESTROY {
  my $self = shift;
  $self->stop();
}

# Explicitly stop a recurring callback.

sub stop {
  my $self = shift;
  return unless $self->{MyID};

  ChronicCallback->stop(
    Id => $self->{MyID}
  );

  delete $self->{MyID};
}

# Run the timers, at least until there are none
# left.

sub run {
  ChronicCallback->run();
}

1;
