leverage lock w and r (clean up after AI)

This commit is contained in:
2026-03-25 02:23:06 +08:00
parent 53248243e3
commit cb0b366e96
3 changed files with 20 additions and 46 deletions

View File

@@ -4,7 +4,7 @@ use tauri_specta::Event as _;
use tracing::warn;
use crate::{
get_app_handle,
get_app_handle, lock_r, lock_w,
models::app_state::{AppState, NekoPosition},
services::{app_events::AppStateChanged, neko_positions},
};
@@ -13,12 +13,12 @@ static APP_STATE: LazyLock<Arc<RwLock<AppState>>> =
LazyLock::new(|| Arc::new(RwLock::new(AppState::default())));
pub fn get_snapshot() -> AppState {
let guard = APP_STATE.read().expect("app state lock poisoned");
let guard = lock_r!(APP_STATE);
guard.clone()
}
pub fn set_scene_setup_nekos_position(nekos_position: Option<NekoPosition>) {
let mut guard = APP_STATE.write().expect("app state lock poisoned");
let mut guard = lock_w!(APP_STATE);
guard.scene_setup.nekos_position = nekos_position;
emit_snapshot(&guard);
drop(guard);
@@ -26,13 +26,13 @@ pub fn set_scene_setup_nekos_position(nekos_position: Option<NekoPosition>) {
}
pub fn set_scene_setup_nekos_opacity(nekos_opacity: f32) {
let mut guard = APP_STATE.write().expect("app state lock poisoned");
let mut guard = lock_w!(APP_STATE);
guard.scene_setup.nekos_opacity = nekos_opacity.clamp(0.1, 1.0);
emit_snapshot(&guard);
}
pub fn set_scene_setup_nekos_scale(nekos_scale: f32) {
let mut guard = APP_STATE.write().expect("app state lock poisoned");
let mut guard = lock_w!(APP_STATE);
guard.scene_setup.nekos_scale = nekos_scale.clamp(0.5, 2.0);
emit_snapshot(&guard);
}

View File

@@ -8,7 +8,7 @@ use specta::Type;
use tauri_specta::Event as _;
use crate::{
get_app_handle, lock_r,
get_app_handle, lock_r, lock_w,
models::{dolls::DollDto, friends::FriendshipResponseDto},
services::{app_events::FriendActiveDollSpritesUpdated, sprite},
state::FDOLL,
@@ -29,27 +29,21 @@ pub fn sync_from_app_data() {
let next = build_sprites(&friends);
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES
.write()
.expect("friend active doll sprite projection lock poisoned");
let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
*projection = next;
emit_snapshot(&projection);
}
pub fn clear() {
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES
.write()
.expect("friend active doll sprite projection lock poisoned");
let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
projection.clear();
emit_snapshot(&projection);
}
pub fn remove_friend(user_id: &str) {
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES
.write()
.expect("friend active doll sprite projection lock poisoned");
let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
if projection.remove(user_id).is_some() {
emit_snapshot(&projection);
@@ -57,9 +51,7 @@ pub fn remove_friend(user_id: &str) {
}
pub fn set_active_doll(user_id: &str, doll: Option<&DollDto>) {
let mut projection = FRIEND_ACTIVE_DOLL_SPRITES
.write()
.expect("friend active doll sprite projection lock poisoned");
let mut projection = lock_w!(FRIEND_ACTIVE_DOLL_SPRITES);
match doll {
Some(doll) => match sprite::encode_doll_sprite_base64(doll) {
@@ -88,9 +80,7 @@ pub fn set_active_doll(user_id: &str, doll: Option<&DollDto>) {
}
pub fn get_snapshot() -> FriendActiveDollSpritesDto {
let projection = FRIEND_ACTIVE_DOLL_SPRITES
.read()
.expect("friend active doll sprite projection lock poisoned");
let projection = lock_r!(FRIEND_ACTIVE_DOLL_SPRITES);
FriendActiveDollSpritesDto(projection.clone())
}

View File

@@ -8,7 +8,7 @@ use specta::Type;
use tauri_specta::Event as _;
use crate::{
get_app_handle, lock_r,
get_app_handle, lock_r, lock_w,
models::app_state::NekoPosition,
services::{
app_events::NekoPositionsUpdated,
@@ -48,9 +48,7 @@ pub fn sync_from_app_data() {
guard.user_data.friends.clone().unwrap_or_default()
};
let mut projection = NEKO_POSITIONS
.write()
.expect("neko positions projection lock poisoned");
let mut projection = lock_w!(NEKO_POSITIONS);
projection.friend_active_dolls = friends
.into_iter()
@@ -71,9 +69,7 @@ pub fn sync_from_app_data() {
}
pub fn clear() {
let mut projection = NEKO_POSITIONS
.write()
.expect("neko positions projection lock poisoned");
let mut projection = lock_w!(NEKO_POSITIONS);
projection.self_cursor = None;
projection.friend_cursors.clear();
@@ -83,18 +79,14 @@ pub fn clear() {
}
pub fn update_self_cursor(position: CursorPositions) {
let mut projection = NEKO_POSITIONS
.write()
.expect("neko positions projection lock poisoned");
let mut projection = lock_w!(NEKO_POSITIONS);
projection.self_cursor = Some(position);
emit_snapshot(&projection);
}
pub fn update_friend_cursor(user_id: String, position: CursorPositions) {
let mut projection = NEKO_POSITIONS
.write()
.expect("neko positions projection lock poisoned");
let mut projection = lock_w!(NEKO_POSITIONS);
if !has_friend_active_doll(&mut projection, &user_id) {
if projection.friend_cursors.remove(&user_id).is_some() {
@@ -108,9 +100,7 @@ pub fn update_friend_cursor(user_id: String, position: CursorPositions) {
}
pub fn remove_friend(user_id: &str) {
let mut projection = NEKO_POSITIONS
.write()
.expect("neko positions projection lock poisoned");
let mut projection = lock_w!(NEKO_POSITIONS);
let removed_active_doll = projection.friend_active_dolls.remove(user_id).is_some();
let removed_position = projection.friend_cursors.remove(user_id).is_some();
@@ -121,9 +111,7 @@ pub fn remove_friend(user_id: &str) {
}
pub fn set_friend_active_doll(user_id: &str, has_active_doll: bool) {
let mut projection = NEKO_POSITIONS
.write()
.expect("neko positions projection lock poisoned");
let mut projection = lock_w!(NEKO_POSITIONS);
projection
.friend_active_dolls
@@ -135,16 +123,12 @@ pub fn set_friend_active_doll(user_id: &str, has_active_doll: bool) {
}
pub fn refresh_from_scene_setup() {
let projection = NEKO_POSITIONS
.read()
.expect("neko positions projection lock poisoned");
let projection = lock_r!(NEKO_POSITIONS);
emit_snapshot(&projection);
}
pub fn get_snapshot() -> NekoPositionsDto {
let projection = NEKO_POSITIONS
.read()
.expect("neko positions projection lock poisoned");
let projection = lock_r!(NEKO_POSITIONS);
build_snapshot(&projection)
}