From f0ec2ec35d02e7a719a9226ba6dd4c08c9ab304f Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 14 Aug 2024 23:46:29 -0400 Subject: [PATCH] add notification center as apart of message passing --- src/app/core/notification.h | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/app/core/notification.h diff --git a/src/app/core/notification.h b/src/app/core/notification.h new file mode 100644 index 00000000..c1cf7ac1 --- /dev/null +++ b/src/app/core/notification.h @@ -0,0 +1,48 @@ +#ifndef YAZE_APP_CORE_NOTIFICATION_H +#define YAZE_APP_CORE_NOTIFICATION_H + +#include +#include +#include +#include + +#include "app/core/message.h" + +namespace yaze { +namespace app { +namespace core { + +struct Notification : public Message { + Notification(const std::string& type, void* sender, std::any payload) + : Message{type, sender, payload} {} +}; + +class NotificationCenter { + public: + void AddObserver(const std::string& notificationType, + IMessageListener* observer) { + observers[notificationType].push_back(observer); + } + + void RemoveObserver(const std::string& notificationType, + IMessageListener* observer) { + auto& observerList = observers[notificationType]; + observerList.erase( + std::remove(observerList.begin(), observerList.end(), observer), + observerList.end()); + } + + void PostNotification(const Notification& notification) { + const auto& observerList = observers[notification.type]; + for (auto observer : observerList) { + observer->onMessageReceived(notification); + } + } + + private: + std::unordered_map> observers; +}; + +} // namespace core +} // namespace app +} // namespace yaze \ No newline at end of file