From 2bd5901c6b84d0c596cc552f884e1962b495db13 Mon Sep 17 00:00:00 2001 From: Petr Gladkikh Date: Mon, 30 Oct 2023 20:06:03 +0100 Subject: [PATCH] Make sure pending changes are always saved --- src/app.rs | 1 + src/engine.rs | 1 - src/track_history.rs | 23 +++++++++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/app.rs b/src/app.rs index b4ffa75..1237855 100644 --- a/src/app.rs +++ b/src/app.rs @@ -83,6 +83,7 @@ impl EmApp { impl eframe::App for EmApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + self.stave.history.do_pending(); if let Some(message) = self.message_receiver.try_iter().last() { match message { Message::UpdateTransportTime(t) => { diff --git a/src/engine.rs b/src/engine.rs index 656284b..086a5d0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -123,7 +123,6 @@ impl Engine { Self::update_track_time(&mut locked); let transport_time = locked.running_at; for s in locked.sources.iter_mut() { - // Alternatively could pass a small pre-allocated array to hold the output events. s.next(&transport_time, &mut queue); } let mut batch = vec![]; diff --git a/src/track_history.rs b/src/track_history.rs index 5a3afe9..e175d2e 100644 --- a/src/track_history.rs +++ b/src/track_history.rs @@ -17,6 +17,12 @@ struct ActionThrottle { action_id: ActionId, } +impl ActionThrottle { + pub fn is_waiting(&self, now: Instant) -> bool { + now - self.timestamp < Duration::from_millis(300) + } +} + // Undo/redo history and snapshots. #[derive(Debug)] pub struct TrackHistory { @@ -55,10 +61,7 @@ impl TrackHistory { let now = Instant::now(); if let Some(throttle) = self.throttle { - // FIXME (impl) Should save a snapshot if there are still pending operations after a quiet period. - if throttle.action_id == action_id - && now - throttle.timestamp < Duration::from_millis(300) - { + if throttle.action_id == action_id && throttle.is_waiting(now) { return; } } @@ -69,6 +72,18 @@ impl TrackHistory { self.update(); } + pub fn do_pending(&mut self) { + // This is ugly, just making it work for now. History may need some scheduled events. + // To do this asynchronously one would need to put id behind an Arc> or + // use Tokio here (and in the engine). + if let Some(throttle) = self.throttle { + if !throttle.is_waiting(Instant::now()) { + self.update(); + self.throttle = None; + } + } + } + /// Normally should not be used from outside. Made it pub as double-borrow workaround. pub fn update(&mut self) { let track = self.track.clone();