Skip to content

Commit

Permalink
Fix progress solver events overriding solution solver events (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
augenfrosch authored Sep 11, 2024
1 parent ac5350a commit b00331b
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub struct SolverConfig {
pub minimize_steps: bool,
}

pub struct SolverUpdates {
pub progress_update: Cell<Option<SolverEvent>>,
pub solution_update: Cell<Option<SolverEvent>>,
}

pub struct MacroSolverApp {
locale: Locale,
recipe_config: RecipeConfiguration,
Expand All @@ -62,22 +67,25 @@ pub struct MacroSolverApp {
solver_progress: usize,
start_time: Option<Instant>,
duration: Option<Duration>,
data_update: Rc<Cell<Option<SolverEvent>>>,
data_update: Rc<SolverUpdates>,
bridge: BridgeType,
}

impl MacroSolverApp {
#[cfg(target_arch = "wasm32")]
fn initialize_bridge(
cc: &eframe::CreationContext<'_>,
data_update: &Rc<Cell<Option<SolverEvent>>>,
data_update: &Rc<SolverUpdates>,
) -> BridgeType {
let ctx = cc.egui_ctx.clone();
let sender = data_update.clone();

<crate::worker::Worker as gloo_worker::Spawnable>::spawner()
.callback(move |response| {
sender.set(Some(response));
match response {
SolverEvent::Progress(_) => sender.progress_update.set(Some(response)),
SolverEvent::IntermediateSolution(_) | SolverEvent::FinalSolution(_) => sender.solution_update.set(Some(response)),
}
ctx.request_repaint();
})
.spawn(concat!("./webworker", env!("RANDOM_SUFFIX"), ".js"))
Expand All @@ -86,14 +94,14 @@ impl MacroSolverApp {
#[cfg(not(target_arch = "wasm32"))]
fn initialize_bridge(
_cc: &eframe::CreationContext<'_>,
_data_cell: &Rc<Cell<Option<SolverEvent>>>,
_data_cell: &Rc<SolverUpdates>,
) -> BridgeType {
BridgeType::new()
}

/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
let data_update = Rc::new(Cell::new(None));
let data_update = Rc::new(SolverUpdates {progress_update: Cell::new(None), solution_update: Cell::new(None) });
let bridge = Self::initialize_bridge(cc, &data_update);

cc.egui_ctx.set_pixels_per_point(1.2);
Expand Down Expand Up @@ -330,15 +338,28 @@ impl MacroSolverApp {
#[cfg(not(target_arch = "wasm32"))]
if let Some(bridge_rx) = &self.bridge.rx {
if let Ok(update) = bridge_rx.try_recv() {
self.data_update.set(Some(update));
match update {
SolverEvent::Progress(_) => self.data_update.progress_update.set(Some(update)),
SolverEvent::IntermediateSolution(_) | SolverEvent::FinalSolution(_) => self.data_update.solution_update.set(Some(update)),
}
}
}

if let Some(update) = self.data_update.take() {
if let Some(update) = self.data_update.progress_update.take() {
match update {
SolverEvent::Progress(progress) => {
self.solver_progress = progress;
}
SolverEvent::IntermediateSolution(_) | SolverEvent::FinalSolution(_) => {
dbg!(update);
}
}
}
if let Some(update) = self.data_update.solution_update.take() {
match update {
SolverEvent::Progress(_) => {
dbg!(update);
}
SolverEvent::IntermediateSolution(actions) => {
self.actions = actions;
}
Expand Down

0 comments on commit b00331b

Please sign in to comment.