refactor: remove global state from fs module and use dispatcher

Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-03 21:11:12 +00:00
parent 9abf48b567
commit d5082fc856
3 changed files with 9 additions and 6 deletions

11
fs.c
View File

@@ -1,5 +1,6 @@
#include "fs.h"
#include "wav_io.h"
#include "dispatcher.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -19,7 +20,6 @@ static pthread_mutex_t autosave_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t autosave_cond = PTHREAD_COND_INITIALIZER;
static volatile bool autosave_running = false;
static volatile bool autosave_pending = false;
static AppState *g_state = NULL;
static time_t last_autosave_time = 0;
// Directory for autosave files
@@ -56,6 +56,10 @@ static void* autosave_thread_func(void *arg) {
// Only autosave if at least 10 seconds have passed since last save
time_t now = time(NULL);
if (now - last_autosave_time >= 10) {
// Get current state from dispatcher
AppState state;
dispatcher_get_state(&state);
// Generate autosave filename with timestamp
char filename[512];
time_t t = time(NULL);
@@ -66,7 +70,7 @@ static void* autosave_thread_func(void *arg) {
tm->tm_hour, tm->tm_min, tm->tm_sec);
// Save the project
fs_save_project(filename, g_state);
fs_save_project(filename, &state);
last_autosave_time = now;
// Remove old autosaves (keep last 10)
@@ -96,8 +100,7 @@ static void* autosave_thread_func(void *arg) {
return NULL;
}
void fs_init(AppState *state) {
g_state = state;
void fs_init(void) {
autosave_running = true;
autosave_pending = false;
ensure_autosave_dir();

2
fs.h
View File

@@ -4,7 +4,7 @@
#include "dispatcher.h"
// Initialize the auto-save thread
void fs_init(AppState *state);
void fs_init(void);
// Cleanup the auto-save thread
void fs_cleanup(void);

2
main.c
View File

@@ -130,7 +130,7 @@ int main(int argc, char *argv[]) {
free(initial_state);
// Initialize filesystem module (auto-save thread)
fs_init(initial_state);
fs_init();
// Initialize engine
if (engine_init(&engine, client_name, dispatch) != 0) {