aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-04-05 16:20:05 +0530
committerAkshay Nair <phenax5@gmail.com>2025-04-05 16:20:05 +0530
commit09cdcd15b31469c839831cad534fec9896999b60 (patch)
tree4aea664837807d06b2056c83191143fb9a8f3d54 /src
parentc4413f3cca0a0750af9881276162b70f3489b499 (diff)
downloadnull-browser-09cdcd15b31469c839831cad534fec9896999b60.tar.gz
null-browser-09cdcd15b31469c839831cad534fec9896999b60.zip
Refactor event queue out of router
Diffstat (limited to '')
-rw-r--r--src/EventQueue.cpp31
-rw-r--r--src/EventQueue.hpp19
-rw-r--r--src/WindowActionRouter.cpp30
-rw-r--r--src/WindowActionRouter.hpp13
-rw-r--r--src/main.cpp7
5 files changed, 55 insertions, 45 deletions
diff --git a/src/EventQueue.cpp b/src/EventQueue.cpp
new file mode 100644
index 0000000..eebda76
--- /dev/null
+++ b/src/EventQueue.cpp
@@ -0,0 +1,31 @@
+#include "EventQueue.hpp"
+#include "LuaRuntime.hpp"
+
+void EventQueue::dispatch_event(BrowserEvent *event) {
+ auto &runtime = LuaRuntime::instance();
+
+ runtime.queue_task([this, event]() {
+ std::unordered_map<QString, std::vector<EventHandlerRequest>> event_map;
+ {
+ const std::lock_guard<std::mutex> lock(events_mutex);
+ event_map = events;
+ }
+
+ if (!event_map.contains(event->kind))
+ return;
+
+ for (auto &event_handler : event_map.at(event->kind)) {
+ // TODO: Pattern filter
+ event_handler.handler(event);
+ }
+ });
+}
+
+void EventQueue::register_event(const EventHandlerRequest &event) {
+ const std::lock_guard<std::mutex> lock(events_mutex);
+ for (const auto &event_name : event.event_names) {
+ if (!events.contains(event_name))
+ events.insert({event_name, {}});
+ events.at(event_name).emplace_back(event);
+ }
+}
diff --git a/src/EventQueue.hpp b/src/EventQueue.hpp
new file mode 100644
index 0000000..c4a9f76
--- /dev/null
+++ b/src/EventQueue.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <QtCore>
+#include <mutex>
+#include <unordered_map>
+
+#include "events.hpp"
+
+class EventQueue {
+public:
+ EventQueue() = default;
+
+ void dispatch_event(BrowserEvent *event);
+ void register_event(const EventHandlerRequest &event);
+
+private:
+ std::mutex events_mutex;
+ std::unordered_map<QString, std::vector<EventHandlerRequest>> events;
+};
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index e1d0664..8c80290 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -1,6 +1,5 @@
#include <QWidget>
#include <QtCore>
-#include <string>
#include <unordered_map>
#include "LuaRuntime.hpp"
@@ -10,35 +9,6 @@
#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"
-void WindowActionRouter::dispatch_event(BrowserEvent *event) {
- auto &runtime = LuaRuntime::instance();
-
- runtime.queue_task([this, event]() {
- std::unordered_map<QString, std::vector<EventHandlerRequest>> event_map;
- {
- const std::lock_guard<std::mutex> lock(events_mutex);
- event_map = events;
- }
-
- if (!event_map.contains(event->kind))
- return;
-
- for (auto &event_handler : event_map.at(event->kind)) {
- // TODO: Pattern filter
- event_handler.handler(event);
- }
- });
-}
-
-void WindowActionRouter::register_event(const EventHandlerRequest &event) {
- const std::lock_guard<std::mutex> lock(events_mutex);
- for (const auto &event_name : event.event_names) {
- if (!events.contains(event_name))
- events.insert({event_name, {}});
- events.at(event_name).emplace_back(event);
- }
-}
-
void WindowActionRouter::initialize() {
auto &runtime = LuaRuntime::instance();
diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp
index f7cb2b4..b72734c 100644
--- a/src/WindowActionRouter.hpp
+++ b/src/WindowActionRouter.hpp
@@ -4,11 +4,8 @@
#include <QtCore>
#include <cstdint>
#include <functional>
-#include <mutex>
-#include <string>
-#include <unordered_map>
-#include "events.hpp"
+#include "EventQueue.hpp"
#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"
@@ -37,8 +34,8 @@ public:
WebViewId fetch_current_tab_id(WindowId win_id = 0);
QList<WebViewData> fetch_webview_data_list(WindowId win_id = 0);
- void dispatch_event(BrowserEvent *event);
- void register_event(const EventHandlerRequest &event);
+ DELEGATE((&event_queue), dispatch_event, dispatch_event);
+ DELEGATE((&event_queue), register_event, register_event)
protected:
WindowActionRouter() = default;
@@ -54,7 +51,5 @@ private:
uint64_t last_id = 1;
Configuration *configuration;
- // TODO: Split event handling to its own class
- std::mutex events_mutex;
- std::unordered_map<QString, std::vector<EventHandlerRequest>> events;
+ EventQueue event_queue;
};
diff --git a/src/main.cpp b/src/main.cpp
index 03fc0d8..066a612 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,7 +2,6 @@
#include "InstanceManager.hpp"
#include "LuaRuntime.hpp"
-#include "WindowActionRouter.hpp"
#include "widgets/BrowserApp.hpp"
QCommandLineParser *create_cli_parser() {
@@ -35,13 +34,9 @@ int main(int argc, char *argv[]) {
QObject::connect(&instance_manager, &InstanceManager::lua_eval_requested,
&lua, &LuaRuntime::evaluate);
QObject::connect(&instance_manager, &InstanceManager::urls_open_requested,
- &lua, [browser](const QStringList &urls) {
- qDebug() << urls;
- browser->create_window(urls);
- });
+ browser, &BrowserApp::create_window);
} else {
qInfo() << "Using current instance";
-
auto urls = parser->positionalArguments();
auto lua_expr = parser->value("expr");