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
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
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
|
|
|
|
2021-07-19 03:23:42 +02:00
|
|
|
use PBot::Utils::PriorityQueue;
|
|
|
|
|
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-19 03:23:42 +02:00
|
|
|
my ($self, $event_name, $subref, $priority) = @_;
|
2021-07-05 07:25:41 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2021-07-19 03:23:42 +02:00
|
|
|
my $entry = {
|
|
|
|
priority => $priority // 50,
|
|
|
|
id => $handler_id,
|
|
|
|
subref => $subref,
|
|
|
|
};
|
|
|
|
|
|
|
|
# create new priority-queue for event-name if one doesn't exist
|
|
|
|
if (not exists $self->{handlers}->{$event_name}) {
|
|
|
|
$self->{handlers}->{$event_name} = PBot::Utils::PriorityQueue->new(pbot => $self->{pbot});
|
|
|
|
}
|
|
|
|
|
2021-07-05 07:25:41 +02:00
|
|
|
# add the event handler
|
2021-07-19 03:23:42 +02:00
|
|
|
$self->{handlers}->{$event_name}->add($entry);
|
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-19 03:23:42 +02:00
|
|
|
my $handlers = $self->{handlers}->{$event_name};
|
|
|
|
|
|
|
|
for (my $i = 0; $i < $handlers->count; $i++) {
|
|
|
|
my $handler = $handlers->get($i);
|
|
|
|
|
|
|
|
if ($handler->{id} eq $handler_id) {
|
|
|
|
$handlers->remove($i--);
|
|
|
|
}
|
|
|
|
}
|
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-19 03:23:42 +02:00
|
|
|
if (not $self->{handlers}->{$event_name}->count) {
|
2021-07-05 07:25:41 +02:00
|
|
|
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-19 03:23:42 +02:00
|
|
|
foreach my $handler ($self->{handlers}->{$event_name}->entries) {
|
2021-07-05 07:25:41 +02:00
|
|
|
# debugging
|
|
|
|
if ($debug) {
|
2021-07-19 03:23:42 +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-19 03:23:42 +02:00
|
|
|
my $handler_result = eval { $handler->{subref}->($event_name, $event_data) };
|
2021-07-07 03:43:52 +02:00
|
|
|
|
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;
|