2021-06-19 06:23:34 +02:00
|
|
|
# File: EventDispatcher.pm
|
|
|
|
#
|
|
|
|
# Purpose: Registers event handlers and dispatches events to them.
|
2021-06-22 02:26:24 +02:00
|
|
|
#
|
|
|
|
# Note: PBot::EventDispatcher has no relation to PBot::EventQueue.
|
2021-06-19 06:23:34 +02:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-11-01 01:15:21 +01:00
|
|
|
package PBot::EventDispatcher;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::Class';
|
2014-11-01 01:15:21 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2014-11-01 01:15:21 +01:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# hash table of event handlers
|
|
|
|
$self->{handlers} = {};
|
2014-11-01 01:15:21 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# add an event handler
|
2014-11-01 01:15:21 +01:00
|
|
|
sub register_handler {
|
2021-07-05 07:25:41 +02:00
|
|
|
my ($self, $event_name, $subref) = @_;
|
|
|
|
|
|
|
|
# get the package of the calling subroutine
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($package) = caller(0);
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# internal identifier to find calling package's event handler
|
2021-07-05 08:26:54 +02:00
|
|
|
my $handler_id = "$package-$event_name";
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# add the event handler
|
2021-07-05 08:26:54 +02:00
|
|
|
$self->{handlers}->{$event_name}->{$handler_id} = $subref;
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# debugging
|
|
|
|
if ($self->{pbot}->{registry}->get_value('eventdispatcher', 'debug')) {
|
2021-07-05 08:26:54 +02:00
|
|
|
$self->{pbot}->{logger}->log("EventDispatcher: Add handler: $handler_id\n");
|
2021-07-05 07:25:41 +02:00
|
|
|
}
|
2014-11-01 01:15:21 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# remove an event handler
|
2020-02-07 21:12:55 +01:00
|
|
|
sub remove_handler {
|
2021-07-05 07:25:41 +02:00
|
|
|
my ($self, $event_name) = @_;
|
|
|
|
|
|
|
|
# get the package of the calling subroutine
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($package) = caller(0);
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# internal identifier to find calling package's event handler
|
2021-07-05 08:26:54 +02:00
|
|
|
my $handler_id = "$package-$event_name";
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# remove the event handler
|
|
|
|
if (exists $self->{handlers}->{$event_name}) {
|
2021-07-05 08:26:54 +02:00
|
|
|
delete $self->{handlers}->{$event_name}->{$handler_id};
|
2021-07-05 07:25:41 +02:00
|
|
|
|
2021-07-05 08:26:54 +02:00
|
|
|
# remove root event-name key if it has no more handlers
|
2021-07-05 07:25:41 +02:00
|
|
|
if (not keys %{$self->{handlers}->{$event_name}}) {
|
|
|
|
delete $self->{handlers}->{$event_name};
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2020-02-07 21:12:55 +01:00
|
|
|
}
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# debugging
|
|
|
|
if ($self->{pbot}->{registry}->get_value('eventdispatcher', 'debug')) {
|
2021-07-05 08:26:54 +02:00
|
|
|
$self->{pbot}->{logger}->log("EventDispatcher: Remove handler: $handler_id\n");
|
2021-07-05 07:25:41 +02:00
|
|
|
}
|
2020-02-07 21:12:55 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# send an event to its handlers
|
2014-11-01 01:15:21 +01:00
|
|
|
sub dispatch_event {
|
2021-07-05 07:25:41 +02:00
|
|
|
my ($self, $event_name, $event_data) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# debugging flag
|
|
|
|
my $debug = $self->{pbot}->{registry}->get_value('eventdispatcher', 'debug') // 0;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-08 05:15:01 +02:00
|
|
|
# undef means no handlers have handled this event
|
2021-07-07 03:43:52 +02:00
|
|
|
my $dispatch_result= undef;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# if the event-name has handlers
|
|
|
|
if (exists $self->{handlers}->{$event_name}) {
|
|
|
|
# then dispatch the event to each one
|
2021-07-05 08:26:54 +02:00
|
|
|
foreach my $handler_id (keys %{$self->{handlers}->{$event_name}}) {
|
2021-07-08 05:15:01 +02:00
|
|
|
# get event handler subref
|
2021-07-05 08:26:54 +02:00
|
|
|
my $subref = $self->{handlers}->{$event_name}->{$handler_id};
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# debugging
|
|
|
|
if ($debug) {
|
2021-07-05 08:26:54 +02:00
|
|
|
$self->{pbot}->{logger}->log("Dispatching $event_name to handler $handler_id\n");
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2021-07-08 05:15:01 +02:00
|
|
|
# invoke an event handler. a handler may return undef to indicate
|
|
|
|
# that it decided not to handle this event.
|
2021-07-07 03:43:52 +02:00
|
|
|
my $handler_result = eval { $subref->($event_name, $event_data) };
|
|
|
|
|
2021-07-08 05:15:01 +02:00
|
|
|
# update $dispatch_result only when handler result is a defined
|
|
|
|
# value so we remember if any handlers have handled this event.
|
2021-07-07 03:55:14 +02:00
|
|
|
$dispatch_result = $handler_result if defined $handler_result;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-08 05:15:01 +02:00
|
|
|
# check for exception
|
|
|
|
if (my $exception = $@) {
|
|
|
|
$self->{pbot}->{logger}->log("Exception in event handler: $exception");
|
2021-07-05 07:25:41 +02:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2015-09-07 07:17:07 +02:00
|
|
|
}
|
2021-07-05 07:25:41 +02:00
|
|
|
|
2021-07-08 05:15:01 +02:00
|
|
|
# return undef if no handlers have handled this event; otherwise the return
|
|
|
|
# value of the last event handler to handle this event.
|
2021-07-07 03:43:52 +02:00
|
|
|
return $dispatch_result;
|
2014-11-01 01:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|