save state

This commit is contained in:
2026-01-16 16:34:32 +01:00
parent fbb6917812
commit a35c222570
15 changed files with 1052 additions and 266 deletions

View File

@@ -1,4 +1,4 @@
// Hot-reload file watcher implementation
// D:\Dev\Mosis\MosisService\designer\src\hot_reload.cpp
#include "hot_reload.h"
#include <iostream>
@@ -6,12 +6,11 @@
#include <Windows.h>
#endif
namespace mosis::desktop {
namespace mosis {
HotReload::HotReload(const std::filesystem::path& watch_path, ReloadCallback callback)
: m_watch_path(watch_path)
, m_callback(std::move(callback))
, m_last_change(std::chrono::steady_clock::now())
{}
HotReload::~HotReload() {
@@ -21,31 +20,28 @@ HotReload::~HotReload() {
void HotReload::Start() {
if (m_running) return;
m_running = true;
#ifdef _WIN32
m_notification_handle = FindFirstChangeNotificationW(
m_watch_path.c_str(),
TRUE, // Watch subtree
FILE_NOTIFY_CHANGE_LAST_WRITE
);
if (m_notification_handle == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create file change notification for: "
<< m_watch_path << std::endl;
std::cerr << "Failed to set up file watching" << std::endl;
m_running = false;
return;
}
#endif
m_running = true;
m_watch_thread = std::thread(&HotReload::WatchThread, this);
}
void HotReload::Stop() {
m_running = false;
if (m_watch_thread.joinable()) {
m_watch_thread.join();
}
#ifdef _WIN32
if (m_notification_handle && m_notification_handle != INVALID_HANDLE_VALUE) {
FindCloseChangeNotification(m_notification_handle);
@@ -54,40 +50,23 @@ void HotReload::Stop() {
#endif
}
bool HotReload::CheckForChanges() {
if (m_change_detected) {
auto now = std::chrono::steady_clock::now();
if (now - m_last_change >= m_debounce_delay) {
m_change_detected = false;
if (m_callback) {
m_callback();
}
return true;
}
}
return false;
}
void HotReload::WatchThread() {
while (m_running) {
#ifdef _WIN32
DWORD result = WaitForSingleObject(m_notification_handle, 100); // 100ms timeout
if (result == WAIT_OBJECT_0) {
m_last_change = std::chrono::steady_clock::now();
m_change_detected = true;
// Reset the notification
if (!FindNextChangeNotification(m_notification_handle)) {
std::cerr << "FindNextChangeNotification failed" << std::endl;
break;
// Debounce - wait a bit for file writes to complete
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (m_callback) {
m_callback();
}
FindNextChangeNotification(m_notification_handle);
}
#else
// TODO: Linux inotify / macOS FSEvents implementation
// For now, just sleep to avoid busy loop
std::this_thread::sleep_for(std::chrono::milliseconds(100));
#endif
}
}
} // namespace mosis::desktop
} // namespace mosis