From 3a35b4b803b5ce4a32ac71d06398faf613c58a96 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 18 Mar 2022 15:08:00 +0100 Subject: [PATCH] Move model loading into `Watcher` This is slightly more complex, but makes it easier to trigger a model load from other sources than the watcher thread. --- fj-app/src/model.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/fj-app/src/model.rs b/fj-app/src/model.rs index 89b5dcd68..6020ffb3f 100644 --- a/fj-app/src/model.rs +++ b/fj-app/src/model.rs @@ -142,27 +142,13 @@ impl Model { } } - let shape = match self.load(¶meters) { - Ok(shape) => shape, - Err(Error::Compile) => { - // It would be better to display an error in the UI, - // where the user can actually see it. Issue: - // https://github.com/hannobraun/fornjot/issues/30 - println!("Error compiling model"); - return; - } - Err(err) => { - panic!("Error reloading model: {:?}", err); - } - }; - // This will panic, if the other end is disconnected, which // is probably the result of a panic on that thread, or the // application is being shut down. // // Either way, not much we can do about it here, except // maybe to provide a better error message in the future. - tx.send(shape).unwrap(); + tx.send(()).unwrap(); } }, )?; @@ -172,19 +158,39 @@ impl Model { Ok(Watcher { _watcher: Box::new(watcher), channel: rx, + model: self, + parameters, }) } } pub struct Watcher { _watcher: Box, - channel: mpsc::Receiver, + channel: mpsc::Receiver<()>, + model: Model, + parameters: HashMap, } impl Watcher { pub fn receive(&self) -> Option { match self.channel.try_recv() { - Ok(shape) => Some(shape), + Ok(()) => { + let shape = match self.model.load(&self.parameters) { + Ok(shape) => shape, + Err(Error::Compile) => { + // It would be better to display an error in the UI, + // where the user can actually see it. Issue: + // https://github.com/hannobraun/fornjot/issues/30 + println!("Error compiling model"); + return None; + } + Err(err) => { + panic!("Error reloading model: {:?}", err); + } + }; + + Some(shape) + } Err(mpsc::TryRecvError::Empty) => { // Nothing to receive from the channel. None