refactor(cpp/core): process_runner usa process_state_machine
This commit is contained in:
@@ -1,10 +1,26 @@
|
||||
#include "core/process_runner.h"
|
||||
#include "core/process_state_machine.h"
|
||||
#include "core/tokens.h"
|
||||
#include <imgui.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace fn_ui {
|
||||
|
||||
// Helpers para puentear el RunnerState (publico) con la SM pura.
|
||||
// RunnerState y ProcessState comparten orden y valores enteros (0..3).
|
||||
namespace {
|
||||
ProcessState to_ps(RunnerState s) { return static_cast<ProcessState>(static_cast<int>(s)); }
|
||||
RunnerState to_rs(ProcessState s) { return static_cast<RunnerState>(static_cast<int>(s)); }
|
||||
|
||||
// Aplica un evento al estado actual del runner via process_transition.
|
||||
void apply_event(ProcessRunner& r, std::atomic<int>& state, ProcessEvent ev) {
|
||||
(void)r;
|
||||
int cur = state.load();
|
||||
ProcessState ns = process_transition(to_ps(static_cast<RunnerState>(cur)), ev);
|
||||
state.store(static_cast<int>(to_rs(ns)));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ProcessRunner::ProcessRunner() = default;
|
||||
|
||||
ProcessRunner::~ProcessRunner() {
|
||||
@@ -26,7 +42,8 @@ std::string ProcessRunner::message() const {
|
||||
|
||||
void ProcessRunner::reset() {
|
||||
if (is_busy()) return;
|
||||
state_.store(static_cast<int>(RunnerState::Idle));
|
||||
// Reset event: Success/Error -> Idle (no-op si ya en Idle).
|
||||
apply_event(*this, state_, ProcessEvent::Reset);
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
message_.clear();
|
||||
}
|
||||
@@ -36,7 +53,9 @@ void runner_trigger(ProcessRunner& r,
|
||||
if (r.is_busy()) return;
|
||||
if (r.th_.joinable()) r.th_.join();
|
||||
|
||||
r.state_.store(static_cast<int>(RunnerState::Running));
|
||||
// Si venimos de Success/Error, primero Reset -> Idle, luego Spawned -> Running.
|
||||
apply_event(r, r.state_, ProcessEvent::Reset);
|
||||
apply_event(r, r.state_, ProcessEvent::Spawned);
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(r.mu_);
|
||||
r.message_.clear();
|
||||
@@ -55,7 +74,8 @@ void runner_trigger(ProcessRunner& r,
|
||||
std::lock_guard<std::mutex> lk(r.mu_);
|
||||
r.message_ = std::move(out);
|
||||
}
|
||||
r.state_.store(static_cast<int>(ok ? RunnerState::Success : RunnerState::Error));
|
||||
// Running -> Success/Error via SM pura.
|
||||
apply_event(r, r.state_, ok ? ProcessEvent::Finished : ProcessEvent::Failed);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ purity: impure
|
||||
signature: "class fn_ui::ProcessRunner { is_busy(); state(); message(); reset(); }; void runner_trigger(ProcessRunner&, std::function<bool(std::string&)>); void runner_status(const ProcessRunner&, const char* label)"
|
||||
description: "Ejecuta una tarea en std::thread en background y expone estado thread-safe (idle/running/success/error). Incluye widget inline con spinner + resultado."
|
||||
tags: [imgui, ui, async, thread, runner, tokens]
|
||||
uses_functions: ["tokens_cpp_core"]
|
||||
uses_functions: ["process_state_machine_cpp_core", "tokens_cpp_core"]
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
|
||||
Reference in New Issue
Block a user