1#ifndef YAZE_APP_PLATFORM_WASM_AUTOSAVE_H_
2#define YAZE_APP_PLATFORM_WASM_AUTOSAVE_H_
10#include <unordered_map>
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "nlohmann/json.hpp"
29class AutoSaveManager {
32 using SaveCallback = std::function<nlohmann::json()>;
33 using RestoreCallback = std::function<void(
const nlohmann::json&)>;
39 static AutoSaveManager& Instance();
46 absl::Status Start(
int interval_seconds = 60);
58 bool IsRunning()
const;
66 void RegisterComponent(
const std::string& component_id,
68 RestoreCallback restore_fn);
74 void UnregisterComponent(
const std::string& component_id);
80 absl::Status SaveNow();
92 bool HasRecoveryData();
98 absl::StatusOr<nlohmann::json> GetRecoveryInfo();
104 absl::Status RecoverLastSession();
110 absl::Status ClearRecoveryData();
117 void SetInterval(
int seconds);
123 int GetInterval()
const {
return interval_seconds_; }
129 void SetEnabled(
bool enabled);
135 bool IsEnabled()
const {
return enabled_; }
141 std::chrono::system_clock::time_point GetLastSaveTime()
const {
142 return last_save_time_;
149 nlohmann::json GetStatistics()
const;
157 AutoSaveManager(
const AutoSaveManager&) =
delete;
158 AutoSaveManager& operator=(
const AutoSaveManager&) =
delete;
159 AutoSaveManager(AutoSaveManager&&) =
delete;
160 AutoSaveManager& operator=(AutoSaveManager&&) =
delete;
164 SaveCallback save_fn;
165 RestoreCallback restore_fn;
169 void InitializeEventHandlers();
170 void CleanupEventHandlers();
171 static void TimerCallback(
void* user_data);
172 absl::Status PerformSave();
173 nlohmann::json CollectComponentData();
174 absl::Status SaveToStorage(
const nlohmann::json& data);
175 absl::StatusOr<nlohmann::json> LoadFromStorage();
178 static constexpr const char* kAutoSaveDataKey =
"yaze_autosave_data";
179 static constexpr const char* kAutoSaveMetaKey =
"yaze_autosave_meta";
180 static constexpr const char* kRecoveryFlagKey =
"yaze_has_recovery";
183 mutable std::mutex mutex_;
184 std::unordered_map<std::string, Component> components_;
185 int interval_seconds_;
189 std::chrono::system_clock::time_point last_save_time_;
194 size_t recovery_count_;
197 bool event_handlers_initialized_;
201 static bool emergency_save_triggered_;