mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-20 02:49:49 +01:00
Progress on refactoring and polishing everything
This commit is contained in:
parent
1304dfde76
commit
2546b13ba6
@ -15,75 +15,90 @@ use PBot::Imports;
|
|||||||
|
|
||||||
sub initialize {
|
sub initialize {
|
||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
$self->{handlers} = {any => []};
|
|
||||||
|
# hash table of event handlers
|
||||||
|
$self->{handlers} = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# add an event handler
|
||||||
sub register_handler {
|
sub register_handler {
|
||||||
my ($self, $event_type, $sub, $package_override) = @_;
|
my ($self, $event_name, $subref) = @_;
|
||||||
my ($package) = caller(0);
|
|
||||||
$package = $package_override if defined $package_override;
|
|
||||||
my $info = "$package\-\>$event_type";
|
|
||||||
$self->{pbot}->{logger}->log("Adding handler: $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug');
|
|
||||||
push @{$self->{handlers}->{$event_type}}, [$sub, $info];
|
|
||||||
}
|
|
||||||
|
|
||||||
sub remove_handler {
|
# get the package of the calling subroutine
|
||||||
my ($self, $event_type, $package_override) = @_;
|
|
||||||
my ($package) = caller(0);
|
my ($package) = caller(0);
|
||||||
$package = $package_override if defined $package_override;
|
|
||||||
my $info = "$package\-\>$event_type";
|
|
||||||
|
|
||||||
if (exists $self->{handlers}->{$event_type}) {
|
# internal identifier to find calling package's event handler
|
||||||
for (my $i = 0; $i < @{$self->{handlers}->{$event_type}}; $i++) {
|
my $event_id = "$package-$event_name";
|
||||||
my $ref = @{$self->{handlers}->{$event_type}}[$i];
|
|
||||||
if ($info eq $ref->[1]) {
|
# add the event handler
|
||||||
$self->{pbot}->{logger}->log("Removing handler: $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug');
|
$self->{handlers}->{$event_name}->{$event_id} = $subref;
|
||||||
splice @{$self->{handlers}->{$event_type}}, $i--, 1;
|
|
||||||
}
|
# debugging
|
||||||
}
|
if ($self->{pbot}->{registry}->get_value('eventdispatcher', 'debug')) {
|
||||||
|
$self->{pbot}->{logger}->log("EventDispatcher: Add handler: $event_id\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# remove an event handler
|
||||||
|
sub remove_handler {
|
||||||
|
my ($self, $event_name) = @_;
|
||||||
|
|
||||||
|
# get the package of the calling subroutine
|
||||||
|
my ($package) = caller(0);
|
||||||
|
|
||||||
|
# internal identifier to find calling package's event handler
|
||||||
|
my $event_id = "$package-$event_name";
|
||||||
|
|
||||||
|
# remove the event handler
|
||||||
|
if (exists $self->{handlers}->{$event_name}) {
|
||||||
|
delete $self->{handlers}->{$event_name}->{$event_id};
|
||||||
|
|
||||||
|
# remove root event-name key if it has are no more handlers
|
||||||
|
if (not keys %{$self->{handlers}->{$event_name}}) {
|
||||||
|
delete $self->{handlers}->{$event_name};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# debugging
|
||||||
|
if ($self->{pbot}->{registry}->get_value('eventdispatcher', 'debug')) {
|
||||||
|
$self->{pbot}->{logger}->log("EventDispatcher: Remove handler: $event_id\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# send an event to its handlers
|
||||||
sub dispatch_event {
|
sub dispatch_event {
|
||||||
my ($self, $event_type, $event_data) = @_;
|
my ($self, $event_name, $event_data) = @_;
|
||||||
|
|
||||||
|
# debugging flag
|
||||||
|
my $debug = $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug') // 0;
|
||||||
|
|
||||||
|
# event handler return value
|
||||||
my $ret = undef;
|
my $ret = undef;
|
||||||
|
|
||||||
if (exists $self->{handlers}->{$event_type}) {
|
# if the event-name has handlers
|
||||||
for (my $i = 0; $i < @{$self->{handlers}->{$event_type}}; $i++) {
|
if (exists $self->{handlers}->{$event_name}) {
|
||||||
my $ref = @{$self->{handlers}->{$event_type}}[$i];
|
# then dispatch the event to each one
|
||||||
my ($handler, $info) = ($ref->[0], $ref->[1]);
|
foreach my $event_id (keys %{$self->{handlers}->{$event_name}}) {
|
||||||
my $debug = $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug') // 0;
|
# event handler subref
|
||||||
$self->{pbot}->{logger}->log("Dispatching $event_type to handler $info\n") if $debug > 1;
|
my $subref = $self->{handlers}->{$event_name}->{$event_id};
|
||||||
|
|
||||||
eval { $ret = $handler->($event_type, $event_data); };
|
# debugging
|
||||||
|
if ($debug) {
|
||||||
if ($@) {
|
$self->{pbot}->{logger}->log("Dispatching $event_name to handler $event_id\n");
|
||||||
chomp $@;
|
}
|
||||||
$self->{pbot}->{logger}->log("Error in event handler: $@\n");
|
|
||||||
|
# invoke event handler
|
||||||
#$self->{pbot}->{logger}->log("Removing handler.\n");
|
eval { $ret = $subref->($event_name, $event_data) };
|
||||||
#splice @{$self->{handlers}->{$event_type}}, $i--, 1;
|
|
||||||
|
# check for error
|
||||||
|
if (my $error = $@) {
|
||||||
|
chomp $error;
|
||||||
|
$self->{pbot}->{logger}->log("Error in event handler: $error\n");
|
||||||
}
|
}
|
||||||
return $ret if $ret;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (my $i = 0; $i < @{$self->{handlers}->{any}}; $i++) {
|
# return event handler result
|
||||||
my $ref = @{$self->{handlers}->{any}}[$i];
|
|
||||||
my ($handler, $info) = ($ref->[0], $ref->[1]);
|
|
||||||
$self->{pbot}->{logger}->log("Dispatching any to handler $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug');
|
|
||||||
|
|
||||||
eval { $ret = $handler->($event_type, $event_data); };
|
|
||||||
|
|
||||||
if ($@) {
|
|
||||||
chomp $@;
|
|
||||||
$self->{pbot}->{logger}->log("Error in event handler: $@\n");
|
|
||||||
|
|
||||||
#$self->{pbot}->{logger}->log("Removing handler.\n");
|
|
||||||
#splice @{$self->{handlers}->{any}}, $i--, 1;
|
|
||||||
}
|
|
||||||
return $ret if $ret;
|
|
||||||
}
|
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user