3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-12 13:59:28 +01:00
pbot/lib/PBot/Core/IRC/EventQueue.pm

77 lines
1.8 KiB
Perl
Raw Normal View History

2021-07-21 07:44:51 +02:00
package PBot::Core::IRC::EventQueue; # pragma_ 2011/21/01
2019-07-11 03:40:53 +02:00
use feature 'unicode_strings';
use utf8;
2019-07-11 03:40:53 +02:00
2021-07-21 07:44:51 +02:00
use PBot::Core::IRC::EventQueue::Entry; # pragma_ 2011/21/01
use strict;
sub new {
2020-02-15 23:38:32 +01:00
my $class = shift;
2019-06-26 18:34:19 +02:00
2020-02-15 23:38:32 +01:00
my $self = {
'queue' => {},
};
2020-02-15 23:38:32 +01:00
bless $self, $class;
}
sub queue {
2020-02-15 23:38:32 +01:00
my $self = shift;
return $self->{'queue'};
}
sub enqueue {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $time = shift;
my $content = shift;
2021-07-21 07:44:51 +02:00
my $entry = new PBot::Core::IRC::EventQueue::Entry($time, $content); # pragma_ 2011/21/01
2020-02-15 23:38:32 +01:00
$self->queue->{$entry->id} = $entry;
return $entry->id;
}
sub dequeue {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $event = shift;
my $result;
if (!$event) { # we got passed nothing, so return the first event
$event = $self->head();
delete $self->queue->{$event->id};
$result = $event;
} elsif (!ref($event)) { # we got passed an id
$result = $self->queue->{$event};
delete $self->queue->{$event};
} else { # we got passed an actual event object
2021-07-21 07:44:51 +02:00
ref($event) eq 'PBot::Core::IRC::EventQueue::Entry' # pragma_ 2011/21/01
2020-02-15 23:38:32 +01:00
or die "Cannot delete event type of " . ref($event) . "!";
$result = $self->queue->{$event->id};
delete $self->queue->{$event->id};
}
return $result;
}
sub head {
2020-02-15 23:38:32 +01:00
my $self = shift;
2020-02-15 23:38:32 +01:00
return undef if $self->is_empty;
2019-06-26 18:34:19 +02:00
2020-02-15 23:38:32 +01:00
no warnings; # because we want to numerically sort strings...
my $headkey = (sort { $a <=> $b } (keys(%{$self->queue})))[0];
use warnings;
2020-02-15 23:38:32 +01:00
return $self->queue->{$headkey};
}
sub is_empty {
2020-02-15 23:38:32 +01:00
my $self = shift;
2020-02-15 23:38:32 +01:00
return keys(%{$self->queue}) ? 0 : 1;
}
1;