3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-22 20:09:43 +01:00

EventDispatcher: can now unregister handlers

This commit is contained in:
Pragmatic Software 2020-02-07 12:12:55 -08:00
parent 270df1551f
commit cf9c45f3e9

View File

@ -27,14 +27,31 @@ sub initialize {
} }
sub register_handler { sub register_handler {
my ($self, $event_type, $sub) = @_; my ($self, $event_type, $sub, $package_override) = @_;
my ($package) = caller(1);
my ($package, $filename, $line, $subroutine) = caller(1); $package = $package_override if defined $package_override;
my $info = "$filename:$line; $subroutine"; my $info = "$package\-\>$event_type";
$self->{pbot}->{logger}->log("Adding handler for $event_type: $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug'); $self->{pbot}->{logger}->log("Adding handler: $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug');
push @{$self->{handlers}->{$event_type}}, [$sub, $info]; push @{$self->{handlers}->{$event_type}}, [$sub, $info];
} }
sub remove_handler {
my ($self, $event_type, $package_override) = @_;
my ($package) = caller(1);
$package = $package_override if defined $package_override;
my $info = "$package\-\>$event_type";
if (exists $self->{handlers}->{$event_type}) {
for (my $i = 0; $i < @{$self->{handlers}->{$event_type}}; $i++) {
my $ref = @{$self->{handlers}->{$event_type}}[$i];
if ($info eq $ref->[1]) {
$self->{pbot}->{logger}->log("Removing handler: $info\n") if $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug');
splice @{$self->{handlers}->{$event_type}}, $i--, 1;
}
}
}
}
sub dispatch_event { sub dispatch_event {
my ($self, $event_type, $event_data) = @_; my ($self, $event_type, $event_data) = @_;
my $ret = undef; my $ret = undef;
@ -55,7 +72,6 @@ sub dispatch_event {
#$self->{pbot}->{logger}->log("Removing handler.\n"); #$self->{pbot}->{logger}->log("Removing handler.\n");
#splice @{$self->{handlers}->{$event_type}}, $i--, 1; #splice @{$self->{handlers}->{$event_type}}, $i--, 1;
} }
return $ret if $ret; return $ret if $ret;
} }
} }
@ -75,10 +91,8 @@ sub dispatch_event {
#$self->{pbot}->{logger}->log("Removing handler.\n"); #$self->{pbot}->{logger}->log("Removing handler.\n");
#splice @{$self->{handlers}->{any}}, $i--, 1; #splice @{$self->{handlers}->{any}}, $i--, 1;
} }
return $ret if $ret; return $ret if $ret;
} }
return $ret; return $ret;
} }