Skip to content

Commit

Permalink
Make sure pending changes are always saved
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrGlad committed Oct 30, 2023
1 parent e99cbcb commit 2bd5901
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];
Expand Down
23 changes: 19 additions & 4 deletions src/track_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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<RwLock<>> 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();
Expand Down

0 comments on commit 2bd5901

Please sign in to comment.