From 9890ca99ee26732b96ab0f5f94e27214bb5c218e Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 19 Jul 2021 10:49:42 -0700 Subject: [PATCH] Add some comments about priority levels --- lib/PBot/EventDispatcher.pm | 16 ++++++++++++---- lib/PBot/NickList.pm | 13 ++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/PBot/EventDispatcher.pm b/lib/PBot/EventDispatcher.pm index 1c9ac38f..cbf24a5b 100644 --- a/lib/PBot/EventDispatcher.pm +++ b/lib/PBot/EventDispatcher.pm @@ -22,6 +22,14 @@ sub initialize { } # add an event handler +# +# priority ranges from 0 to 100. 0 is the highest priority, i.e. an handler with +# priority 0 will handle events first. 100 is the lowest priority and will handle +# events last. priority defaults to 50 if ommited. +# +# NickList reserves 0 and 100 to ensure its list is populated by JOINs, etc, +# before any handlers need to consult its list, or depopulated by PARTs, QUITs, +# KICKs, etc, after any other handlers need to consult its list. sub register_handler { my ($self, $event_name, $subref, $priority) = @_; @@ -108,13 +116,13 @@ sub dispatch_event { # that it decided not to handle this event. my $handler_result = eval { $handler->{subref}->($event_name, $event_data) }; - # update $dispatch_result only when handler result is a defined - # value so we remember if any handlers have handled this event. - $dispatch_result = $handler_result if defined $handler_result; - # check for exception if (my $exception = $@) { $self->{pbot}->{logger}->log("Exception in event handler: $exception"); + } else { + # update $dispatch_result only when handler result is a defined + # value so we remember if any handlers have handled this event. + $dispatch_result = $handler_result if defined $handler_result; } } } diff --git a/lib/PBot/NickList.pm b/lib/PBot/NickList.pm index 065312df..b20a9a27 100644 --- a/lib/PBot/NickList.pm +++ b/lib/PBot/NickList.pm @@ -34,19 +34,26 @@ sub initialize { $self->{pbot}->{commands}->register(sub { $self->cmd_nicklist(@_) }, "nicklist", 1); # handlers for various IRC events + # TODO: track mode changes to update user flags # Update: turns out that IRCHandler's on_mode() is doing this already -- we need to make that # emit a mode-change event or some such and register a handler for it here. + + # lowest priority so these get handled by NickList before any other handlers + # (all other handlers should be given a priority > 0) $self->{pbot}->{event_dispatcher}->register_handler('irc.namreply', sub { $self->on_namreply(@_) }, 0 ); $self->{pbot}->{event_dispatcher}->register_handler('irc.join', sub { $self->on_join(@_) }, 0 ); + $self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_activity(@_) }, 0 ); + $self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_activity(@_) }, 0 ); + + # highest priority so these get handled by NickList after all other handlers + # (all other handlers should be given a priority < 100) $self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_part(@_) }, 100 ); $self->{pbot}->{event_dispatcher}->register_handler('irc.quit', sub { $self->on_quit(@_) }, 100 ); $self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick(@_) }, 100 ); $self->{pbot}->{event_dispatcher}->register_handler('irc.nick', sub { $self->on_nickchange(@_) }, 100 ); - $self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_activity(@_) }, 0 ); - $self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_activity(@_) }, 0 ); - # handlers for the bot itself joining/leaving channels + # handlers for the bot itself joining/leaving channels (lowest priority) $self->{pbot}->{event_dispatcher}->register_handler('pbot.join', sub { $self->on_self_join(@_) }, 0); $self->{pbot}->{event_dispatcher}->register_handler('pbot.part', sub { $self->on_self_part(@_) }, 0); }