Bot-ChatBots

 view release on metacpan or  search on metacpan

lib/Bot/ChatBots/Role/Poller.pm  view on Meta::CPAN

   my $self = shift;
   $self->schedule($self->interval, @{$self->args});
   return $self; # ignored
}

sub poller {
   my $self = shift;
   my $args = (@_ && ref($_[0]) ? $_[0] : {@_});

   # flag variable to avoid instances treading on each other
   my $is_busy;

   # callback where the poll function should push received data
   my $on_data = sub {
      my ($data) = @_;

      my @updates;
      try {
         @updates = $self->parse_response($data);
      }
      catch {

lib/Bot/ChatBots/Role/Poller.pm  view on Meta::CPAN

      my @retval = $self->process_updates(
         refs => {
            data => $data,
         },
         source_pairs => { },
         updates => \@updates,
         %$args, # may override it all!
      );

      $self->processed(@retval) if $self->can('processed');
      $is_busy = 0;
   };

   # this is what should be scheduled
   return sub {
      return if $is_busy;
      $is_busy = 1; # $on_data below will reset $is_busy when ready
      $self->poll($on_data, $args);
   };
}

sub schedule {
   my ($self, $interval, @rest) = @_;
   my $poller = $self->poller(@rest);
   Mojo::IOLoop->timer(0 => $poller);
   Mojo::IOLoop->recurring($interval, $poller);
   return $self;



( run in 0.249 second using v1.01-cache-2.11-cpan-87723dcf8b7 )