From 1764bac919d05aeba7f7848219029eac16b99aea Mon Sep 17 00:00:00 2001 From: Alberto Nidasio <alberto.nidasio@skywarder.eu> Date: Mon, 14 Aug 2023 16:19:23 +0000 Subject: [PATCH] [MessageBroker] Fixed multiple callbacks firing --- src/shared/Core/Message/Filter.h | 2 +- src/shared/Core/MessageBroker/MessageBroker.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/shared/Core/Message/Filter.h b/src/shared/Core/Message/Filter.h index 44d95c49..d7116069 100644 --- a/src/shared/Core/Message/Filter.h +++ b/src/shared/Core/Message/Filter.h @@ -29,7 +29,7 @@ * The topic can contain wildcard symbols * and ? as last token. For example: * - "*" and "?" match with all topics * - "Prefix/?" matches with "Prefix", "Prefix/A" and "Prefix/A/B" - * - "Prefix/*" matches with "Prefix/A" and "Prefix/A/B" + * - "Prefix/any" matches with "Prefix/A" and "Prefix/A/B" * * The difference between * and ? is that the latter also matches when only the * prefix is present. diff --git a/src/shared/Core/MessageBroker/MessageBroker.cpp b/src/shared/Core/MessageBroker/MessageBroker.cpp index 6065f0bb..ba0a26e8 100644 --- a/src/shared/Core/MessageBroker/MessageBroker.cpp +++ b/src/shared/Core/MessageBroker/MessageBroker.cpp @@ -25,8 +25,8 @@ void MessageBroker::subscribe(Filter filter, Module* observer, { observers.insert(filter, {observer, new Callback(std::move(callback))}); - // Register the beforeDelete slot of the module to automatically remove the - // subscriber + // Register the beforeDelete slot of the module to automatically + // remove the subscriber connect(observer->getEventHandler(), &EventHandler::beforeDelete, this, &MessageBroker::onModuleDeleted); } @@ -44,9 +44,11 @@ void MessageBroker::unsubscribe(Filter filter, Module* module) void MessageBroker::publish(const Message& message) { - for (auto filter : observers.keys()) + // Iterate over all filters to check if the message matches any + for (auto filter : observers.uniqueKeys()) { - // If the message matches the filed, then notify all its subscribers + // If the message matches this particular filter, then notify all the + // subscribers if (filter.match(message)) { for (auto subscriber : observers.values(filter)) -- GitLab