Skip to content

Commit 7bf83b4

Browse files
committed
Fix profile switching deadlock
1 parent 0e191c2 commit 7bf83b4

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

src-tauri/src/events/frontend/profiles.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::Error;
22

3-
use crate::shared::DEVICES;
4-
use crate::store::profiles::{get_device_profiles, DEVICE_STORES, PROFILE_STORES};
3+
use crate::store::profiles::{acquire_locks_mut, get_device_profiles, PROFILE_STORES};
54

65
use tauri::command;
76

@@ -12,33 +11,29 @@ pub fn get_profiles(device: &str) -> Result<Vec<String>, Error> {
1211

1312
#[command]
1413
pub async fn get_selected_profile(device: String) -> Result<crate::shared::Profile, Error> {
15-
let devices = DEVICES.read().await;
16-
if !devices.contains_key(&device) {
14+
let mut locks = acquire_locks_mut().await;
15+
if !locks.devices.contains_key(&device) {
1716
return Err(Error::new(format!("device {device} not found")));
1817
}
1918

20-
let mut device_stores = DEVICE_STORES.write().await;
21-
let profile_stores = PROFILE_STORES.read().await;
22-
let selected_profile = device_stores.get_selected_profile(&device)?;
23-
let profile = profile_stores.get_profile_store(devices.get(&device).unwrap(), &selected_profile)?;
19+
let selected_profile = locks.device_stores.get_selected_profile(&device)?;
20+
let profile = locks.profile_stores.get_profile_store(locks.devices.get(&device).unwrap(), &selected_profile)?;
2421

2522
Ok(profile.value.clone())
2623
}
2724

2825
#[allow(clippy::flat_map_identity)]
2926
#[command]
3027
pub async fn set_selected_profile(device: String, id: String) -> Result<(), Error> {
31-
let devices = DEVICES.read().await;
32-
if !devices.contains_key(&device) {
28+
let mut locks = acquire_locks_mut().await;
29+
if !locks.devices.contains_key(&device) {
3330
return Err(Error::new(format!("device {device} not found")));
3431
}
3532

36-
let mut device_stores = DEVICE_STORES.write().await;
37-
let mut profile_stores = PROFILE_STORES.write().await;
38-
let selected_profile = device_stores.get_selected_profile(&device)?;
33+
let selected_profile = locks.device_stores.get_selected_profile(&device)?;
3934

4035
if selected_profile != id {
41-
let old_profile = &profile_stores.get_profile_store(devices.get(&device).unwrap(), &selected_profile)?.value;
36+
let old_profile = &locks.profile_stores.get_profile_store(locks.devices.get(&device).unwrap(), &selected_profile)?.value;
4237
for instance in old_profile.keys.iter().flatten().chain(&mut old_profile.sliders.iter().flatten()) {
4338
if !matches!(instance.action.uuid.as_str(), "opendeck.multiaction" | "opendeck.toggleaction") {
4439
let _ = crate::events::outbound::will_appear::will_disappear(instance, false, false).await;
@@ -52,7 +47,7 @@ pub async fn set_selected_profile(device: String, id: String) -> Result<(), Erro
5247
}
5348

5449
// We must use the mutable version of get_profile_store in order to create the store if it does not exist.
55-
let store = profile_stores.get_profile_store_mut(devices.get(&device).unwrap(), &id).await?;
50+
let store = locks.profile_stores.get_profile_store_mut(locks.devices.get(&device).unwrap(), &id).await?;
5651
let new_profile = &store.value;
5752
for instance in new_profile.keys.iter().flatten().chain(&mut new_profile.sliders.iter().flatten()) {
5853
if !matches!(instance.action.uuid.as_str(), "opendeck.multiaction" | "opendeck.toggleaction") {
@@ -65,7 +60,7 @@ pub async fn set_selected_profile(device: String, id: String) -> Result<(), Erro
6560
}
6661
store.save()?;
6762

68-
device_stores.set_selected_profile(&device, id)?;
63+
locks.device_stores.set_selected_profile(&device, id)?;
6964

7065
Ok(())
7166
}

src/components/ProfileManager.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
if (value == "opendeck_edit_profiles") {
6363
if (oldValue) showPopup = true;
6464
value = oldValue;
65-
} else if (value && (!profile || profile.id != value)) {
65+
} else if (value && value != oldValue && (!profile || profile.id != value)) {
6666
setProfile(value);
6767
oldValue = value;
6868
}

0 commit comments

Comments
 (0)