fix: apply buffered layout moves optimistically
Keep queued mixer-layout moves visible and replay them over confirmed snapshots so rapid Shift+J/K or Shift+arrow input advances through each destination instead of repeatedly targeting stale order. Tests: cargo test -p tui --all-targets --all-features --locked Tests: cargo clippy -p tui --all-targets --all-features --locked -- -D warnings
This commit is contained in:
parent
6d44716144
commit
a48dcab234
151
tui/src/lib.rs
151
tui/src/lib.rs
|
|
@ -529,6 +529,7 @@ impl MixerClient for BufferedMixerClient {
|
|||
if state.configuration_work.len() >= 64 {
|
||||
return Err(io::Error::other("configuration edit buffer is full"));
|
||||
}
|
||||
apply_optimistic_configuration_edit(&mut state.snapshot, &edit);
|
||||
state
|
||||
.configuration_work
|
||||
.push_back(BufferedAction::EditConfiguration { edit });
|
||||
|
|
@ -709,6 +710,11 @@ fn execute_action<C: MixerClient>(
|
|||
|
||||
fn publish_confirmed(state: &mut BufferedState, confirmed: &MixerSnapshot) {
|
||||
state.snapshot = confirmed.clone();
|
||||
for action in &state.configuration_work {
|
||||
if let BufferedAction::EditConfiguration { edit } = action {
|
||||
apply_optimistic_configuration_edit(&mut state.snapshot, edit);
|
||||
}
|
||||
}
|
||||
let queued_volumes = state.volume_work.keys().cloned().collect::<Vec<_>>();
|
||||
let queued_mutes = state.mute_work.iter().cloned().collect::<Vec<_>>();
|
||||
state.desired_volumes.retain(|id, _| {
|
||||
|
|
@ -741,6 +747,26 @@ fn publish_confirmed(state: &mut BufferedState, confirmed: &MixerSnapshot) {
|
|||
}
|
||||
}
|
||||
|
||||
fn apply_optimistic_configuration_edit(snapshot: &mut MixerSnapshot, edit: &ConfigurationEdit) {
|
||||
let ConfigurationEdit::MoveLayoutEntry { id, position } = edit else {
|
||||
return;
|
||||
};
|
||||
let Some(index) = snapshot
|
||||
.configuration
|
||||
.mixer_layout
|
||||
.iter()
|
||||
.position(|entry| match entry {
|
||||
MixerLayoutEntry::Fader { fader, .. } => fader == id,
|
||||
MixerLayoutEntry::Separator { id: separator, .. } => separator == id,
|
||||
})
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let entry = snapshot.configuration.mixer_layout.remove(index);
|
||||
let position = (*position).min(snapshot.configuration.mixer_layout.len());
|
||||
snapshot.configuration.mixer_layout.insert(position, entry);
|
||||
}
|
||||
|
||||
fn discard_failed_action(state: &mut BufferedState, action: &BufferedAction) {
|
||||
match action {
|
||||
BufferedAction::SetVolume { id, gain } => {
|
||||
|
|
@ -5170,6 +5196,131 @@ mod tests {
|
|||
assert_eq!(mutes.lock().unwrap().as_slice(), &[true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffered_client_applies_rapid_layout_moves_in_order() {
|
||||
struct SlowLayoutClient {
|
||||
snapshot: MixerSnapshot,
|
||||
positions: Arc<Mutex<Vec<usize>>>,
|
||||
}
|
||||
|
||||
impl MixerClient for SlowLayoutClient {
|
||||
fn snapshot(&mut self) -> io::Result<MixerSnapshot> {
|
||||
Ok(self.snapshot.clone())
|
||||
}
|
||||
|
||||
fn set_volume(
|
||||
&mut self,
|
||||
_id: MixerObjectId,
|
||||
_gain: f64,
|
||||
_expected_revision: u64,
|
||||
) -> io::Result<MixerSnapshot> {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn toggle_mute(
|
||||
&mut self,
|
||||
_id: MixerObjectId,
|
||||
_expected_revision: u64,
|
||||
) -> io::Result<MixerSnapshot> {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn edit_configuration(
|
||||
&mut self,
|
||||
edit: ConfigurationEdit,
|
||||
_expected_revision: u64,
|
||||
) -> io::Result<MixerSnapshot> {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
let ConfigurationEdit::MoveLayoutEntry { id, position } = edit else {
|
||||
unreachable!()
|
||||
};
|
||||
let index = self
|
||||
.snapshot
|
||||
.configuration
|
||||
.mixer_layout
|
||||
.iter()
|
||||
.position(|entry| match entry {
|
||||
MixerLayoutEntry::Fader { fader, .. } => fader == &id,
|
||||
MixerLayoutEntry::Separator { id: separator, .. } => separator == &id,
|
||||
})
|
||||
.unwrap();
|
||||
let entry = self.snapshot.configuration.mixer_layout.remove(index);
|
||||
let position = position.min(self.snapshot.configuration.mixer_layout.len());
|
||||
self.snapshot
|
||||
.configuration
|
||||
.mixer_layout
|
||||
.insert(position, entry);
|
||||
self.snapshot.configuration.revision += 1;
|
||||
self.positions.lock().unwrap().push(position);
|
||||
Ok(self.snapshot.clone())
|
||||
}
|
||||
}
|
||||
|
||||
let layout = |id: &str| MixerLayoutEntry::Separator {
|
||||
id: id.to_owned(),
|
||||
label: id.to_owned(),
|
||||
};
|
||||
let mut snapshot = DemoClient::default().view();
|
||||
snapshot.configuration.mixer_layout = vec![layout("a"), layout("b"), layout("c")];
|
||||
let positions = Arc::new(Mutex::new(Vec::new()));
|
||||
let mut client = BufferedMixerClient::spawn(SlowLayoutClient {
|
||||
snapshot,
|
||||
positions: Arc::clone(&positions),
|
||||
})
|
||||
.unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(1);
|
||||
while client
|
||||
.snapshot()
|
||||
.unwrap()
|
||||
.configuration
|
||||
.mixer_layout
|
||||
.is_empty()
|
||||
{
|
||||
assert!(Instant::now() < deadline, "client did not publish layout");
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
|
||||
let first = client
|
||||
.edit_configuration(
|
||||
ConfigurationEdit::MoveLayoutEntry {
|
||||
id: "a".to_owned(),
|
||||
position: 1,
|
||||
},
|
||||
0,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(layout_ids(&first), ["b", "a", "c"]);
|
||||
let second = client
|
||||
.edit_configuration(
|
||||
ConfigurationEdit::MoveLayoutEntry {
|
||||
id: "a".to_owned(),
|
||||
position: 2,
|
||||
},
|
||||
0,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(layout_ids(&second), ["b", "c", "a"]);
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(1);
|
||||
while positions.lock().unwrap().len() < 2 {
|
||||
assert!(Instant::now() < deadline, "layout edits did not drain");
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert_eq!(positions.lock().unwrap().as_slice(), &[1, 2]);
|
||||
}
|
||||
|
||||
fn layout_ids(snapshot: &MixerSnapshot) -> Vec<&str> {
|
||||
snapshot
|
||||
.configuration
|
||||
.mixer_layout
|
||||
.iter()
|
||||
.map(|entry| match entry {
|
||||
MixerLayoutEntry::Fader { fader, .. } => fader.as_str(),
|
||||
MixerLayoutEntry::Separator { id, .. } => id.as_str(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffered_client_starts_disconnected_and_recovers_in_the_background() {
|
||||
struct RecoveringClient {
|
||||
|
|
|
|||
Loading…
Reference in New Issue