refactor: implement unidirectional data flow with dispatcher pattern

Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-02 14:27:56 +00:00
parent 2face8fe3d
commit d6ca5a0079
7 changed files with 684 additions and 1259 deletions

36
main.c
View File

@@ -7,13 +7,19 @@
#include "tui.h"
#include "cli.h"
#include "gui.h"
#include "dispatcher.h"
static Engine engine;
static volatile int keep_running = 1;
static DispatchFn dispatch = NULL;
void signal_handler(int sig) {
(void)sig;
keep_running = 0;
if (dispatch) {
Action action = { .type = ACTION_QUIT };
dispatch(action);
}
}
void print_usage(const char *program) {
@@ -63,22 +69,45 @@ int main(int argc, char *argv[]) {
}
}
// Create initial state
AppState initial_state;
memset(&initial_state, 0, sizeof(AppState));
initial_state.transport_state = TRANSPORT_STOPPED;
initial_state.clock_source = CLOCK_SOURCE_INTERNAL;
initial_state.bpm = 120.0;
initial_state.quantize_mode = QUANTIZE_OFF;
initial_state.quantize_threshold = 0;
initial_state.running = true;
for (int i = 0; i < MAX_CLIPS; i++) {
initial_state.clips[i].state = CLIP_EMPTY;
initial_state.clips[i].buffer = (float *)calloc(MAX_BUFFER_SIZE, sizeof(float));
initial_state.clips[i].buffer_size = 0;
initial_state.clips[i].write_position = 0;
initial_state.clips[i].read_position = 0;
}
// Initialize dispatcher
dispatch = dispatcher_init(&initial_state);
// Initialize engine
if (engine_init(&engine, client_name) != 0) {
if (engine_init(&engine, client_name, dispatch) != 0) {
fprintf(stderr, "Failed to initialize engine\n");
return 1;
}
engine.control_channel = control_channel;
// Set up signal handler
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
// Start dispatcher
dispatcher_start();
// Start engine
if (engine_start(&engine) != 0) {
fprintf(stderr, "Failed to start engine\n");
engine_cleanup(&engine);
dispatcher_stop();
return 1;
}
@@ -107,6 +136,7 @@ int main(int argc, char *argv[]) {
printf("\nShutting down...\n");
engine_stop(&engine);
engine_cleanup(&engine);
dispatcher_stop();
return 0;
}