refactor(tickingservice): flatten nested if blocks with early returns

This commit is contained in:
2026-06-30 01:54:22 +02:00
parent b2cddea636
commit fed9372b35
+35 -26
View File
@@ -10,43 +10,52 @@
namespace ZShell::services { namespace ZShell::services {
TickingService::TickingService(QObject* parent) TickingService::TickingService(QObject* parent)
: Service(parent) : Service(parent), m_timer(new QTimer(this)) {
, m_timer(new QTimer(this)) {
m_timer->setSingleShot(false); m_timer->setSingleShot(false);
QObject::connect(m_timer, &QTimer::timeout, this, [this] { QObject::connect(m_timer, &QTimer::timeout, this, [this] { tick(); });
tick();
});
QString configPath = QDir::homePath() + QStringLiteral("/.config/zshell/config.json"); QString configPath =
QDir::homePath() + QStringLiteral("/.config/zshell/config.json");
auto reloadConfig = [this, configPath]() { auto reloadConfig = [this, configPath]() {
QFile file(configPath); QFile file(configPath);
if (file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); return;
if (!doc.isNull()) { }
QJsonObject dashboard = doc.object().value("dashboard").toObject(); QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
if (dashboard.contains("resourceUpdateInterval")) { if (doc.isNull()) {
applyInterval(dashboard.value("resourceUpdateInterval").toInt(1000)); return;
} }
} QJsonObject dashboard = doc.object().value("dashboard").toObject();
} if (dashboard.contains("resourceUpdateInterval")) {
}; applyInterval(
dashboard.value("resourceUpdateInterval").toInt(1000));
}
};
reloadConfig(); reloadConfig();
static auto* watcher = new QFileSystemWatcher(); static auto* watcher = new QFileSystemWatcher();
if (!watcher->files().contains(configPath)) { if (!watcher->files().contains(configPath)) {
QObject::connect(watcher, &QFileSystemWatcher::fileChanged, this, [this, configPath]() { QObject::connect(
watcher,
&QFileSystemWatcher::fileChanged,
this,
[this, configPath]() {
QTimer::singleShot(100, this, [this, configPath]() { QTimer::singleShot(100, this, [this, configPath]() {
QFile file(configPath); QFile file(configPath);
if (file.exists()) { if (!file.exists()) {
QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); return;
if (!doc.isNull()) { }
QJsonObject dashboard = doc.object().value("dashboard").toObject(); QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
if (dashboard.contains("resourceUpdateInterval")) { if (doc.isNull()) {
applyInterval(dashboard.value("resourceUpdateInterval").toInt(1000)); return;
} }
} QJsonObject dashboard =
doc.object().value("dashboard").toObject();
if (dashboard.contains("resourceUpdateInterval")) {
applyInterval(dashboard.value("resourceUpdateInterval")
.toInt(1000));
} }
}); });
}); });